DLS Course 5 - Week 3, Exercise 1 - one_step_attention() TraceBack Error

I have tried to follow the instructions, but no matter what I try I get an error. From my most recent attempt I get the following Traceback -

ValueError                                Traceback (most recent call last)
<ipython-input-24-82d46b724a9d> in <module>
     19     print("\033[92mAll tests passed!")
---> 21 one_step_attention_test(one_step_attention)

<ipython-input-24-82d46b724a9d> in one_step_attention_test(target)
      9     a = np.random.uniform(1, 0, (m, Tx, 2 * n_a)).astype(np.float32)
     10     s_prev =np.random.uniform(1, 0, (m, n_s)).astype(np.float32) * 1
---> 11     context = target(a, s_prev)
     13     assert type(context) == tf.python.framework.ops.EagerTensor, "Unexpected type. It should be a Tensor"

<ipython-input-23-3118bf2e23ba> in one_step_attention(a, s_prev)
     17     ### START CODE HERE ###
     18     # Use repeator to repeat s_prev to be of shape (m, Tx, n_s) so that you can concatenate it with all hidden states "a" (≈ 1 line)
---> 19     s_prev = repeator(s_prev.shape)
     20     # Use concatenator to concatenate a and s_prev on the last axis (≈ 1 line)
     21     # For grading purposes, please list 'a' first and 's_prev' second, in this order.

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
    980       with ops.name_scope_v2(name_scope):
    981         if not self.built:
--> 982           self._maybe_build(inputs)
    984         with ops.enable_auto_cast_variables(self._compute_dtype_object):

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in _maybe_build(self, inputs)
   2616     if not self.built:
   2617       input_spec.assert_input_compatibility(
-> 2618           self.input_spec, inputs, self.name)
   2619       input_list = nest.flatten(inputs)
   2620       if input_list and self._dtype_policy.compute_dtype is None:

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
    156                      str(len(input_spec)) + ' inputs, '
    157                      'but it received ' + str(len(inputs)) +
--> 158                      ' input tensors. Inputs received: ' + str(inputs))
    159   for input_index, (x, spec) in enumerate(zip(inputs, input_spec)):
    160     if spec is None:

ValueError: Layer repeat_vector_1 expects 1 inputs, but it received 2 input tensors. Inputs received: [<tf.Tensor: shape=(), dtype=int32, numpy=10>, <tf.Tensor: shape=(), dtype=int32, numpy=64>]

As seen in the stack trace I try

s_prev = repeator(s_prev.shape)

Attempts that include turning this into two parameters also fail. From my understanding, after the RepeatVector is initialized in the pre-written portion we have a RepeatVector of (,Tx,). According to the Tensorflow/Keras documentation, (tf.keras.layers.RepeatVector  |  TensorFlow v2.14.0).

Input shape: 2D tensor of shape (num_samples, features) . Output shape: 3D tensor of shape (num_samples, n, features) .

So, I was trying to pass in (m, n_s), which would be the right shape. I did

 s_prev = repeator(s_prev.shape)


 s_prev = repeator(s_prev.shape[0], s_prev.shape[1])


 s_prev = repeator([s_prev.shape])

All to no avail. Am I getting the logic of this function wrong? Any help would be appreciated. Thank you!

Don’t include .shape there.
If you include .shape, you’re only passing a tuple that holds the shape of s_prev - not s_prev itself.

s_prev already will have the correct shape after repeator() processes it. The instructions tell you what shape s_prev will have - not what shape you have to pass to the function.

Thank you for the quick response. The Tensorflow documentation’s wording kept leading me to think I had to input a tuple. It works now, thank you!