Exercise 8 - lstm_backward

Dear community
Here are my code for the assignment, I did not got all the expected output correct. I saw an older thread like this but there was not an answer for this.

{moderator edit - solution code removed}

I compared your code to mine and it looks correct to me. Maybe I missed something, but the first questions are:

  1. Are you sure that your lstm_cell_backward code passes the test cases?
  2. Are you sure that you actually are running the latest version of your code? Just typing new code in a function and calling it again runs the old code, right? You have to click “Shift-Enter” on the changed cell to get the new code loaded.
  3. What is the indication of error you get? Please show us your output.

I’ve just done one more check and lstm_cell_backward got the result the same as the expected output sir. I’ve also running the code from the start and the output still the same. Here is my output. It got wrong answer on dx, da0, dWf and dbf. Thank you for your quick reply!

Yes, I agree those results are wrong. But the code looked right to me. Maybe it’s time to look at your complete notebook. Please check your DMs for a message from me about how to proceed with that.

To close the loop on the public thread, it turns out that the back prop code was correct and that the actual error was in the forward prop code in lstm_forward. That function was used in the test cell for lstm_backward.

The mistake in lstm_forward was to initialize c_next like this:

c_next = c[:,:,0]

They specifically warn you against doing that in the instructions, because it makes c_next a reference to the same memory object as c which does not end well.

One of the “gotchas” to watch out for in python is that objects (e.g. numpy arrays or dictionaries) are passed “by reference” on function calls. So you have to be very careful when you write to an object in a function: you may be modifying a global object. Here’s a thread which discusses some other cases in which that is a problem and shows other examples to watch out for.

1 Like