C5W4 exercise 5 Encoder

When submitting my code, I got this error.
When calling self.pos_encoding(), the parameter(maximum_position_encoding, self.embedding_dim) should be passed through class definition right? Why I got an error message.


TypeError Traceback (most recent call last)
in
1 # UNIT TEST
----> 2 Encoder_test(Encoder)

~/work/W4A1/public_tests.py in Encoder_test(target)
114 x = np.array([[2, 1, 3], [1, 2, 0]])
115
→ 116 encoderq_output = encoderq(x, True, None)
117
118 assert tf.is_tensor(encoderq_output), “Wrong type. Output must be a tensor”

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in call(self, *args, **kwargs)
1010 with autocast_variable.enable_auto_cast_variables(
1011 self._compute_dtype_object):
→ 1012 outputs = call_fn(inputs, *args, **kwargs)
1013
1014 if self._activity_regularizer:

in call(self, x, training, mask)
52 print(“2”, x)
53 # Add the position encoding to embedding
—> 54 x += self.pos_encoding()
55 # Pass the encoded embedding through a dropout layer
56 # use training=training

TypeError: ‘tensorflow.python.framework.ops.EagerTensor’ object is not callable

Hint is already given to you in the Notebook:

  1. Add the position encoding: self.pos_encoding [:, :seq_len, :] to your embedding.

The error message is pretty clear in this case: the code you wrote ends up calling pos_encoding as if it’s a function. You can also see from the instructions that Saif shows that pos_encoding is a tensor, not a function. So that seems like the first thing to figure out: how did you end up treating it as a function?

Might be a stupid qustion… where is seq_len from. I didn’t see it defined anywhere.

Hello @Kejun_Zhang,

seq_len is defined for you by the assignment in the call method of the Encoder class:

image

Cheers,
Raymond

2 Likes