Assertion Error in Course 1-Week 2 Logistic Regression Assignment

I am getting assertion error in many places in the assignment due to the test code embedded by coursera. Getting assertion error in “initialise with 0”, “propoate, optimise & model” function.

When i wrote the same code without the assert command then there is no error but in the embedded code of coursera which is used for testing the code, assertion method is already there and it cant be removed. So how can I pass this assignment?

PS: I tried running only my code without running test code of coursera but even then I failed the assignment as coursera automatically runs it while submitting it.

Please click my name and message your notebook as an attachment.

Hi Talha Khan,

Welcome to the community.

What value did you put for b? Putting just (0) and (0.) makes a lot of difference. If that is the problem then rectify the changes. Hopefully, it might work.

2 Likes

Hi Rashmi,

Code is working if I write the same code on my own by just removing the assert command and it is also giving correct output. The issue is with assert function that is there in the test code embedded by coursera. Since I am doing it on my organisation’s laptop hence I am unable to attach the SS (due to restrictions by IT) but the problem is with assert function only. I am getting assertion error on at least 4 places and the moment I write the same code without assert, it is resolved and gives correct output. However, the embedded code is still taken into account for marking and my marks are deducted due to error in it.

Hi Balaji,

I am unable to attach file due to restriction in my companies IT policy. Is there any other way to share screen?

For the “initialize with 0” function, what does the cell after it return when you remove the assertions from the function definition? Can you write the exact things it prints so we can see if it is what you really want?

Error:


AssertionError Traceback (most recent call last)
in
2 w, b = initialize_with_zeros(dim)
3
----> 4 assert type(b) == float
5 print ("w = " + str(w))
6 print ("b = " + str(b))

AssertionError:


Output when I run my code removing assert command:

w = [[0.]
[0.]]
b = 0

If your code is not fixed, please send it after you reach home.

It looks like the error is that your b is not of float type. You can try fixing this by setting b equal to float(0) instead of just 0.

1 Like

Balaji, I cant send it as I can only access thru my company’s laptop. This is an enterprise version. I am pasting 1 sample error and correct code below. Similarly I am getting errors in atleast 4 places. Btw I have already submitted the assignment and these errors are showing there- can you not access it there?


Error:

AssertionError Traceback (most recent call last)
in
2 w, b = initialize_with_zeros(dim)
3
----> 4 assert type(b) == float
5 print ("w = " + str(w))
6 print ("b = " + str(b))

AssertionError:

Output when I run my code removing assert command:

w = [[0.]
[0.]]
b = 0

As @martinhiserote wrote, the problem is the type of a variable, “b”.
This assertion is to ensure the variable “b” is a “float” type. But, if you insert just “0” to “b”, its type is “int”. So. you may need to cast “b” to be “float” or just add “.” (period).

“0” and “0.” have different meanings. The former is “int”, and the latter is “float”.

1 Like

Yes, it is working fine now.

Thanks. Its working fine now. However, I have 3 more assertion errors…
2nd error in propagate function:


AssertionError Traceback (most recent call last)
in
6
7 assert type(grads[“dw”]) == np.ndarray
----> 8 assert grads[“dw”].shape == (2, 1)
9 assert type(grads[“db”]) == np.float64
10

AssertionError:

1 Like

I suppose you are better to check the dimension of each variable, since this is a real error, not just a matter of “cast”.
My best guess is an error of matrix operation to calculate “dw”. This is a dot product, not a simple multiplication.

Why not drop it in google drive and share the link over a direct message?

Balaji, the problem is that my company’s laptop prevents me from attaching anything anywhere. The moment I attach a file the system detects it and asks me to remove it without which the system wont work. I am checking with IT if some way out can be found.

But if i remove assertion function and run the code it is giving correct output.

Removing an assertion means, you bypass the error checking. So, everything can be passed. We can not say “it is correct”.

On the other hand, even if the output is not what a test program/grader expects, it may work in some cases. What the coursera platform is doing is a kind of black box testing. A test program sets assumptions and check output shape, type, value and so on. Assertion is the key gate to check those.

So, what is the shape of your “dw” ?

Before I leave, I’m going to post some hints.

Assuming that a cell for “def propagate()” was executed with no error message, which is only a syntax check, the test program found an error in the shape of dw.

dw = \frac{\partial J}{\partial w} = \frac{1}{m}X(A-Y)^T

From the error message, this equation may not be implemented correctly. Note that this is a matrix operation to use np.dot with “transpose” (A-Y). The only one possibility that passes the syntax check but gets an assertion error is simply “multiply” X by (A-Y) without “transpose”. In this case, by a broadcasting function of python, it can calculate dw. But, it is not correct one. The shape of this output is (2,3), not (2,1).
So, in net, from your error message, my best guess is, you did not use np.dot to calculate dw (and did not transpose (A-Y).
The above guess may not be correct, but it is worth to revisit your implementation of dw.

Hi Nobu,

Now I have used np.dot for calculating dw so assertion error is gone but new error is coming as below:

ValueError 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

in propagate(w, b, X, Y)
43 # db = …
44 # YOUR CODE STARTS HERE
—> 45 dw=(1/m)*(np.dot((A-Y).T,X))
46 db=(1/m)*np.sum(A-Y,axis=1)
47

<array_function internals> in dot(*args, **kwargs)

ValueError: shapes (3,1) and (2,3) not aligned: 1 (dim 1) != 2 (dim 0)


Also can you tell me how do I identify in the above error that which part is causing this error. I mean does python highlight the lines which are giving this error. I am unable to debug errors.