# [Week 3 Exercise 8 Wrong Value of W1], Please help

Stuck at this step due to the below error. Tried multiple things. Everything before this works perfectly fine. Lab ID: dhcenzyt

Cost after iteration 0: 0.693059
Cost after iteration 1000: 0.693059
Cost after iteration 2000: 0.693059
Cost after iteration 3000: 0.693059
Cost after iteration 4000: 0.693059
Cost after iteration 5000: 0.693059
Cost after iteration 6000: 0.693059
Cost after iteration 7000: 0.693059
Cost after iteration 8000: 0.693059
Cost after iteration 9000: 0.693059
W1 = [[-0.00643025 0.01936718]
[-0.02410458 0.03978052]
[-0.01653973 -0.02096177]
[ 0.01046864 -0.05990141]]
b1 = [[-1.02420756e-06]
[ 1.27373948e-05]
[ 8.32996807e-07]
[-3.20136836e-06]]
W2 = [[-0.01041081 -0.04463285 0.01758031 0.04747113]]
b2 = [[0.00010457]]

AssertionError Traceback (most recent call last)
in
----> 1 nn_model_test(nn_model)

~/work/release/W3A1/public_tests.py in nn_model_test(target)
292 assert output[“b2”].shape == expected_output[“b2”].shape, f"Wrong shape for b2."
293
→ 294 assert np.allclose(output[“W1”], expected_output[“W1”]), “Wrong values for W1”
295 assert np.allclose(output[“b1”], expected_output[“b1”]), “Wrong values for b1”
296 assert np.allclose(output[“W2”], expected_output[“W2”]), “Wrong values for W2”

AssertionError: Wrong values for W1

Notice that your cost values are not changing. That would suggest that one place to look is at your “update parameters” logic. Maybe that is failing and not actually applying the changes. If your previous functions all pass their tests, it would mean that the bug is in nn_model. In other words, the functions are correct, but you are calling them incorrectly. E.g. not storing the results of update_parameters in the right variable name or the like.

Did try updating the nn_model to have parameters = update_parameters(parameters, grads) in order to explicitly assign the values of parameters, but with that I am getting the following error:

UnboundLocalError Traceback (most recent call last)
in
----> 1 nn_model_test(nn_model)

~/work/release/W3A1/public_tests.py in nn_model_test(target)
261
262 t_X, t_Y = nn_model_test_case()
→ 263 parameters = target(t_X, t_Y, n_h, num_iterations=10000, print_cost=True)
264
265 print("W1 = " + str(parameters[“W1”]))

in nn_model(X, Y, n_h, num_iterations, print_cost)
44
45 # YOUR CODE STARTS HERE
—> 46 forward_propagation(X,parameters)
47 compute_cost(A2,Y)
48 backward_propagation(parameters,cache,X,Y)

UnboundLocalError: local variable ‘parameters’ referenced before assignment

That suggests that the mistake is what I suggested before: you are misspelling the variable name someplace. I suggest you use the browser ^F to find all instances of parameters.

Or perhaps you forgot to call the “initialize” routine to initialize the parameters dictionary first. Where is that variable initialized in your nn_model code?

Fixed the initialization problem like you mentioned. The code runs now but gives the same issue as before that the cost function doesn’t change. Checked for spelling errors couldn’t find any.
Cost after iteration 0: 0.693059
Cost after iteration 1000: 0.693059
Cost after iteration 2000: 0.693059
Cost after iteration 3000: 0.693059
Cost after iteration 4000: 0.693059
Cost after iteration 5000: 0.693059
Cost after iteration 6000: 0.693059
Cost after iteration 7000: 0.693059
Cost after iteration 8000: 0.693059
Cost after iteration 9000: 0.693059
W1 = [[ -2.78075372 24.6551251 ]
[ -9.86567503 84.07448507]
[ 3.81694612 -34.36674759]
[ 11.14201259 -97.19823001]]
b1 = [[-0.00126684]
[ 0.04581774]
[ 0.00228186]
[-0.00655761]]
W2 = [[ 0.21044987 -44.43253262 15.06883275 30.69468426]]
b2 = [[0.13070777]]

Figured out my mistake. Thanks for all your help!

It’s great to hear that you found the solution! Thanks for confirming.

Dear Community,
I am also struggling a bit with this week’s assignment and i would like to ask for some help.
i think i have a quite complex problem, let me start explaining.
First I also had this same problem, when cost function does not decreasing. Moreover i have b values still 0…

Cost after iteration 0: 0.692686
Cost after iteration 1000: 0.692686
Cost after iteration 2000: 0.692686
Cost after iteration 3000: 0.692686
Cost after iteration 4000: 0.692686
Cost after iteration 5000: 0.692686
Cost after iteration 6000: 0.692686
Cost after iteration 7000: 0.692686
Cost after iteration 8000: 0.692686
Cost after iteration 9000: 0.692686
W1 = [[0.00550798 0.00708148]
 [0.00290905 0.00510828]
 [0.00892947 0.00896293]
 [0.00125585 0.00207243]]
b1 = [[0.]
 [0.]
 [0.]
 [0.]]
W2 = [[0.00051467 0.0044081  0.00029876 0.00456833]]
b2 = [[0.]]
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-20-1243cded3c7d> in <module>
----> 1 nn_model_test(nn_model)

~/work/release/W3A1/public_tests.py in nn_model_test(target)
    292     assert output["b2"].shape == expected_output["b2"].shape, f"Wrong shape for b2."
    293 
--> 294     assert np.allclose(output["W1"], expected_output["W1"]), "Wrong values for W1"
    295     assert np.allclose(output["b1"], expected_output["b1"]), "Wrong values for b1"
    296     assert np.allclose(output["W2"], expected_output["W2"]), "Wrong values for W2"

