Stuck in Exercise 3 - conv_forward

I cant find what’s wrong. The code runs throough some iterations but stops with an error at a certain point.

ValueError                                Traceback (most recent call last)
<ipython-input-73-7e580406a9e8> in <module>
      6                "stride": 2}
----> 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-72-5ab1f4c28a9c> in conv_forward(A_prev, W, b, hparameters)
    103                     weights = W[:,:,:,c]
    104                     biases = b[:,:,:,c]
--> 105                     Z[i, h, w, c] = conv_single_step(a_slice_prev,weights,biases)
    107     # YOUR CODE ENDS HERE

<ipython-input-68-f329765a0740> in conv_single_step(a_slice_prev, W, b)
     23     # Z = None
---> 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 (3,0,4) (3,3,4) 

That probably means you are handling the stride incorrectly. The key point to realize is that the stride happens only in the input space, but the loops here are over the output space. In the output space, we must touch every location and not skip any. The skipping caused by the stride happens in the input space. So for example it would be a mistake to include the stride in the loop limit.

Or perhaps you have reversed the roles of vertical and horizontal here. Note that h stands for height not horizontal, right?

This hint was a bit confusing for me. It lead me to remove stride from the calculation of vert_start and hor_start. But I need it here because this is the step width to itereate over a_prev_pad.

My original mistake was having a typo in the calculation of n_H and n_W. It took me some time to find out removing stride from the loop was a mistake too.

Eventually I figured out the correct solution. So thanks for your feedback.

Glad to hear that you found the solution and sorry if my hint was a misdirection. Of course you need to handle the stride in the body of the loop when you calculate vert_start and horiz_start, but one common error is to use the stride in the “range” expressions of the for loops on h and w which is what I was trying to warn against.