Assignment week2 exercise5 propagate

Hello everyone,

In exercise 5 - propagate, I am getting the following output (including some logging output of mine between `BEGINNING OF FUNCTION BODY` and `END OF FUNCTION BODY`) including an assertion error about the values of `dw`:

``````BEGINNING OF FUNCTION BODY
m: 3
Shape of w: (2, 1)
Shape of X: (2, 3)
Shape of Y: (1, 3)
Shape of Z: (1, 3)
Shape of A: (1, 3)
Shape tmp_cost_val: (1, 3)
Cost: 5.873473260859346
END OF FUNCTION BODY
dw = [[0.93406047]
[2.18899374]]
db = 0.06593791286065272
cost = 5.873473260859346
BEGINNING OF FUNCTION BODY
m: 3
Shape of w: (2, 1)
Shape of X: (2, 3)
Shape of Y: (1, 3)
Shape of Z: (1, 3)
Shape of A: (1, 3)
Shape tmp_cost_val: (1, 3)
Cost: 5.873473260859346
END OF FUNCTION BODY
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-91-be03942b69a5> in <module>
14 print ("cost = " + str(cost))
15
---> 16 propagate_test(propagate)

~/work/release/W2A2/public_tests.py in propagate_test(target)
37     assert type(grads['dw']) == np.ndarray, f"Wrong type for grads['dw']. {type(grads['dw'])} != np.ndarray"
38     assert grads['dw'].shape == w.shape, f"Wrong shape for grads['dw']. {grads['dw'].shape} != {w.shape}"
---> 39     assert np.allclose(grads['dw'], expected_dw), f"Wrong values for grads['dw']. {grads['dw']} != {expected_dw}"
40     assert np.allclose(grads['db'], expected_db), f"Wrong values for grads['db']. {grads['db']} != {expected_db}"
41     assert np.allclose(cost, expected_cost), f"Wrong values for cost. {cost} != {expected_cost}"

AssertionError: Wrong values for grads['dw']. [[0.93406047]
[2.18899374]] != [[0.99845601]
[2.39507239]]
``````

while the expected output should be:

``````Expected output

dw = [[0.99845601]
[2.39507239]]
db = 0.001455578136784208
cost = 5.801545319394553
``````

So first, there seems to be a mistake with my computation of `cost` and then, there seems to be an issue with my gradients `dw` and `db`. I have tried several different ways of computing `cost` and `dw` which I would like to show here, but I am not sure if I would violate the coursera honor code then. How should we proceed here?

Thanks a lot for your help.

Youâ€™re right that weâ€™re not supposed to share source code publicly. But we can do one more step in debugging (at least). If you look at the formulas for dw and db, they arenâ€™t that complicated. But they depend on the values of A and m, so itâ€™s worth also checking those. I added some prints like yours to show those values and hereâ€™s what I get:

``````m = 3
A = [[0.99987661 0.99999386 0.00449627]]
dw = [[0.99845601]
[2.39507239]]
db = 0.001455578136784208
cost = 5.801545319394553
m = 3
A = [[0.99987661 0.99999386 0.00449627]]
All tests passed!
``````

What do you see when you print those values? Iâ€™ll bet that your A value is wrong. Note that is also an input to the cost formula which is also slightly off.

Dear @paulinpaloalto,

Thanks a lot for your advice. Indeed you were right that my value of `A` was wrong. My mistake was that I added the bias term `b` to `X` instead of adding it to to the result of \$w^T X\$. Python broadcasting is great, but in this case, it caused me a lot of headaches. I definitely learnt to pay close attention to these details subtle in the future

1 Like

Cool! Glad to hear that you found the solution. Yes, Iâ€™ve had that same thought about broadcasting before as well: itâ€™s great when it saves you work, but itâ€™s also dangerous in that something that you didnâ€™t intend can â€śjust workâ€ť instead of throwing a â€śsize mismatchâ€ť error. Oh, well! As you say, there is no substitute for attention to detail.