Stuck at function propagate in exercice 5 week2

Hi there,
I am stuck at the function propagate and it seems I am not the only one. This is what I tried:

def propagate(w, b, X, Y):
m = X.shape[1]
A = sigmoid(np.dot(w.T,X)+b)
loga = np.log(A)
log1minusa = np.log(1-A)
logpart1 = np.dot(Y.T,loga)
logpart2 = np.dot((1-Y).T,log1minusa)
cost = -np.sum(logpart1+logpart2,axis=1)
cost = cost[0]/3

dz = np.abs(A-Y)
dw = np.dot(X,dz.T)
dw = dw / m
db = np.sum(dz,axis=1,dtype=‘float64’)/m
db = db[0]

cost = np.squeeze(np.array(cost))

and I get the following error when doing the propagate test afterwards:

dw = [[-0.00145353]
[-0.00466444]]
db = 0.0015419373038432329
cost = 1.8015453193941504


AssertionError Traceback (most recent call last)
in
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.00145353]
[-0.00466444]] != [[0.99845601]
[2.39507239]]

I tried to go on with the rest but I get stopped by other mistakes, I guess I have to solve that one before. Does anybody see what is my mistake ?

grads = {"dw": dw,
         "db": db}

return grads, cost

There are a number of problems with your cost code. Why are you hard-coding the number of samples to 3 when you divide the cost by m?

The way you are doing the transposes is also wrong. Both Y and A (and hence log(A)) are both 1 x m, right? So transposing Y means you will be dotting m x 1 by 1 x m which will give a result with is m x m. That will not end well.

Your code for A and dw and db looks mostly right. The only question is that I don’t remember any absolute values being involved in those calculations: gradients can be both positive and negative, right?

1 Like

Please have a look at this thread which shows examples about how to use transpose in computations like the cost formula here.

db comes out correct but “dw” is also wrong in my case as I used the same above method, The jupyter notebook was last updated on the 23rd of July 2021, were the asserted values updated as well as i have checked multiple times and I didn’t find any problems with the calculation of “dw”, in fact it is rather simple to do by hand and calculation by hand also gives the same wrong answer(according to the grader)!!!

@Zaa: you need to get the latest version of the notebook. If you close and reopen it using the “Work in Browser” button, do you get a clean copy? If not, then use the topic on the FAQ Thread. Note that you need to delete the public_tests.py file as well, as described in the “Important Note” section of that topic.

1 Like

Ok getting a clean copy of public_tests.py solved my problem :slight_smile: Should have read the assignment requirements page properly :D. Thanks for the help!

Thank you Paulinpaloalto for your reply. Indeed I made many mistakes and thinking about it I think I corrected it. I checked it by hand and transposing log(A) to do a 1 x m . m x 1 does the sum I wanted. Moreover, I did not need to do a np.sum for obtaining the cost value. However, even after obtaining a clean copy of the notebook and deleting the public_tests.py file, I still get the error on the assertion. The expected output dw = [[0.99845601]
[2.39507239]], db = 0.001455578136784208 and cost = 5.801545319394553 is different from what I get: dw = [[-0.00154399] [-0.00492761]], db = 0.00145558 (is the only right value) and cost = 0.0015453193941501516

Sorry, but I don’t believe that you completed the “get a fresh copy” procedure correctly. Click “File → Open” and then look in the file public_tests.py. Here is the beginning of the code for propagate_test:

def propagate_test(target):
    w, b = np.array([[1.], [2.], [-1]]), 2.5, 
    X = np.array([[1., 2., -1., 0], [3., 4., -3.2, 1], [3., 4., -3.2, -3.5]])
    Y = np.array([[1, 1, 0, 0]])

    expected_dw = np.array([[-0.03909333], [ 0.12501464], [-0.99960809]])
    expected_db = np.float64(0.288106326429569)
    expected_grads = {'dw': expected_dw,
                      'db': expected_db}
    expected_cost = np.array(2.0424567983978403)
    expected_output = (expected_grads, expected_cost)

Notice that the expected values of dw, db and cost do not agree either with your values or the “expected values” that you show. The test case is different, so you did not correctly “refresh” that file. You need to first delete that file using the “file explorer” UI. Then do the Lab Help → Get Latest Version procedure.

Thank you Paulinpaloalto, indeed after several retries, I obtained the fresh copy of the public_tests.py
I corrected another mistake and finally got it to work as required.