Week1 assignment 1 rnn_forward

I am having problem with rnn_forward. I use this function:

{moderator edit - solution code removed}

I think it is correct. And my rnn_cell_forward passed test.
The output I get all shapes OK and caches match with expectation. However, wrong values for a and y_pred. Anyone has a helpful comment? Thanks!!!

What will your a[:,:,t-1] give you for the first iteration when t = 0? I did not write the code that way: I just used a_next. Note that should be initialized prior to the loop using the parameter value a0, which is explicitly mentioned in the instructions. You will note from how the test case is constructed that the a0 value that is passed in the test case is non-zero.

If I write it your way, here are the wrong answers I get. Do they agree with yours? Of course the answer to that question will give concrete evidence about whether that is the only mistake in your code or whether there are others.

a[4][1] = 
 [-0.93013738  0.991315   -0.98694298 -0.99723276]
a.shape = 
 (5, 10, 4)
y_pred[1][3] =
 [0.0440187  0.41032346 0.01401205 0.42558194]
y_pred.shape = 
 (2, 10, 4)
caches[1][1][3] =
 [-1.1425182  -0.34934272 -0.20889423  0.58662319]
len(caches) = 
AssertionError                            Traceback (most recent call last)
<ipython-input-8-0cebf7854aa8> in <module>
     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}"
---> 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

Thanks for your hint, Paul. Yes, a0 is nonzero; the moment I realized it was the moment I fixed the problem :slight_smile: :ok_hand:

By the way, a[:,:,t-1] should work just fine because a is initialized to zero and any slice is also appropriately sized zero tensor. The real issue was in a0 initialization.

RIght, since the whole a array is initialized to zeros, it wouldn’t matter which timestep you used, if that had been where the initial value of a_next was supposed to come from. But there is another “teachable moment” there. Are you aware of what myArray[-1] means in python? Try it and see. Then try myArray[-2]. :nerd_face:

-1 points to the last element index, and -2 is the one before last.