C4 W4 A1 - Error in verify() function?

Dear all,

I am having the following error on the test function for the verify() function on the Face Recognition assessment

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

Printing the result of the distance calculation I got 0.5436483025550842 which matches with the first case in the cell test,

assert(np.allclose(verify(“images/camera_1.jpg”, “bertrand”, database, FRmodel), (0.54364836, True)))

Any idea?

PD: And the curious thing is that for the next cells the code works
PD2: Definitely there is a problem with all the tests for this function, I have tried all, and in all asserst I find the same error output from above

2 Likes

Does your verify() function return two values - distance as float and door open indicator as boolean?

I think this error is returned by numpy as it is trying to cast some value but is unable to do so. This could occur if the your verify() function doesn’t return 2 values or if the data types of returned values aren’t compatible to expected values.

That would explain why you get an error in asset cell but not the next one. To check, compare your output of that cell to expected:

It’s not Kian, please go away (1.0259346, False)

1 Like

Error found, I forgot to include True and False in the “if” block that was all

1 Like

Right. If the if-else block was not completed, the variable is of type NoneType which cannot be cast to type Boolean, causing the exception you saw before.

I had the same problem and the explanation was that the grader was expecting a scalar (float) but it was receiving an array - so it did not know what to do with it.

Error

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

The solution was just to “flatten” the variable dist in the statement:

#if None:

the grader was expecting a scalar (float) but it was receiving an array

I don’t think you have analyzed the cause and effect here correctly.

The issue is that you need to guarantee that “door_open” is set to either True or False.
The “door_open” return parameter does not have a default value.
You can’t just set it to True if the dist case is met, because if you don’t have an ‘else’ clause, then the “door_open” variable is undefined when the function returns.

Or maybe I don’t understand exactly what you mean by “flatten the variable dist”?

Hi @TMosh ,

The door_open parm is already set to either True or False, just as you said.

The problem in my code was that I was using the following (pseudo)code:

if np.linalg.norm(variable) < scalar:

And that produced the ufunc 'isfinite' TypeError .

So what I did instead, which fixed the issue, (and thus the intent of my comment, so maybe this helps somebody else):

t = np.linalg.norm(variable)
if t < scalar

That seemed to work for me.

Hello,

I had the same issue, the autograder issued the following error message:

Code Cell UNQ_C2: Unexpected error (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’’”)) occurred during function check. We expected function verify to return verify test 1 failed. Please check that this function is defined properly.

I had the following line which had no issue with the couple of test-cased with Younes and Kian, however it broke the autograder.
dist = np.linalg.norm(variable)

Then, after reading the comments here, I casted the np.float32 into a basic python float, both test-cases went thru. The autograder didn’t raise that error anymore.
dist = float(np.linalg.norm(variable))

I think you must have just confused yourself because of the fact that typing new code and then calling the function again does nothing: it just runs the old code again. You have to actually click “Shift-Enter” on the changed cell before you call it again in order to run the new code.

Watch this:

np.random.seed(42)
A = np.random.randn(1,4)
dist = np.linalg.norm(A)
print(f"type(dist) = {type(dist)}")
fdist = float(dist)
print(f"type(fdist) = {type(fdist)}")

type(dist) = <class 'numpy.float64'>
type(fdist) = <class 'float'>

So you get a scalar float in either case.

I’ll bet that the previous code which failed with the error about ufunc 'isfinite' looked like this:

dist = np.linalg.norm(A, axis = -1)
print(f"type(dist) = {type(dist)}")

type(dist) = <class 'numpy.ndarray'>

We’ve seen this a number of times before, e.g. on this recent thread.

You’re correct, I’ve just tried again!!
With axis=-1, the autograder fails like before, even though it goes thru in jupyter.
Without axis=-1, and without the float cast, it passes the autograder.

Just added the following assert. PY is really a pain in the neck for type checking.
assert type(dist) == type(0.7), f"check dist type: {type(dist)} although {type(0.7)} expected"

Good catch, thank you Paul.

Hi, Bertrand.

Thanks very much for following through here and confirming the theory! It always feels better when we can explain what happened. Science in action! :nerd_face:

Regards,
Paul