W2 A2 | Wrong values for grads['dw']

I have this propagate function but I can not pass the test:st

{moderator edit - solution code removed}

w =  np.array([[1.], [2.]])
b = 2.
X =np.array([[1., 2., -1.], [3., 4., -3.2]])
Y = np.array([[1, 1, 0]])
grads, cost = propagate(w, b, X, Y)

assert type(grads["dw"]) == np.ndarray
assert grads["dw"].shape == (2, 1)
assert type(grads["db"]) == np.float64


print ("dw = " + str(grads["dw"]))
print ("db = " + str(grads["db"]))
print ("cost = " + str(cost))

propagate_test(propagate)


dw = [[-0.00154399]
 [-0.00492761]]
db = 0.0014555781367842193
cost = 0.0015453193941501516
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-19-7e41fbd3da6a> 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.00154399]
 [-0.00492761]] != [[0.99845601]
 [2.39507239]]

but I do not know what is wrong. please help me.

1 Like

I think that means that either your notebook or your public_tests.py file are out of date. Please use the “get a clean copy” procedure documented on the FAQ Thread. Please read the instructions all the way to the end: you need to delete all the “dot py” files to make sure you get new versions of those as well. Then “copy/paste” your completed code back to the fresh copy of the notebook and let us know if that doesn’t help.

3 Likes

Thanks! solved my problem!

Hello, I’m having the same issue on the same assignment but this did not help solve my problem.

1 Like

@NilAtabey: What do you mean by “the same problem”? Please show the output of the test which is failing. When you say “this didn’t help”, do you mean that you got a fresh copy of both the notebook and the public_tests.py file? Did you read the FAQ Topic all the way to the end where it discusses how to get the “dot py” files associated with the assignment replaced?

I also faced same problem but became success to overcome later on. I followed the instruction of Mr Paulinpaloalto.

1 Like

I am having the same issue. Even after getting new versions of the notebook and public_tests.py it is still saying the result is wrong

[w =  np.array([[1.], [2]])
b = 1.5
X = np.array([[1., -2., -1.], [3., 0.5, -3.2]])
Y = np.array([[1, 1, 0]])
grads, cost = propagate(w, b, X, Y)

assert type(grads["dw"]) == np.ndarray
assert grads["dw"].shape == (2, 1)
assert type(grads["db"]) == np.float64


print ("dw = " + str(grads["dw"]))
print ("db = " + str(grads["db"]))
print ("cost = " + str(cost))

propagate_test(propagate)
 [-0.12340575]]
db = -0.24378625015499036
cost = 0.43826140666475
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-17-7cd89fb9dcd2> in <module>
     14 print ("cost = " + str(cost))
     15 
---> 16 propagate_test(propagate)

~/work/release/W2A2/public_tests.py in propagate_test(target)
     49     assert type(grads['dw']) == np.ndarray, f"Wrong type for grads['dw']. {type(grads['dw'])} != np.ndarray"
     50     assert grads['dw'].shape == w.shape, f"Wrong shape for grads['dw']. {grads['dw'].shape} != {w.shape}"
---> 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}"

AssertionError: Wrong values for grads['dw']. [[-0.00942637]
 [ 0.22120096]
 [-0.89922014]] != [[-0.03909333]
 [ 0.12501464]
 [-0.99960809]]
1 Like

Well, your dw result is the correct shape, but all the values are completely wrong. So there must be something wrong in your code. Please start by carefully comparing your dw code to the math formulas shown in the instructions.

Also notice that there is more than one test there. The first one is visible in the notebook, but the other is performed in the file public_tests.py and it has a different shape for w and hence dw. But your values that we can see even on the first test are also incorrect: they differ from the “Expected Values” that are shown in the notebook. Notice that your db and cost values are also wrong, so I would be worried that maybe your A value is wrong. I added print statements in my propagate code to show m and A and here’s what I see when I run that test cell:

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

What do you see if you print A?

Thank you for the feedback. You were right, I forgot the bias term. I saw this same error in this and similar threads and I just assumed it was an issue with refreshing the notebook, but no, it was me!

That’s great news you were able to find the solution based on that hint. Onward! :nerd_face:

How can I resubmit the assignment? I don’t see the button there anymore?

It wasn’t showing up but after a few attempts of clicking on ‘Work in browser’ it did.

Glad to hear it came back. It’s the very last thing that renders when you open the notebook. The other easy thing worth trying when you don’t see it is:

  1. Kernel → Restart and Clear Output
  2. Save
  3. Submit (if you see the button)

There’s a topic about this general problem on the DLS FAQ thread that also mentions some other points worth being aware of, e.g. that the button only works to grade the “standard” copy of the notebook.