C3W1_Assignment - cell with no code of mine produces an error

Hello,
I am going through the Deep N-grams notebook assignment for week 1 of course 3. I’m almost to the end of the notebook. I finished implementing log_perplexity() and all the unit tests passed. However, two cells later (I ran the next cell successfully), I am getting an error.
This is the code in the cell (note: none of this code is mine):

#for line in eval_lines[1:3]:
eval_text = “\n”.join(eval_lines)
eval_ids = line_to_tensor([eval_text], vocab)
input_ids, target_ids = split_input_target(tf.squeeze(eval_ids, axis=0))

preds, status = model(tf.expand_dims(input_ids, 0), training=False, states=None, return_state=True)

#Get the log perplexity
log_ppx = log_perplexity(preds, tf.expand_dims(target_ids, 0))
print(f’The log perplexity and perplexity of your model are {log_ppx} and {np.exp(log_ppx)} respectively’)

This is the error I am getting:


InvalidArgumentError Traceback (most recent call last)
Cell In[42], line 4
2 eval_text = “\n”.join(eval_lines)
3 eval_ids = line_to_tensor([eval_text], vocab)
----> 4 input_ids, target_ids = split_input_target(tf.squeeze(eval_ids, axis=0))
6 preds, status = model(tf.expand_dims(input_ids, 0), training=False, states=None, return_state=True)
8 #Get the log perplexity

Cell In[20], line 12, in split_input_target(sequence)
2 “”"
3 Splits the input sequence into two sequences, where one is shifted by one position.
4
(…)
9 tf.Tensor, tf.Tensor: Two tensors representing the input and output sequences for the model.
10 “”"
11 # Create the input sequence by excluding the last character
—> 12 input_text = sequence[:-1]
13 # Create the target sequence by excluding the first character
14 target_text = sequence[1:]

File /usr/local/lib/python3.8/dist-packages/tensorflow/python/util/traceback_utils.py:153, in filter_traceback..error_handler(*args, **kwargs)
151 except Exception as e:
152 filtered_tb = _process_traceback_frames(e.traceback)
→ 153 raise e.with_traceback(filtered_tb) from None
154 finally:
155 del filtered_tb

File /usr/local/lib/python3.8/dist-packages/tensorflow/python/framework/ops.py:6656, in raise_from_not_ok_status(e, name)
6654 def raise_from_not_ok_status(e, name):
6655 e.message += (" name: " + str(name if name is not None else “”))
→ 6656 raise core._status_to_exception(e) from None

InvalidArgumentError: {{function_node _wrapped__StridedSlice_device/job:localhost/replica:0/task:0/device:GPU:0}} Attempting to slice scalar input. [Op:StridedSlice] name: strided_slice/

What am I doing wrong?

Thanks,
Katrina

Hello, I figured it out myself (or rather, with the help of Claude.ai). Anyway, the issue it seems is that in line_to_tensor() i needed to use tf.strings.bytes_split(line) instead of just list(line).

3 Likes

Very helpful! Cheers.

Thanks for sharing it, I was facing the same issue