AssertionError: Wrong values for W1

So i started troubleshooting backwards, taking a look at update function.
The more interesting is that in the update function the returned b2 value has some not appropriate dimenson and i dont know where it comes from.

W1 = [[-0.00643025  0.01936718]
 [-0.02410458  0.03978052]
 [-0.01653973 -0.02096177]
 [ 0.01046864 -0.05990141]]
b1 = [[-1.02420756e-06]
 [ 1.27373948e-05]
 [ 8.32996807e-07]
 [-3.20136836e-06]]
W2 = [[-0.01041081 -0.04463285  0.01758031  0.04747113]]
b2 = [[1.21732533e-05]
 [2.12263977e-05]
 [1.36755874e-05]
 [1.05251698e-05]]
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-22-c37f65e5c916> in <module>
      7 print("b2 = " + str(parameters["b2"]))
      8 
----> 9 update_parameters_test(update_parameters)

~/work/release/W3A1/public_tests.py in update_parameters_test(target)
    245     assert output["b1"].shape == expected_output["b1"].shape, f"Wrong shape for b1."
    246     assert output["W2"].shape == expected_output["W2"].shape, f"Wrong shape for W2."
--> 247     assert output["b2"].shape == expected_output["b2"].shape, f"Wrong shape for b2."
    248 
    249     assert np.allclose(output["W1"], expected_output["W1"]), "Wrong values for W1"

AssertionError: Wrong shape for b2.

Although i hope i implemented the right recall operations like:

W1 = copy.deepcopy(parameters["W1"])
...
dW1 = grads["dW1"]

and learning rate determinition

W1 = W1 - learning_rate * dW1
...

So I went back to init parameters and found that even the random initialization is not correct

W1 = [[0.00435995 0.00025926]
 [0.00549662 0.00435322]
 [0.00420368 0.00330335]
 [0.00204649 0.00619271]]
b1 = [[0.]
 [0.]
 [0.]
 [0.]]
W2 = [[0.00299655 0.00266827 0.00621134 0.00529142]]
b2 = [[0.]]
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-10-0eb4c3a6d62e> in <module>
      8 print("b2 = " + str(parameters["b2"]))
      9 
---> 10 initialize_parameters_test(initialize_parameters)

~/work/release/W3A1/public_tests.py in initialize_parameters_test(target)
     57     assert parameters["b2"].shape == expected_output["b2"].shape, f"Wrong shape for b2."
     58 
---> 59     assert np.allclose(parameters["W1"], expected_output["W1"]), "Wrong values for W1"
     60     assert np.allclose(parameters["b1"], expected_output["b1"]), "Wrong values for b1"
     61     assert np.allclose(parameters["W2"], expected_output["W2"]), "Wrong values for W2"

AssertionError: Wrong values for W1

after the random.rand(n_h, n_y) * 0.01 how is it even possible that these random numbers are not accepted?

I would appreciate some explanational comments.
Hope the equations in codes inserted doesn’t mean any problem.

Regards

That is the wrong random function to use. The clue is that all the values are positive, but they should be both positive and negative. They literally wrote out the correct code for you in the instructions. Please have a closer look! Also note that the unit tests for that section should have failed. You can’t just ignore those failures and expect it all to work later when you call those lower level functions.

But that can’t be the only problem. Check your logic for the update of b2, although it was the right shape in one case. The other way that b2 could end up with the wrong shape is if your db2 gradient value is the wrong shape. So also please check the gradient computation logic and make sure you didn’t just “copy/paste” the logic for b1 for b2.

If the costs don’t change that suggests yet a different type of problem that was discussed earlier on this thread: maybe you don’t use the same variable name for the parameters dictionary in the various places you reference that in nn_model.

Thank you for your detailed response!
I found the mistyping of the random function, now it works fine. And all my assignment is working now, yet i have a question.
You mentioned before, not to ignore any test failures.
I have a failure during the cost function calculations after implementing the suggested equations

logprobs = np.multiply(np.log(A2),Y)
cost = - np.sum(logprobs)

The test of the section gives an error, regarding to my cost function value.

cost = 0.6926858869721941
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-156-9523b300d7a7> in <module>
      3 print("cost = " + str(compute_cost(A2, t_Y)))
      4 
----> 5 compute_cost_test(compute_cost)

~/work/release/W3A1/public_tests.py in compute_cost_test(target)
    131 
    132     assert type(output_1) == float, "Wrong type. Float expected"
--> 133     assert np.isclose(output_1, expected_output_1), f"Wrong value. Expected: {expected_output_1} got: {output_1}"
    134 
    135     assert type(output_2) == float, "Wrong type. Float expected"

AssertionError: Wrong value. Expected: 0.5447066599017815 got: 1.0493608309109266

Expected output

cost = 0.6930587610394646
All tests passed!
 All tests passed.

Regarding to the compute_cost_test my value is not acceptable, but the text below show different value. Are there still any problems with function implementation?

Regards

A difference in the third decimal place is not a rounding error: it is a real error. We are doing 64 bit floating point, so rounding errors are on the order of 10^{-16} in the mantissa. You’ve made a very common mistake on the cost: you took the sample code that they gave you as the complete answer. It’s not and they actually said that in the instructions, although apparently they were a bit too subtle about it. Compare that code to the mathematical formula for the cost shown in the instructions and ask yourself two questions:

  1. Why is there only one term? What happened to the Y = 0 term?
  2. What happened to the factor of \frac {1}{m}?
3 Likes

Yes, now i noticed the answers for those two questions, thank you so much for your help!