# C5, W1, A1 Exercise 2 run_forward; "Wrong Results for a"

My rnn_cell_forward passes all tests so I’m not sure what’s wrong here. Any help would be greatly appreciated.

## 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) = 2

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)
75 assert len(caches[0]) == T_x, f"len(cache) must be T_x = {T_x}"
76
—> 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

Expected Output:
a[4][1] =
[-0.99999375 0.77911235 -0.99861469 -0.99833267]
a.shape =
(5, 10, 4)
y_pred[1][3] =
[ 0.79560373 0.86224861 0.11118257 0.81515947]
y_pred.shape =
(2, 10, 4)
caches[1][1][3] =
[-1.1425182 -0.34934272 -0.20889423 0.58662319]
len(caches) =
2

This is miscategorized at two levels: it is course 5, not course 4. And it is W1, not W4, right? I’ll edit the title.

Well, rnn_forward is a different function than rnn_cell_forward. Just because rnn_cell_forward passes the test cases doesn’t mean rnn_forward can’t call it incorrectly. So the place to look for the problem is in the rnn_forward logic.

Thanks Paul,

I don’t think I’m doing anything crazy. I initialize a, y_pred, and a_next to zero using np.zeros to the shapes described in the assignment and defaults for the other parameters then just do this inside the loop;

{moderator edit - solution code removed}

So at least for the first iteration, I’m just passing the x array and parameters given and an a_next array of zeros. I would have thought if I’m doing something wrong at least the first result would be correct.

It’s also a little puzzling that the cache values look correct.

That is the core logic and it looks completely correct to me. There aren’t that many other moving parts, but there are still things to do wrong. If you’re sure you’ve checked everything else, then we can switch to “in case of emergency, break glass” mode. Please check your DMs for a message from me.

Just to close the loop here, we had a private conversation involving the actual code and there was a mistake in the initialization portion of the rnn_forward logic.

Hello, again.
Second time I need to come to the forum because I am puzzled with the error.
I got the same mistake that Tom Allen I have the same questions.
After seeing your answer about the error being in the initialization, I still do not get it. It says in the instructions to initialize a and y_pred with zeros. Pretty sure the dimensions are right. And for the third it says
“Initialize the 2D hidden state `a_next` by setting it equal to the initial hidden state, 𝑎0”, so I put a a_next = a0. is that wrong?

1 Like

Ok. I got it right now.
I had put a[:,:,t-1] as the parameter in rnn_cell_forward, so I had to initialize it as a[:,:,-1]=a0.
But then I guessed what the exercise demanded was to put a_next as the parameter, and initializing with a0 was ok in that case.
Thanks for the help Paul. Really appreciated when you are stuck.

1 Like

It good to hear that you were able to find the solution based on the earlier discussion on this thread in addition to a good thought process analyzing the instructions. Onward!

1 Like