Exercise 9 - L_model_backward ReLu key error


I’ve been wracking my head over this one. The stub for ReLu portion of the backdrop is confusing. One, the iterator with reversed(range(L-1)) doesn’t make sense to me. Why the iterator?

I’ve read plenty of documentation on iterators and it seems the technique from the forward prop using integers is sufficient.

Thus, I get a key error because I am not able to get produce dW2. Printing out ‘l’, it says ‘0’. That means grads[“dW” + str(l + 1)] = dW_temp will never produce a key for dW2.

Can someone help me clear it up? Thanks a lot.


The first step is to make sure you understand how loop ranges work in python. Everything is “0-based”, right? Run this code and watch what happens:

for ii in reversed(range(4)):
    print(f"ii = {ii}")

print(f"After loop ii = {ii}")

That gives this result:

ii = 3
ii = 2
ii = 1
ii = 0
after loop ii = 0

And in this particular test case, L = 2, so the loop is only going to run once for l = 0. That will give you dW1 and db1. So where do dW2 and db2 come from? The output layer, which happens before the loop, right? So why did that not happen in your code?

Thanks, Paul. I had grads[“dW” + str(L-1)] where it should have been simply L, as the doc string says!