C5W1E1 rnn_forward

Hi. I get wrong dimensions in the rnn_forward function.
a_tmp, y_pred_tmp, caches_tmp = rnn_forward(x_tmp, a0_tmp, parameters_tmp)
y_pred[:,:,t] = y_pred

error: too many indices for array

rnn_cell_forward has passed all the tests and it’s correct. Any clue?

Thank you.

Hi @Apostolos ! Welcome back to the community,

The instructions of the function rnn_forward describe in detail the sizes of each object. My first suggestion is: add a “print” statement after each initialization and check that the shapes are ok.

I think that re-defining the y_pred variable is not a good idea. One of those should probably be “yt_pred”.

Hi! Thank you for your time. There are indeed very detailed instructions about arrays shapes. I have already tried printing the shape and it is a 2d array (2,10). Although rnn_cell_forward pass all the tests, the issue is located after using it.

Update: now I am getting wrong values for a.

a[4][1] =
[-0.99999375 -0.994001 -0.99999997 -0.99999767]
a.shape =
(5, 10, 4)

Well, how about y_pred_tmp? You’ll notice that your code just discards that value, right? That can’t have been what you really intended.

I don’t think it discards it.
That’s the entire error message:

a[4][1] = 
 [-0.99999375 -0.994001   -0.99999997 -0.99999767]
a.shape = 
 (5, 10, 4)
y_pred[1][3] =
 [0.79560373 0.89134676 0.06430944 0.66923503]
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)
     75     assert len(caches[0]) == T_x, f"len(cache) must be T_x = {T_x}"
---> 77     assert np.allclose(a[5, 2, 2:6], [0.99999291, 0.99332189, 0.9921928, 0.99503445]), "Wrong values for a"
     78     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"
     79     assert np.allclose(caches[1], x_tmp), f"Fail check: cache[1] != x_tmp"

AssertionError: Wrong values for a

I figured it out. For some reason, I used a0 instead of using a_next in rnn_forward resulting not updating along the time steps. Maybe I changed that in despair because of the previous errors.
Thanks for your time. :slight_smile:

It’s good to hear that you found the solution under your own power. Onward! :nerd_face:

1 Like

Hi @Apostolos and @paulinpaloalto , I had the same error as yours. I want to ask why do we use a_next instead of a0 in computing a_next and yt_pred in our rnn_cell_forward function. Any help would be appreciated. Thanks

Hi, Syed.

I’m not sure I understand your question. rnn_forward takes a0 as an argument, because that is the initial value of the hidden state. But then within the logic of rnn_forward, you are iterating on rnn_cell_forward through all the timesteps. At that level the whole point is that at each timestep the input value of the hidden state is the output of the hidden state from the previous timestep. That will be a0 for the first timestep, but then it changes at each timestep, right? That’s how an RNN does its magic: the state keeps changing as it processes the input sequence one element at a time. That’s the key point of the architecture.

But if I’m just missing your point and answering a different question than you were intending to ask, then please clarify your question.