C5W1A1 optional hw

Problems about function rnn_backward() and lstm_backward().
I passed the test of rnn_cell_backward() and lstm_cell_backward().
And my shapes of results of rnn_backward() and lstm_backward() are correct. But the values seem to be wrong. Below are my codes, I do not know where is the problem, please help me.

(moderator edit: code removed)

1 Like

The problem is in the first function you did not initialized with zeros and the proper shape the gradient da_prev. In the second function you have an extra line in order to initialize the gradient da_prevt specifically da_prevt = da[:, :, -1]. Please initialize this gradient the same way you initialized the others using the correct shape.

i have the same problem…
even I initialize da_prevt with the correct shape with the remaining like above the values are still not like expected :frowning:

As I hinted before da_prevt should be initialized with an np.array of zeros and the appropriate shape and nothing more. The rest of the code (not including the initialization of that gradient) seems OK.

Now it works - thank you very much :slight_smile:

I had a similar issue with rnn_backward function and my code was very similar to what NocturneJay posted. Eventually I made it work and got the expected output but just setting da_prevt to an array of zeros was not sufficient. Only changing this one line means that we pass the array of zeros as the first argument of rnn_cell_backward, and I guess this is not what we want because we should use da. I also modified the line 'gradients = rnn_cell_backward(…, …) after examining Figure 7 in the notebook.


Thank you so much @azotova. I managed to fix my problem after determining Fig. 7 !

1 Like

That is correct you need to include as an argument da in the function rnn_cell_backward or in the function lstm_cell_backward. I though it could be inferred as da is an argument of the function rnn_backward or the function lstm_backward. I hope this reaffirm your comment.

Hello, I don’t know what I am doing wrong but I am getting this error. Please help.

ValueError Traceback (most recent call last)
19 da_tmp = np.random.randn(5, 10, 4)
—> 20 gradients_tmp = lstm_backward(da_tmp, caches_tmp)
22 print(“gradients[“dx”][1][2] =”, gradients_tmp[“dx”][1][2])

in lstm_backward(da, caches)
54 da_prevt = gradients[‘da_prev’]
55 dc_prevt = gradients[‘dc_prev’]
—> 56 dx[:,:,t] = gradients[‘dxt’]
57 dWf += gradients[‘dWf’]
58 dWi += gradients[‘dWi’]

ValueError: could not broadcast input array from shape (8,10) into shape (3,10)

You’ll have better luck if you start a new thread, rather than adding a new reply to one that’s four months old.

I have not worked on the backpropagation exercises, so I don’t have any advice for that.

@raha you ever get this fixed? If you create a new question and add your lstm code I’ll give it a look. Just worked through the back-prop questions so might be able to help.