Course 5 week1 exe2 does not pass

Hi.
I have a problem in exercise 2 rnn_forward. The test does not pass and I do not unserstand why.
This is my code

{moderator edit - solution code removed}

and I get:
AssertionError Traceback (most recent call last)
in
18
19 #UNIT TEST
—> 20 rnn_forward_test(rnn_forward)

~/work/W1A1/public_tests.py in rnn_forward_test(target)
78 assert len(caches[0]) == T_x, f"len(cache) must be T_x = {T_x}"
79
—> 80 assert np.allclose(a[5, 2, 2:6], [0.99999291, 0.99332189, 0.9921928, 0.99503445]), “Wrong values for a”
81 assert np.allclose(y_pred[2, 1, 1: 5], [0.19428, 0.14292, 0.24993, 0.00119], atol=1e-4), “Wrong values for y_pred”
82 assert np.allclose(caches[1], x_tmp), f"Fail check: cache[1] != x_tmp"

AssertionError: Wrong values for a

You’ve not coded the function properly. See this comment for instance:

# initialize “a” and “y_pred” with zeros (≈2 lines)

Oh, I copied the wrong code sorry.
The code which I am testing is:

{moderator edit - solution code removed}

The output is:

AssertionError                            Traceback (most recent call last)
<ipython-input-6-0cebf7854aa8> in <module>
     18 
     19 #UNIT TEST
---> 20 rnn_forward_test(rnn_forward)

~/work/W1A1/public_tests.py in rnn_forward_test(target)
     78     assert len(caches[0]) == T_x, f"len(cache) must be T_x = {T_x}"
     79 
---> 80     assert np.allclose(a[5, 2, 2:6], [0.99999291, 0.99332189, 0.9921928, 0.99503445]), "Wrong values for a"
     81     assert np.allclose(y_pred[2, 1, 1: 5], [0.19428, 0.14292, 0.24993, 0.00119], atol=1e-4), "Wrong values for y_pred"
     82     assert np.allclose(caches[1], x_tmp), f"Fail check: cache[1] != x_tmp"

AssertionError: Wrong values for a

Posting solution code in a public topic is discouraged and can get your account suspended. It’s okay to share stacktrace on a public post and send code to a mentor via direct message. Please clean up the post.

Here’s the mistake:
Given that a is intialized to zeros, you’ll always pass 0 as the previous hidden state in the call to rnn_cell_forward. What we want is to:

  1. Pass 0s for the very first timestep for previous hidden state and get the next hidden state.
  2. Pass next hidden state output at <t-1> as the hidden state to the current timestep.

Got you. Thank you. I did not know how to explain my problem without the code.
I’ll remove the post
Gilad

Can you guide me how to do it?

Made my correction. Sorry about that. Please fix rnn_cell_forward.

I edited your posts to remove the code. But I’m not sure I understand the state of things based on the conversation so far. Have you found the solution yet?

My approach would be to point out that you do not use the parameter value a0 that is passed into rnn_forward. There’s a reason they pass you that value and it is explained in the instructions. That also means that the way you wrote the call to rnn_cell_forward from rnn_forward needs to change to accommodate the proper use of a0 and a_next. You could make that work with your code, but it’s more complicated than it needs to be: just use a_next there.

Sorry if I’m being just a little cryptic there to avoid actually writing code … :nerd_face:

Hi. I managed. Thx. You can remove the post.

1 Like

That’s good news that you found the solution! Thanks for confirming.

It’s fine to leave the thread here: it might help other people who hit a similar problem. I’ve removed the full source code, so it’s ok as is.