Exercise 3 - conv_forward error

Hi,

I’m working with the convolutional forward prop and I’m getting this error:

25 s = np.multiply(a_slice_prev, W)
26 Z = np.sum(s)
—> 27 Z = Z + np.float(b)
28
29 # YOUR CODE ENDS HERE

TypeError: only size-1 arrays can be converted to Python scalars

It’s strange because the call conv_single_step(a_slice_prev, W[:,:,:,c], b) uses a function done and validated before as correct.
Where is the error?

Thxs!

Hey @Edu4rd , could you mention which week’s assignment is this ? Thanks.

Convolutional Model, Step by Step. The first one of the week 1 for this course.

Exercise 3.

Hi,
I’m really struggling with this exercise. I have been tinkering around, going back to the theory,…
I’m stuck with this error:

----> 8 Z, cache_conv = conv_forward(A_prev, W, b, hparameters)
9 print(“Z’s mean =\n”, np.mean(Z))
10 print(“Z[0,2,1] =\n”, Z[0, 2, 1])

in conv_forward(A_prev, W, b, hparameters)
105
106 # Z[i, h, w, c] = None
→ 107 Z[i,h,w,c] = conv_single_step(a_slice_prev, weights, biases)
108
109

in conv_single_step(a_slice_prev, W, b)
23 # Z = None
24 # YOUR CODE STARTS HERE
—> 25 s = np.multiply(a_slice_prev, W)
26 Z = np.sum(s)
27 Z = Z + np.float(b)

ValueError: operands could not be broadcast together with shapes (2,3,4) (3,3,4)

I would appreciate a hint on what is wrong. It seems like the last slice is not 3,3,4 and that’s causing the error.

1 Like

Hello there!

I am not sure if it is ok to add to posts instead of creating a new one, but I am having exactly the same issue as @Edu4rd .

I also searched all similar comments and followed the advice provided by @TMosh here: Week 1 course 4 Programming Assignment 1 - #2 by TMosh (adding np. to the float).

Unfortunately, this did not work for me. I am also continuing to get the following message:

only size-1 arrays can be converted to Python scalars

However, the value I am getting back is already a scalar:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-22-182241fd5e53> in <module>
      6                "stride": 2}
      7 
----> 8 Z, cache_conv = conv_forward(A_prev, W, b, hparameters)
      9 print("Z's mean =\n", np.mean(Z))
     10 print("Z[0,2,1] =\n", Z[0, 2, 1])

<ipython-input-21-7ac1ac8c5e0d> in conv_forward(A_prev, W, b, hparameters)
     97 
     98                     print(f'Z{i},{h},{w},{c}')
---> 99                     print(f'conv_single_step func -> {conv_single_step(a_slice_prev, weights, biases)}')
    100                     Z[i, h, w, c] = conv_single_step(a_slice_prev, weights, biases)
    101 

<ipython-input-19-0dc5d6ead47d> in conv_single_step(a_slice_prev, W, b)
     26     s = a_slice_prev * W
     27     Z = np.sum(s, dtype=np.float)
---> 28     Z += np.float(b)  
     29 
     30     # YOUR CODE ENDS HERE

TypeError: only size-1 arrays can be converted to Python scalars

The value of Z at crashing time is:

conv_single_step func -> 1.5592957225103126

So why is the tester saying that the result of the function is a non-size-1 array?

(By the way, this previous function passed the tester before).

Thank you for any pointers. Also, I can create a different posting if this was the expectation.

:slight_smile:

The assert is being thrown by np.float(b) in conv_single_step().
This implies that ‘b’ isn’t a scalar.

The issue may be if your print statement at line 99 of conv_forward() is not passing the correct data shapes to conv_single_step().

What is the purpose of that print() statement in line 99? Just for debugging?

Hi @TMosh thanks for the reply. Yes, the print statement is only for debugging purposes.

I will take a look at the shapes…

Hi @TMosh , I checked the dimensions as you suggested and now I get at least a first result for Z.

However, I am still getting the “only size-1 arrays can be converted to Python scalars” error on the 2nd iteration:

TypeError                                 Traceback (most recent call last)
<ipython-input-41-182241fd5e53> in <module>
      6                "stride": 2}
      7 
----> 8 Z, cache_conv = conv_forward(A_prev, W, b, hparameters)
      9 print("Z's mean =\n", np.mean(Z))
     10 print("Z[0,2,1] =\n", Z[0, 2, 1])

<ipython-input-40-95aab82c1890> in conv_forward(A_prev, W, b, hparameters)
     98                     weights = W[ : , : , c : c+1 , c : c+1]
     99                     biases = b [ : , : , c : c+1 , c : c+1]
--> 100                     print(f'conv_single_step func -> {conv_single_step(a_slice_prev, weights, biases)}')
    101                     Z[i, h, w, c] = conv_single_step(a_slice_prev, weights, biases)
    102 

<ipython-input-4-0dc5d6ead47d> in conv_single_step(a_slice_prev, W, b)
     26     s = a_slice_prev * W
     27     Z = np.sum(s, dtype=np.float)
---> 28     Z += np.float(b)
     29 
     30     # YOUR CODE ENDS HERE

TypeError: only size-1 arrays can be converted to Python scalars

Maybe the problem is in the n_c vs n_c_prev dimensions. But I am stuck for several hours now. Any pointers would be appreciated (and the print is for debugging purposes only).

Thank you

Hi @TMosh , please ignore my comment. I posted a new thread. Thank you.

Looks like a problem in your code for weights and biases.
You’re just supposed to slice W and b by selecting ‘c’ in the last index.