ML Specialization, Advanced Learning course, Course 2, Week 2 Practice Lab Problem

I am getting this error even though my answer is consistent with the hints for this question, exercise 1, I used the non-vector format for the code:

my_softmax(z):         None
tensorflow softmax(z): [0.03 0.09 0.24 0.64]

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-17-9cac3ae3f00f> in <module>
      6 
      7 # BEGIN UNIT TEST
----> 8 test_my_softmax(my_softmax)
      9 # END UNIT TEST

~/work/public_tests.py in test_my_softmax(target)
     10     atf = tf.nn.softmax(z)
     11 
---> 12     assert np.allclose(a, atf, atol=1e-10), f"Wrong values. Expected {atf}, got {a}"
     13 
     14     z = np.array([np.log(0.1)] * 10)

<__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)
   2247 
   2248     """
-> 2249     res = all(isclose(a, b, rtol=rtol, atol=atol, equal_nan=equal_nan))
   2250     return bool(res)
   2251 

<__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)
   2353         y = asanyarray(y, dtype=dt)
   2354 
-> 2355     xfin = isfinite(x)
   2356     yfin = isfinite(y)
   2357     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''

Hello @JMG511

Looks like you have pinpointed a problem that your my_softmax(z) was returning a None. For this, you might want to check the return statement of your function - is there a return statement? If so, try to add some print statements in between your code to show you the content of each variable that you assign value to, and see if those values were consistent with your expectation. There should be no None whatsoever in any of those variables so the first one giving you a None could well be your starting point for debugging.

Good luck!
Raymond

PS: after debugging, always remove those added print statement as they may interfere with the grader.

I didn’t put in a print and don’t know where to put one in. There is one in the subsequent cell that was pre-written, and cannot be edited.

{mentor edit: code removed - not allowed by the Code of Conduct}

how exactly, you mean like print for N, a, and ez_sum? How are those print commands written exactly?

Hi,

I see two method/function definition in your code. Typically, you just need to insert or edit the lines of coder between START CODE HERE and END CODE HERE. And its advised to keep the rest untouched.

syntax for print in python is print(a) for printing the value of a. you could use print(a[j]) as well. please look up python syntax for more

1 Like

I don’t understand what you mean unfortunately, there is a return statement which is consistent with the hints provided. I did add print statements, it did not make an actual difference with respect to this response for the my_softmax, the “none” that you saw.

Hello @JMG511,

We add prints to investigate our code and find problem. Here is an example based on the code skeleton provided by the hint:

Now, you see, I have copied and pasted the code skeleton to the exercise cell, and in between the lines, I added print statements that print the outcome of the previous line’s calculation. In the bottom of the screenshot is part of the output of running the test cell. Let’s examine a few lines of the output:

image
ez_sum is the denominator of the exercise’s equation. The denominator is the summation of exp(z[k]) for all values of k. The output above “2.71828…” is the first summation step, and we can use a calculator to check that the value is as expected.

image
As shown in the screenshot, the output value of “10.10733…” is also consistent with our expectation.

image
a[j] is as defined by the exercise’s equation, and “0.0320586…” is also correct because

I think @Hari_Krishnan_94 has pointed out a major problem, so I would suggest you to first make your exercise cell like the one in my screenshot, then fill in the missing code, run the exercise cell and the cell that contains the test, see if it passes and if not, check the printed numbers step-by-step to find out which one does not meet expectation. Lastly, after the test passed, remove all the prints to avoid them from interfering with the grader.

You may apply this technique for future debugging of exercise that does not pass.

Cheers,
Raymond

Thanks Raymond. It was actually something to do with the fact that I had two def my_softmax(z)'s in there. It was not an easy error to identify as simple as it was.

2 Likes