Week 2 Exercise 5 propagate function

@paulinpaloalto The expected output is same, but there is some assertion error, I could not figure out what is happening, thanks

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]]

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.00154399]
[-0.00492761]] != [[0.99845601]
[2.39507239]]

Expected output

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

1 Like

Here are my results from the test cell for propagate:

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!

My results match the “Expected Values” in the version of the notebook that I have. I’m guessing maybe you have an old version of the notebook. There is a topic on the FAQ Thread about how to refresh to the latest version. Make sure to delete the “dot py” files like public_tests.py when you do the “refresh”.

1 Like

Can you help me to fix the error?

1 Like

You should be using np.log, not just plain log. You did it correctly for the (1 - A) term.

One other thing to check is your parentheses. I think the np.sum and the factor of -\displaystyle \frac {1}{m} only apply to the first term the way you have written the code. That will not end well. :nerd_face:

1 Like

I have problem with same exercise. This what I take as a error.
How can I fix it?

1 Like

It looks like something has gone wrong in your code that results in the grads variable returned by the propagate function being a python string instead of a python dictionary.

Put the following print statement right before the “return” statement in your propagate function:

print(f"type(grads) = {type(grads)}")

When I do that, here’s what I get:

type(grads) = <class 'dict'>

Note that the code that stores your dw and db values in the grads dictionary was given to you and you should not have needed to change it. Here’s what it looks like in the default notebook:


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

If your code looks different than that, then how did that happen?

2 Likes

Thanks for your help. I changed the code and forgat it when try.

1 Like

Hello @paulinpaloalto ,

For below inputs :
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]])

The output is :
dw = [[ 0.75214595]
[-0.19812289]]
db = -0.37501213501318953
cost = 0.15900537707692405
The value of A matrix is matching exactly and cost is also correct.

But dw and db not matching. Request you to kindly help.

Thanks & Regards
Suhas Gupta

1 Like

Here are my results (I added a print statement in the logic to show A):

A = [[0.99979657 0.62245933 0.00273196]]
dw = [[ 0.25071532]
 [-0.06604096]]
db = -0.1250040450043965
cost = 0.15900537707692405

But I guess it makes sense that since your cost value matches, your A is pretty much guaranteed to be correct. So it is only the dw and db values that are wrong. That gives you a pretty good clue where to look: compare your logic to the math formulas shown in the text. There aren’t that many moving parts there.

Here’s one clue: take your dw[0] value and divide it by my dw[0] value:

quot =  0.75214595/0.25071532
print(quot)
2.999999960114124

If you do the same with the db values:

quot = -0.37501213501318953/-0.1250040450043965
print(quot)
3.0000000000000004

And note that m = 3 in this test case, right? Hmmmmm. How does m affect the formulas for dw and db?

1 Like

Thank you very much I got it where my mathematical formula was wrong.

1 Like


How to fix this Assertion error?

1 Like

@paulinpaloalto Could you please check it?

1 Like

Note that the mentors do not have the superpower to examine your notebooks, so I can only reason from the results you show. It’s clear that your cost code generates different values on both of the test cases there, but it looks like everything else is correct. So you need to carefully compare your implementation of the cost to the math formula. If you used np.dot, make sure that you used the transpose correctly. You have two 1 x m vectors, right? So you need to be dotting 1 x m dot m x 1 in order to get a 1 x 1 output. If you dot m x 1 with 1 X m then you get m x m and summing that does not give the same result. Here’s a thread about that.

1 Like

Getting this error in q 5 - Merge all functions into a model
Wrong values for d[‘w’]. [[ 0.14449502]
[-0.1429235 ]
[-0.19867517]
[ 0.21265053]] != [[ 0.08639757]
[-0.08231268]
[-0.11798927]
[ 0.12866053]]

1 Like

Hello Adeel,

Please show us the error log that you are receiving. Thanks!

1 Like

1 Like

The usual cause for that error is “hard-coding” the values of learning rate and number of iterations when you call optimize from model. If there are equal signs mixed in with your parameters, that is a mistake. Or if you omit those parameters, that also counts as “hard-coding”, since it means you are using the default values defined with the optimize function and ignoring the actual values that are requested in the original call to model.

3 Likes

Thanks, @paulinpaloalto error is resolved. Appreciate your team’s quick response.

1 Like

@paulinpaloalto
Can you help me figure out what is the issue in my code?
Code:

{moderator edit - solution code removed}

1st cell error:
File “”, line 44
dw=(1/m)*(np.dot(X,((A-Y).T)))
^
SyntaxError: invalid syntax

Second cell: (can’t find the function for some reason)

NameError Traceback (most recent call last)
in
3 X = np.array([[1., -2., -1.], [3., 0.5, -3.2]])
4 Y = np.array([[1, 1, 0]])
----> 5 grads, cost = propagate(w, b, X, Y)
6
7 assert type(grads[“dw”]) == np.ndarray

NameError: name ‘propagate’ is not defined

1 Like

When you get an error at the beginning of a line like that, it means the error is on the previous line. Check the parentheses on the “cost” line. You’ll find that they don’t match: there are 11 open parens, but only 9 close parens, which is what causes that error. Note that the editor in the notebook is python syntax aware: click on a paren and it will highlight the matching one. Or not :laughing:

2 Likes