C5_W3: one_step_attention_test fails without a clue

I expect one_step_attention to be quite straight-foward but somehow the test fails:

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-24-a8c417adcf76> in <module>
     23     print("\033[92mAll tests passed!")
     24 
---> 25 one_step_attention_test(one_step_attention)

<ipython-input-24-a8c417adcf76> in one_step_attention_test(target)
     19     assert type(context) == tf.python.framework.ops.EagerTensor, "Unexpected type. It should be a Tensor"
     20     assert tuple(context.shape) == (m, 1, n_s), "Unexpected output shape"
---> 21     assert np.all(context.numpy() == expected_output), "Unexpected values in the result"
     22 
     23     print("\033[92mAll tests passed!")

AssertionError: Unexpected values in the result

There are a fair number of moving parts there, so time for some debugging. The first step is to reread the instructions carefully. Then be sure to carefully read all the comments that they gave you in the template code: they give a lot of hints there about what the code looks like.

If that doesn’t shed any light, then add print statements to see the shapes of everything. Here’s what I see:

before repeator s_prev.shape = (10, 64)
after repeator s_prev.shape = (10, 30, 64)
alphas.shape (10, 30, 1)
a.shape (10, 30, 64)
context.shape (10, 1, 64)
All tests passed!

One other observation to make here is that there are a lot of “stateful” objects involved in that function. Make sure you run all the cells in order, in particular the one right before the function cell itself which creates a bunch of the objects used by the function. The easy way to make sure the state of the notebook is consistent is:

Kernel -> Restart and Clear Output
Cell -> Run All

Then check the test cell and see if it still failed.

1 Like

I don’t what happen. Restart the Colab kernel fixes this. No code change at all. This will be the first thing that I try next time I hit any strange error.

2 Likes

To illustrate the issue with notebooks that use global objects, here is a simple example:

Let’s say you have a notebook that has three cells.

  • Cell 1 creates a global object.
  • Cell 2 modifies that object.
  • Cell 3 tests the expected value of the object.

Let’s say when you wrote the code in Cell 2, you made a mistake, so the test in Cell 3 throws an error message.

So you edit Cell 2, and then run Cells 2 and 3 again. This modifies the value of the global object. You do this several times before your code in Cell 2 is correct.

But the test in Cell 3 still fails, because your code in Cell 2 has modified the global object many times. The test in Cell 3 assumes that your code in Cell 2 was only run once.

So you have to go back and clear the state of the notebook, and re-run Cell 1 to create a fresh instance of the object, and run Cell 2 once to modify the object. Then the test in Cell 3 will pass.

I’m not saying this is a good design for the notebook - I’m just explaining a little about why the Course 5 assignments behave this way.

The global object that causes this issue is usually the LSTM itself.

4 Likes