Course 4. Week 1, Question 3: conv_forward

I’ve been stuck on this question for days and scoured all the questions asked previously, but so far hasn’t been able to find a solution

Here’s the error output:

ValueError                                Traceback (most recent call last)
<ipython-input-34-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-33-cb92f6979a75> in conv_forward(A_prev, W, b, hparameters)
    102                     biases = b[c]
    103                     print("biases", np.shape(biases))
--> 104                     Z[i, h, w, c] = conv_single_step(a_slice_prev, weights, biases)
    105 
    106     # YOUR CODE ENDS HERE

<ipython-input-4-290fd28ffbde> in conv_single_step(a_slice_prev, W, b)
     23     # Z = None
     24     # YOUR CODE STARTS HERE
---> 25     s = np.multiply(W, a_slice_prev)
     26     Z = np.sum(s)
     27     Z = Z + np.float64(b)

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

Looking at previous posts, I’ve spent hours checking that that

  1. I’ve checked h and w is multiplied by stride
  2. a_slice_prev is a function of a_prev_pad, slicing index of vert, horiz, : .
  3. Weights and biases are by taking index c.
  4. h in range is a function of vert, w in range is a function of horiz.

I’ve spent hours looking/considering the shape of the output volume Z, A_prev_pad but coming up blank. Any help is greatly appreciated.

The problem is not with how you slice a_slice_prev, but that the shape of W is incorrect. So how did that happen? I would start by tracking backwards through your conv_forward logic to figure out why the weights value is incorrect.

This is a study in interpreting error messages: the order of the dimensions that they state in the error message matters. They are stated in the same order as the operands on the operation that “throws”. Any time you are spending hours on a problem and not finding the answer, it’s worth taking a step back and considering if the problem is that you are looking in the wrong place.

Actually you can see that your slicing of the bias term is incorrect in the exception trace that you show above. The channels dimension is the last dimension of b and W, right? You’re slicing as if it is the first dimension. If you do the same thing w.r.t. W, that will not end well. In fact, it will give exactly the error that you are seeing.

Thanks. Yes, this was the hint I missed. I wasn’t too sure on how to interpret the error message, and your explanation helped.