Course 4 Week 1 Exercise 3 - conv_forward

I have been working on this exercise for a while and I still could not find the problem. I would be glad if someone could help to find the answer.

Here is the error that I got

ValueError Traceback (most recent call last)
6 “stride”: 2}
----> 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)
80 weights = W[:,:,:,c:c+1]
81 biases = b[0,0,0,c:c+1]
—> 82 Z[i, h, w, c] = conv_single_step(a_slice_prev, weights, biases)

in conv_single_step(a_slice_prev, W, b)
23 # Z = None
—> 25 s = a_slice_prev * W
26 Z = np.sum(s, axis=None)
27 Z = Z + float(b[0])

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

It looks like there is a problem with how you are “slicing” the input. Also specifying the index values on the W and b with c:c+1 as the last dimension gives you a 4D array. If you just use “c” as the last index, then you end up with a 3D array of dimension 3 x 3 x 4 for W, which is what you want.

Then the “slice” you take of A_prev_pad needs to be the same shape as that: 3 x 3 x 4. So how did you end up with 1 x 3 x 3 x 1? Are you including the “samples” dimension (dimension 0)?

Thanks for your help.

As you mentioned the problem was exactly with the way I sliced A_prev_pad and the dimensions of W and b.

a_slice_prev is a slice of A_prev_pad with indexes start with i, vert_start:vert_end, horiz_start:horiz_end, and all the filters.

I could pass the test with your help. Thanks