Question about C4_W2 Decoder Unit test error

I am working on C4W2_Assignment decoder part, for Exercise 2 DecoderLayer, for unit test, I have a problem with error message as:



InvalidArgumentError Traceback (most recent call last)
Cell In[26], line 2
1 # UNIT TEST
----> 2 w2_unittest.test_decoderlayer(DecoderLayer, create_look_ahead_mask)

File /tf/w2_unittest.py:400, in test_decoderlayer(target, create_look_ahead_mask)
396 cases.append(t)
398 return cases
→ 400 cases = g()
401 print_feedback(cases)

File /tf/w2_unittest.py:388, in test_decoderlayer..g()
386 # Now let’s try a example with padding mask
387 padding_mask = np.array([[[1, 1, 0]]])
→ 388 out, attn_w_b1, attn_w_b2 = decoderLayerq(q, encoderq_output, True, look_ahead_mask, padding_mask)
390 t = test_case()
391 if not np.allclose(out[0, 0], [1.1297308, -1.6106694, 0.32352272, 0.15741566]):

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

Cell In[24], line 67, in DecoderLayer.call(self, x, enc_output, training, look_ahead_mask, padding_mask)
61 Q1 = self.layernorm1(mult_attn_out1 + x)
63 # BLOCK 2
64 # calculate self-attention using the Q from the first block and K and V from the encoder output.
65 # Dropout will be applied during training
66 # Return attention scores as attn_weights_block2 (~1 line)
—> 67 mult_attn_out2, attn_weights_block2 = self.mha2(Q1, enc_output, padding_mask, return_attention_scores=True)
69 # apply layer normalization (layernorm2) to the sum of the attention output and the output of the first block (~1 line)
70 mult_attn_out2 = self.layernorm1(mult_attn_out2 + Q1)

InvalidArgumentError: Exception encountered when calling layer ‘key’ (type EinsumDense).

cannot compute Einsum as input #1(zero-based) was expected to be a int64 tensor but is a float tensor [Op:Einsum] name:

Call arguments received by layer ‘key’ (type EinsumDense):
• inputs=tf.Tensor(shape=(1, 1, 3), dtype=int64)


Someone can help?

I recommend you open the w2_unittest.py file, find the test_decoderlayer() function, and see which test your code doesn’t handle correctly.

That will help you identify the defect in your code.

1 Like