Week 1 Conv_Forward

Hi all, I am having difficulty sorting out my conv_forward function. I have been getting the following error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-95-7e580406a9e8> in <module>
      6                "stride": 2}
      7 
----> 8 Z, cache_conv = conv_forward(A_prev, W, b, hparameters)
      9 z_mean = np.mean(Z)
     10 z_0_2_1 = Z[0, 2, 1]

<ipython-input-94-5e8a8e42af02> in conv_forward(A_prev, W, b, hparameters)
     68                     biases = b[0,0,0,c]
     69 
---> 70                     Z[i,h,w,c] = conv_single_step(a_slice_prev, weights, biases)
     71 
     72     # YOUR CODE ENDS HERE

<ipython-input-8-261b0a936c73> in conv_single_step(a_slice_prev, W, b)
     23     # Z = None
     24     # YOUR CODE STARTS HERE
---> 25     s = a_slice_prev * W
     26     Z = np.sum(s)
     27     Z = Z + float(b)

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

This is occurring after following the following strategy:

I’d appreciate any help.

It looks like you did not apply all the suggestions in that other thread. In particular you missed this one:

And this one:

It also appears that you have something wrong with the output w dimension. The h value is correct, but you’re using the input w value. So you need to look carefully to see how that happened. Here’s my output from that test cell including some added print statements to show various shapes:

stride 2 pad 1
New dimensions = 3 by 4
Shape Z = (2, 3, 4, 8)
Shape A_prev_pad = (2, 7, 9, 4)
Z[0,0,0,0] = -2.651123629553914
Z[1,2,3,7] = 0.4427056509973153
Z's mean =
 0.5511276474566768
Z[0,2,1] =
 [-2.17796037  8.07171329 -0.5772704   3.36286738  4.48113645 -2.89198428
 10.99288867  3.03171932]
cache_conv[0][1][2][3] =
 [-1.1191154   1.9560789  -0.3264995  -1.34267579]
First Test: All tests passed!

Thanks. My bug was that I used A_prev_pad instead of a_prev_pad for a_slice_prev, and I did not initialize Z to be 4 dimensional. I also used “f” for num_filters in the for loop instead of the “n_” parameter. Thanks for the help!

I am getting following error in ‘conv_single_step()’. Kindly suggest how to solve?
‘AssertionError: You must cast the output to float’

Did you follow this instruction from the code comments?
image