An error in exercice 5 week 1 course 3final assignement

Hi everyone, i hope you are doing well i get an error when compiling this code (it’s not an editable code and all the previous tests are passed successfully)
#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’)
the error:

InvalidArgumentError Traceback (most recent call last)
Cell In[41], 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/

Hi @Nizar2077

eval_ids is a scalar tensor rather than a sequence, which causes the slicing operation in split_input_target to fail. The function tf.squeeze(eval_ids, axis=0) may have removed all dimensions that results in a scalar.

You can check the shape of eval_ids after squeezing to find the error. If it becomes scalar, you will need to use tf.expand_dims to restore the batch dimension or skip squeezing altogether. Also, ensure that the output of line_to_tensor is indeed a tensor with at least one dimension.

Hope it helps! Feel free to ask if you need further assistance!

Hi, i used that code to check the problem i hope it helps (please give me instructions on how to avoid the error ):

Step 1: Validate eval_lines and eval_text

if not eval_lines or len(eval_lines) == 0:
raise ValueError(“eval_lines is empty. Please provide valid input data.”)
eval_text = “\n”.join(eval_lines)
if len(eval_text) < 2:
raise ValueError(“eval_text is too short. Please ensure it contains sufficient data.”)
print(“Length of eval_text:”, len(eval_text))

Step 2: Generate eval_ids and validate its contents

eval_ids = line_to_tensor([eval_text], vocab)
if eval_ids.shape[0] == 0:
raise ValueError(“eval_ids is empty. Check the line_to_tensor function and its inputs.”)
print(“eval_ids shape:”, eval_ids.shape)

Step 3: Check dimensions of eval_ids without modifying

print(“eval_ids dimensions before any processing:”, eval_ids.shape)

Step 4: Validate if eval_ids has enough elements for splitting

if eval_ids.shape[0] < 2:
raise ValueError(“eval_ids does not have enough elements for split_input_target. Ensure adequate sequence length.”)
print(“eval_ids contains enough elements for splitting.”)

this is the output:

Length of eval_text: 41574
eval_ids shape: (1,)
eval_ids dimensions before any processing: (1,)

ValueError Traceback (most recent call last)
Cell In[47], line 20
18 # Step 4: Validate if eval_ids has enough elements for splitting
19 if eval_ids.shape[0] < 2:
—> 20 raise ValueError(“eval_ids does not have enough elements for split_input_target. Ensure adequate sequence length.”)
21 print(“eval_ids contains enough elements for splitting.”)

ValueError: eval_ids does not have enough elements for split_input_target. Ensure adequate sequence length

can you tell me if the problem is related to my code or not so i can understand the situation if i should fix my code or skip the assignement for the moment?
all the previous tests are working perfectly so i don’t think that there’s something wrong with my code

your issue could be related to your build vocabulary where you were suppose to split input target.

Also can you confirm the assignment name?

if this is practice assignment or the grades one.

this is the final graded assignement of the course 3 week 1, this code is not editable and all the previous tests are passed successfully so the problem is that i can’t see an issue with my code

the error you mentioned here you got by running which part of code exercise? 4 or5?

did you pass the log_perplexity grade??

Because based on the error of this ungraded code cell, it is pointing there is issue in the line_to_tensor grade function cell or check the create_batch_dataset grade function codes, especially for the code line covert the data into tensor using a given vocab. check if you have used the correct data recall argument, check if you used lines which could be incorrect in this case.

if unable to find, then send screenshots of the codes your wrote from first grade function till the grade function you got the error.

Remember passing unittest doesn’t confirm your codes are always correct. Also please make sure not to post any codes here, it is against community guidelines.

Click on my name and then message the screenshot of the codes(do not send codes by copy paste)

the problem is solved thanks to @Alireza_Saei ,i was using another code for the line_to_tensor although it passed the test it caused the problem afterwards so thank you for the help

2 Likes