W2_A2_Ex-5_propagate function

Hello team,

I am receiving the following error when I run my code:

AssertionError                            Traceback (most recent call last)
<ipython-input-33-7cd89fb9dcd2> in <module>
     14 print ("cost = " + str(cost))
---> 16 propagate_test(propagate)

~/work/release/W2A2/public_tests.py in propagate_test(target)
     51     assert np.allclose(grads['dw'], expected_dw), f"Wrong values for grads['dw']. {grads['dw']} != {expected_dw}"
     52     assert np.allclose(grads['db'], expected_db), f"Wrong values for grads['db']. {grads['db']} != {expected_db}"
---> 53     assert np.allclose(cost, expected_cost), f"Wrong values for cost. {cost} != {expected_cost}"
     54     print('\033[92mAll tests passed!')

AssertionError: Wrong values for cost. [4.26439458e-04 4.26439458e-04 4.23729468e-02 2.00051029e+00] != 2.0424567983978403

Below is my output which seems a bit off:

w = [[ 0.25071532]
db = -0.1250040450043965
cost = [0.15809348 0.15809348 0.15900538]

Expected output:

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

Can someone please assist? My calcs seem to be correct, just not sure why I my cost variable is resulting in an array. I can provide my code sample if needed.

The problem is that cost should be a scalar. Yours is a vector.

For debugging, add some print statements to your code to display the sizes of the variables.

Thanks @TMosh. I see it as a row matrix

[[0.15809348 0.15809348 0.15900538]]

here is the code i used to calc cost

{moderator edit - solution code removed}

If you use np.sum without the axis or keepdims option, you will get a scalar regardless of the shape of the input. The problem is that the code you wrote does not do what you intended. Superficially it looks correct, but check the matching of the parens there. The np.sum only includes the first term, so you end up with a scalar plus a 1 x 3 vector. The notebook editor is syntax aware: click on a paren and it will highlight the matching one.

Thank you team. That worked!

Glad to hear you got it to work. Programming can be frustrating because it requires both high level conceptual understanding and at the same time absolute attention to the most nitty gritty details. A paren in the wrong place or the difference between ; and : can ruin everything. :nerd_face:

Thanks again. Can I please ask, when you mention “sizes” of variables, are you referring to the .shape of the vectors?

Yes, when we are talking about numpy arrays, it’s the “shape” as in:


that is the best way to understand the array you are dealing with.