C3W2 exercise 6 Value error

I received the following error for the C3W2 exercise 6.


ValueError Traceback (most recent call last)
Cell In[128], line 7
1 # Try the output for the introduction example
2 #sentence = “Many French citizens are goin to visit Morocco for summer”
3 #sentence = “Sharon Floyd flew to Miami last Friday”
4
5 # New york times news:
6 sentence = “Peter Parker , the White House director of trade and manufacturing policy of U.S , said in an interview on Sunday morning that the White House was working to prepare for the possibility of a second wave of the coronavirus in the fall , though he said it wouldn ’t necessarily come”
----> 7 predictions = predict(sentence, model, sentence_vectorizer, tag_map)
8 for x,y in zip(sentence.split(’ '), predictions):
9 if y != ‘O’:

Cell In[126], line 24, in predict(sentence, model, sentence_vectorizer, tag_map)
22 sentence_vectorized = tf.expand_dims(sentence_vectorized, 0)
23 # Get the model output
—> 24 output = model(sentence_vectorized)
25 # Get the predicted labels for each token, using argmax function and specifying the correct axis to perform the argmax
26 outputs = np.argmax(output, axis = -1)

File /usr/local/lib/python3.8/dist-packages/keras/src/utils/traceback_utils.py:70, in filter_traceback..error_handler(*args, **kwargs)
67 filtered_tb = _process_traceback_frames(e.traceback)
68 # To get the full stack trace, call:
69 # tf.debugging.disable_traceback_filtering()
—> 70 raise e.with_traceback(filtered_tb) from None
71 finally:
72 del filtered_tb

File /usr/local/lib/python3.8/dist-packages/keras/src/engine/input_spec.py:235, in assert_input_compatibility(input_spec, inputs, layer_name)
233 ndim = shape.rank
234 if ndim != spec.ndim:
→ 235 raise ValueError(
236 f’Input {input_index} of layer “{layer_name}” ’
237 “is incompatible with the layer: "
238 f"expected ndim={spec.ndim}, found ndim={ndim}. "
239 f"Full shape received: {tuple(shape)}”
240 )
241 if spec.max_ndim is not None:
242 ndim = x.shape.rank

ValueError: Exception encountered when calling layer ‘sequential’ (type Sequential).

Input 0 of layer “lstm_7” is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (1, 1, 52, 50)

Call arguments received by layer ‘sequential’ (type Sequential):
• inputs=tf.Tensor(shape=(1, 1, 52), dtype=int64)
• training=None
• mask=None

From my understanding in the model building stage for the LSTM for the C3W2 exercise 3,

i have use vocab_size + 1 for the input and mask_zero = True in the sequential stage.

May I know what is the problem that might have occurred in the model building stage for exercise

1 Like

Hi @Loo_Guan_Yee

One of the possible mistakes could be you sentence_vectorizer function. Check your implementation if it exactly matches the expected output (especially dimensions).

Cheers

1 Like

For the sentence vectorization function for C3W2 Exercise 1, i have done the following 2 things.

  1. In the text vectorization function, I have set standardize = “None”.
  2. for the adapt function, I have set it with the sentence as the parameter.

Is there something that i missed out for the implementation of the sentence vectorization function?

1 Like

Hi @Loo_Guan_Yee

No, your implementation of sentence_vectorizer is perfect.

The problem is the way to use it:
it should be used as sentence_vectorizer(sentence)
but not as sentence_vectorizer([sentence]) (which results with additional dimension)

Cheers

1 Like

Hi @arvyzukai,

Thanks for spotting the error in the exercise 6. However, I noticed the test accuracy for the dataset is 0.004. I did not manage to get the expected output from exercise 6.

I have spotted 2 additional errors.

  1. Removed as requested from @arvyzukai.

  2. In the exercise 6, the tagmap keys are the taggings and the value is the index. pred_label should be assigned as labels[tag_idx] instead.

1 Like

Hello @arvyzukai i

arvy for me the it matches with expected output for sentences vectorised but not the vocab_size. I am getting vocab_size of 1000 where as expected output shows 4650. Can I get a hint for this? I feel my get vocabulary code seems to be incorrect, I had used list(sentences)

Regards
DP

Hi @Deepti_Prasad

I’m not sure I understand your question. Are you talking about Exercise 6 (the OP’s question) or are you talking about Exercise 1 (in which case you should start a new thread)?

ok :pensive: will create new post thread

I mean … between us - you are the moderator - you should be telling me this (“Reply as a Linked Topic.” ) :roll_eyes:

Don’t take my class please :smiling_face_with_tear: :sob: I am stuck since yesterday.

the updated assignments has incomplete instructions related to the task and confusing me.

I have created new post.

Regards
DP