Week2, Exercise 8

Hello,

I got this error when I ran my program :

Error: Data type mismatch
Error: Wrong shape
Error: Wrong output
0 Tests passed
3 Tests failed
I have ckecked many things (I did not use num_px nor 12288 for the initialization for example) but I can’t find a way to fix it.

thanks for your help!

Hi, David.

It looks like you posted this in the “Course 2” forum, but I’m guessing from the contents and from your similar post on the Coursera Forums that you are talking about the Logistic Regression exercise in Course 1 Week 2 and the model function specifically.

In order to debug this, let’s try creating a test case that you can see in the notebook that is pretty similar to what the model_test routine does. Click the cell that executes model_test and then click “Insert → Cell Below”. Then copy/paste in the following code:

np.random.seed(42)
tr_Y = np.array([1,0,1]).reshape(1,3)
tr_X = np.random.randn(4, 3)
te_Y = np.array([1,0]).reshape(1,2)
te_X = np.random.randn(4, 2)
t_d = model(tr_X, tr_Y, te_X, te_Y, num_iterations = 50, learning_rate = 0.01, print_cost = True )
print(f"w.shape = {t_d['w'].shape}")
print(f"w = {t_d['w']}")
print(f"b = {t_d['b']}")
print(f"Y_prediction_train = {t_d['Y_prediction_train']}")
print(f"Y_prediction_test  = {t_d['Y_prediction_test']}")

When I run that code, here’s what I see:

Cost after iteration 0: 0.693147
train accuracy: 33.33333333333334 %
test accuracy: 50.0 %
w.shape = (4, 1)
w = [[-0.11233712]
 [-0.13969226]
 [-0.04239255]
 [-0.04846568]]
b = -0.09506014826412587
Y_prediction_train = [[0. 0. 0.]]
Y_prediction_test  = [[1. 1.]]

What happens when you run that?

2 Likes

Dear Paul, thank you very much for your reply.

I have changed a bit my code and I have “All tests passed” :slightly_smiling_face:
Here is what I’ve got after running the program with the data you provided :
“Cost after iteration 0: 0.693147
train accuracy: 66.66666666666667 %
test accuracy: 50.0 %
w.shape = (4, 1)
w = [[0.10213572]
[0.11612811]
[0.01710521]
[0.04208375]]
b = 0.07350354535785854
Y_prediction_train = [[1. 1. 1.]]
Y_prediction_test = [[0. 0.]]”

thanks again,
David

The problem on this notebook is that the “built-in” test cases are broken, so you can’t depend on the fact that all the tests pass. The course staff has a fix in the works. But it will only help to the extent that it will soon say “Tests failed” instead of “Tests passed”. Clearly there is something wrong, since your results are different than mine. Now we need a way to drill down and figure out where the error is. Are you sure that you precisely compared the results of the propagate test cell in the notebook? There they actually give you output values. They all need to match exactly to the full resolution shown. Is that what you see?

Then for the optimize section, the code prints the results, but they don’t show you what the answers should be, so it’s not that useful. Here’s what I got with correct code:

w = [[0.19033591]
 [0.12259159]]
b = [1.92535983]
dw = [[0.67752042]
 [1.41625495]]
db = 0.21919450454067654
Costs = [array(5.80154532)]
All tests passed!

There again, the “All tests passed!” is not the thing to look at. Please compare your numbers to what I show. Are they all exactly the same to the full resolution shown?

Thanks again for your reply.
I had exactly the same result for the results of optimize_test(optimize) so I think we’re OK here.
Then for the example you sent me yesterday, we agree that the results are different (except the cost).
I can give you the results of the print function on the different variables:
after initializing:
w =[[0.]
[0.]
[0.]
[0.]]
[[0.]
[0.]
[0.]
[0.]]
[[0.]
[0.]
[0.]
[0.]]
and b=0.0
0.0
0.0
Then, after using the optimize function:
parameters={‘w’: array([[ 0.00194946],
[-0.0005046 ],
[ 0.00083111],
[ 0.00143207]]), ‘b’: 0.0008311888169172717}
{‘w’: array([[ 0.00194946],
[-0.0005046 ],
[ 0.00083111],
[ 0.00143207]]), ‘b’: 0.0008311888169172717}
{‘w’: array([[ 0.00194946],
[-0.0005046 ],
[ 0.00083111],
[ 0.00143207]]), ‘b’: 0.0008311888169172717}
grads={‘dw’: array([[-0.38934561],
[ 0.10118076],
[-0.16607398],
[-0.28602339]]), ‘db’: -0.16580948911996324}
{‘dw’: array([[-0.38934561],
[ 0.10118076],
[-0.16607398],
[-0.28602339]]), ‘db’: -0.16580948911996324}
{‘dw’: array([[-0.38934561],
[ 0.10118076],
[-0.16607398],
[-0.28602339]]), ‘db’: -0.16580948911996324}
costs=[array(0.69314718)]
[array(0.69314718)]
[array(0.69314718)]
Then, the new w=[[ 0.00194946]
[-0.0005046 ]
[ 0.00083111]
[ 0.00143207]]
[[ 0.00194946]
[-0.0005046 ]
[ 0.00083111]
[ 0.00143207]]
[[ 0.00194946]
[-0.0005046 ]
[ 0.00083111]
[ 0.00143207]]
and the new b=[[ 0.00194946]
[-0.0005046 ]
[ 0.00083111]
[ 0.00143207]]
[[ 0.00194946]
[-0.0005046 ]
[ 0.00083111]
[ 0.00143207]]
[[ 0.00194946]
[-0.0005046 ]
[ 0.00083111]
[ 0.00143207]]
Finaly, Y_prediction_test =[[ 0.00194946]
[-0.0005046 ]
[ 0.00083111]
[ 0.00143207]]
[[ 0.00194946]
[-0.0005046 ]
[ 0.00083111]
[ 0.00143207]]
[[ 0.00194946]
[-0.0005046 ]
[ 0.00083111]
[ 0.00143207]]
and Y_prediction_train =[[1. 1. 1.]]
[[1. 1. 1.]]
[[1. 1. 1.]]
What do you think?
I guess I cannot share my program (?)
I have to admit that I’m new in python scripting, so it’s not really easy for me and I’m glad someone can help!
Best,
David