Week 2 Programming Assignment Assertion Error

dw = [[-0.08079671]
 [-1.12687945]]
db = 0.20650798115359614
cost = 2.1256720437436023
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-12-7cd89fb9dcd2> in <module>
     14 print ("cost = " + str(cost))
     15 
---> 16 propagate_test(propagate)

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

AssertionError: Wrong values for grads['dw']. [[-0.21186069]
 [-0.42784094]
 [-1.55246367]] != [[-0.03909333]
 [ 0.12501464]
 [-0.99960809]]

Expected output

dw = [[ 0.25071532]
 [-0.06604096]]
db = -0.1250040450043965
cost = 0.15900537707692405

Getting the error mentioned above! I am stuck on this problem for about 3 hours and tried to debug the code in every way I possibly could, but nothing’s working, even tried a fresh copy of the notebook which was a possible fix for this as mentioned in other threads, but still not working. Any help is appreciated!

There is apparently something wrong with your algorithm. The formulas are all given in the text of the instructions. Have you checked the code against those? One way to debug is to look at the A value, which is input to all the other calculations. I added a print statement and here’s what I get:

A = [[0.99979657 0.62245933 0.00273196]]
dw = [[ 0.25071532]
 [-0.06604096]]
db = -0.1250040450043965
cost = 0.15900537707692405
A = [[0.99849882 0.99979657 0.15446527 0.99966465]]
All tests passed!

Does your A value agree with mine?

Hey,

I printed the values of A and got this:

A = [[0.99979657 0.62245933 0.99726804]]

So, only the 1st and 2nd values are matching with yours, but the 3rd one isn’t matching!?

So, actually found the bug in the code!!

In the sigmoid function, I was using

-abs(z)

instead of:

-z

I thought they both return the same value, but no, when I changed it to -z all tests were passed!

Right, those are definitely different. Note that every element of -|z| will be negative regardless of the sign of the corresponding element of z, which is not what happens when you compute -z, right?

Yeh if there’s already a - in the value so it will become + in the later method. Thaks!