C2W2 programming assignment "Optimization Methods"

Have been unable to get past first part.
Code seems to work for W1 & b1, but fails for W2.
This is what I have:

    # YOUR CODE STARTS HERE
    parameters['W' + str(l)] -= learning_rate * grads['dW' + str(l)]
    parameters['b' + str(l)] -= learning_rate * grads['db' + str(l)]
    # YOUR CODE ENDS HERE

which returns …

W2 =
[[ 0.3190391 -0.24937038 1.46210794]
[-2.06014071 -0.3224172 -0.38405435]
[ 1.13376944 -1.09989127 -0.17242821]]

with expected…

AssertionError: Wrong values for W2. Check the formulas. Expected:
[[ 0.32171798 -0.25467393 1.46902454]
[-2.05617317 -0.31554548 -0.3756023 ]
[ 1.1404819 -1.09976462 -0.1612551 ]]

Can anyone see my mistake??

The code you show looks fine and is independent of the layer number. So if you get the correct results for layer 1, but not layer 2, the bug must be elsewhere. E.g. maybe your loop indices are wrong or something else like that which would affect only some layers …

Ok, you can’t modify the test cell, but I used “Insert → Cell Below” and added this code to find out what the original value of W2 is before the “update”:

parameters, grads, learning_rate = update_parameters_with_gd_test_case()
print("W2 =\n" + str(parameters["W2"]))

Here’s the output I get:

W2 =
[[ 0.3190391  -0.24937038  1.46210794]
 [-2.06014071 -0.3224172  -0.38405435]
 [ 1.13376944 -1.09989127 -0.17242821]]

Notice that is the same as your incorrect value of W2. So you must have modified the template code of the “for” loop and broken it so that it no longer covers layer 2.

If you want to get a clean copy of the notebook and see what the template code originally looked like, there’s a topic about that on the FAQ Thread. It’s not illegal to modify the template code, but it requires extreme care.

Thanks for the quick reply… I tried that, but the new cell doesn’t know what “update_parameters_with_gd_test_case()” is. I get:

NameError: name ‘update_parameters_with_gd_test_case’ is not defined

That just means you need to run the previous cells in the notebook: “Cell → Run All Above”.

Sorry, my bad!! I thouht that I had reloaded all previous cells…

So, I’m with you, that it has to be a loop count issue. However, in my feeble mind, I had assumed that since that line is outside of the
# YOUR CODE STARTS HERE

# YOUR CODE ENDS HERE
comments, and as it was given to us, it was correct. So, I’ve not touched it… thus my mind went to “I must be doing something wrong here…”

as print statement verified that L is indeed 2 (just before the loop), therefore we should get W1 and W2… This is why I sought assistance…

OK Paulinpaloalto, I added an extra loop and that fixed the situation… You may wish to look at the code. I had to go from 1 to L+1.

Yes, that’s the way it is in the template code, so you must have changed it previously. The point is that loop indices are like array indices in python: they are 0-based. Try this loop and watch what happens:

for ii in range(1,5):
    print(f"ii = {ii}")

For the record, here is the template code as it was given to you:

    L = len(parameters) // 2 # number of layers in the neural networks

    # Update rule for each parameter
    for l in range(1, L + 1):
        # (approx. 2 lines)
        # parameters["W" + str(l)] =  
        # parameters["b" + str(l)] = 
        # YOUR CODE STARTS HERE

As I suggested in my earlier post, there is a way to get a clean copy and confirm what I just showed.