Planar Data Classification with one hidden layer: Week 3 Assignment Exercise 9

Hi,

I am getting the following error and am unable to understand what I am doing wrong (I have a feeling it has something to do with the inputs to my predict() function). Please and thanks in advance.

The following cell failed:

parameters, t_X = predict_test_case()

predictions = predict(parameters, t_X)
print("Predictions: " + str(predictions))

predict_test(predict)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-22-eb08347ef392> in <module>
      4 print("Predictions: " + str(predictions))
      5 
----> 6 predict_test(predict)

~/work/release/W3A1/public_tests.py in predict_test(target)
    298     output = target(parameters, X)
    299 
--> 300     assert np.allclose(output, expected_output), f"Wrong prediction. Expected: {expected_output} got: {output}"
    301 
    302     print("\033[92mAll tests passed!")

<__array_function__ internals> in allclose(*args, **kwargs)

/opt/conda/lib/python3.7/site-packages/numpy/core/numeric.py in allclose(a, b, rtol, atol, equal_nan)
   2157 
   2158     """
-> 2159     res = all(isclose(a, b, rtol=rtol, atol=atol, equal_nan=equal_nan))
   2160     return bool(res)
   2161 

<__array_function__ internals> in isclose(*args, **kwargs)

/opt/conda/lib/python3.7/site-packages/numpy/core/numeric.py in isclose(a, b, rtol, atol, equal_nan)
   2255     y = array(y, dtype=dt, copy=False, subok=True)
   2256 
-> 2257     xfin = isfinite(x)
   2258     yfin = isfinite(y)
   2259     if all(xfin) and all(yfin):

**TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''**

I would guess it is the output of your predict function, not the input that is causing the problem. You don’t show us the output of the statement:

print("Predictions: " + str(predictions))

That happens before it calls the test routine, so seeing that might shed some light.

Note that you can’t modify the test cell, but you can do “Insert → Cell Below” and then print the type of your predictions value like this:

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

Here’s the output of the cell calling the predict function

Cost after iteration 0: 0.693198
Cost after iteration 1000: 0.000219
Cost after iteration 2000: 0.000108
Cost after iteration 3000: 0.000071
Cost after iteration 4000: 0.000053
Cost after iteration 5000: 0.000043
Cost after iteration 6000: 0.000035
Cost after iteration 7000: 0.000030
Cost after iteration 8000: 0.000027
Cost after iteration 9000: 0.000024
Predictions: {‘W1’: array([[ 0.56305445, -1.03925886],
[ 0.7345426 , -1.36286875],
[-0.72533346, 1.33753027],
[ 0.74757629, -1.38274074]]), ‘b1’: array([[-0.22240654],
[-0.34662093],
[ 0.33663708],
[-0.35296113]]), ‘W2’: array([[ 1.82196893, 3.09657075, -2.98193564, 3.19946508]]), ‘b2’: array([[0.21344644]])}
Cost after iteration 0: 0.693198
Cost after iteration 1000: 0.000219
Cost after iteration 2000: 0.000108
Cost after iteration 3000: 0.000071
Cost after iteration 4000: 0.000053
Cost after iteration 5000: 0.000043
Cost after iteration 6000: 0.000035
Cost after iteration 7000: 0.000030
Cost after iteration 8000: 0.000027
Cost after iteration 9000: 0.000024

Output for the test cell command as suggested above:
type(predictions) = <class ‘dict’>

That looks like the problem: with your code predictions is a python dictionary. It is supposed to be a numpy array with the prediction values. It should be 1 x m. So you need to figure out how that happened. It looks like you probably wrote something like this:

predictions = parameters

But that is not at all what was intended here. Please read the instructions again and consider what it would take to compute the results of forward propagation and then convert those into 1 or 0 depending on whether the output is > 0.5 or not. They give you some hints in the instructions about how to do that.

1 Like

Yes, Paul… Assigning predictions when A2>0.5 seems to have solved the problem… Thanks a lot!