# D5W1 A1 Assignment Exercise 6 rnn_backward need help

in the D5W1 A1 exercise for “Building_a_Recurrent_Neural_Network_Step_by_Step”

Exercise 6 rnn_backward need help I run into a strange problem and need help understanding.

when I run rnn_backward I got this error:

ValueError Traceback (most recent call last)
in
11 a_tmp, y_tmp, caches_tmp = rnn_forward(x_tmp, a0_tmp, parameters_tmp)
12 da_tmp = np.random.randn(5, 10, 4)
—> 13 gradients_tmp = rnn_backward(da_tmp, caches_tmp)
14

in rnn_backward(da, caches)
39 for t in reversed(range(T_x)):
40 # Compute gradients at time step t. Choose wisely the “da_next” and the “cache” to use in the backward propagation step. (≈1 line)
—> 41 gradients = rnn_cell_backward(da_prevt, caches[t])
42 # Retrieve derivatives from gradients (≈ 1 line)

in rnn_cell_backward(da_next, cache)
30 ### START CODE HERE ###
31 # compute the gradient of dtanh term using a_next and da_next (≈1 line)
—> 32 dtanh = da_next * (1 - np.tanh(np.dot(Wax, xt) + np.dot(Waa, a_prev) + ba)**2)
33
34 # compute the gradient of the loss with respect to Wax (≈2 lines)

ValueError: operands could not be broadcast together with shapes (10,4) (5,10)

However a step before when I worked on rnn_cell_backward I got the function working perfectly.

So I am not sure why the aggregation stage ended up with mismatched shape (usually a result of the underlying operation say in rnn_cell_backward). need help in understanding what I may have done wrong here.

My lab ID is pahlbtkn

There are potentially two errors in your code.
The first one is shape related. It looks like you passed a parameter that has the shape of (10,4).
From Traceback, it must be da_prevt. I suppose you initialized da_prevt in a wrong shape.

In addition, passing da_prevt to rnn_cell_backward() is not enough.

Choose wisely the “da_next” and the “cache”…

You have an upstream gradients passed as a parameter. So, you can easily get gradients at time “t”, which should be added to da_prevt.