Week # must be added in the tags option of the post.
Link to the classroom item you are referring to:
Description (include relevant info but please do not post solution code or your entire notebook)
I am running into a casting type problem in the Z + b line, resulting in the following error:
Z = [[[-6.99908945]]]
AssertionError Traceback (most recent call last)
in
6 Z = conv_single_step(a_slice_prev, W, b)
7 print(“Z =”, Z)
----> 8 conv_single_step_test(conv_single_step)
9
10 assert (type(Z) == np.float64), “You must cast the output to numpy float 64”
~/work/release/W1A1/public_tests.py in conv_single_step_test(target)
48 expected_output = np.float64(-3.5443670581382474)
49
—> 50 assert (type(Z) == np.float64 or type(Z) == np.float32), “You must cast the output to float”
51 assert np.isclose(Z, expected_output), f"Wrong value. Expected: {expected_output} got: {Z}"
52
AssertionError: You must cast the output to float
In the previous line I can get Z into the right type, but it somehow gets messed-up when casting with the b parameter. The code for the problematic line is:
Yes, the problem is that your Z value was ending up as an array. If you add a scalar and an array, you end up with an array, even if the array has only one element.
But note that there are several ways to accomplish this and they actually gave you instructions for a different way to accomplish the same thing in the comments in the template code:
# Add bias b to Z. Cast b to a float() so that Z results in a scalar value.
# Z = None
Here’s some sample code:
print(f"type(b) {type(b)}")
print(f"b.shape {b.shape}")
print(f"b {b}")
c = b + 0.5
print(f"type(c) {type(c)}")
print(f"c.shape {c.shape}")
print(f"c {c}")
fc = float(c)
print(f"type(float(c)) {type(fc)}")
print(f"float(c) {fc}")
Well, maybe their test is being too picky, but the assertion requires that the final type of Z by np.float64 or np.float32. That is not what you get with the way you wrote it.
What they meant was
Z = Z + float(b)
If you interpret the comment literally, that’s what it says. The way I wrote it the Z before that addition has type np.float64 and adding a type float to that preserves the type as np.float64. I added before and after print statements:
type(Z) before <class 'numpy.float64'>
type(Z) after <class 'numpy.float64'>
Z = -6.999089450680221
type(Z) before <class 'numpy.float64'>
type(Z) after <class 'numpy.float64'>
All tests passed!
I should add that your method of indexing b[0,0,0] to extract one element of the array as a scalar is totally fine. My intent wasn’t to disparage your method. The point was just that they did try to help you out here, if you are feeling that you were given insufficient guidance about how to get around this problem.
It’s great that you found your own solution. Onward!