Week 4 conv_forward()

I’m stuck with this error. Either W or a_slice_prev have the wrong shapes (or both).

ValueError                                Traceback (most recent call last)
<ipython-input-24-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-23-66e27fad15e2> in conv_forward(A_prev, W, b, hparameters)
     84                     weights = W[:,:,:,n_C-1]
     85                     biases = b[:, :, :, n_C-1]
---> 86                     Z[i, h, w, c] = conv_single_step(a_slice_prev, weights, biases)
     87 
     88     # YOUR CODE ENDS HERE

<ipython-input-4-74f3183eacc3> 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 + float(b)

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

Code:

{moderator edit - solution code removed}

You are not using the h and w values that are your “loop indices” in the actual logic of the loops. That is a mistake. To see why, it is important to understand that the loops here are over the output space, right? Then you compute for each output position where that comes from in the input space. Notice that your vert_start value will be the same in every iteration of the loop, right? So how can that work? You need to be “stepping” through the input space at a different position (using the stride) for each iteration of the loop.

Note that you are making the same type of mistake on the channel dimension as well. You are ignoring the value of c when you select the channel for weights and biases.

Thanks! I was using the loops final range instead of the iterator in the code. Passed all tests with the fix.