Course 4, week 1, assignment 1, exercise 2 conv_single_step how to cast Z to float?

Hello dear DeepLearning.ai community.
In the second exercise of the first week’s assignment on CNN, I get the correct value of Z but I still get the error “You must cast the output to float”.
So far, I’ve tried np.asscalar() and np.astype(float) in a variety of combinations.
Out of the two ways, np.asscalar() makes most sense to me because we are supposed to get a scalar value. Whereas, np.astype(float) inevitably results in Z being a numpy array.
So, what exactly is meant when we are asked to “cast” a numpy array to scalar?
Thank you in advance.
Ivan
P.S. I have just found another function to cast a numpy array to float: np.float(). No luck with that one as well.

“… + np.float64(b)” is the most common usage.

Or you can use np.float(b), it works also.

4 Likes

And the other part of the Z = equation involves np.sum(), which also returns a float.

And the thing you’re summing comes from “np.multiply(…)”.

1 Like

Thank you @TMosh.
Yes, I tried using np.float(b) after I’d found your response here.
But no luck.
I tried two routes. The first one being Z = np.float(Z + np.float(b)), which makes most sense to me because it yields a scalar. So it does. And the scalar is correct. But I get the error. Then I tried Z = Z + np.float(b). That doesn’t make sense to me because it results in a numpy array (with one value). The value itself is correct. But again, I get the same error. Adding 64 or 32 doesn’t make any difference.
Ivan
P.S. After playing with np.float() - literally by adding it and removing it and by adding 64 and removing it - I’ve got the test passed. May be the notebook got out of stuck finally … But thank you very much TMosh!

Did you try this?
{edited}

1 Like

Do not use np.float32().

1 Like

Thank you,
that’s what I did.
I kept adding 64 and removing it. Adding np.float and removing it.
After several iterations of that exercise I got the test passed.
I think the notebook got stuck and then reloaded.
Thank you very much!
Ivan

It’s easy to get the kernel confused and not actually test your current code.

Also quite a few of the notebooks have unintended internal states, due to use of global variables.

It’s always best to manually click the “Save” button, then restart the kernel and run all the cells again.

1 Like

I think the explanation is that the graders in C4 and C5 do not do an automatic “Save” for you. So when you click “Submit”, it is grading whatever the state was the last time you clicked “Save”. What you are seeing is not necessarily what the grader sees.

1 Like