Course4, Week1, Exercise 3 - conv_forward, Error: operands could not be broadcast together with shapes (2,3,4) (3,3,4)

hello community, I am having this error, I have done a lot of debugging during the last two days and I can’t find the error, even I have been reading the post in the community but no answer at all. I am going to post my solution, I hope any moderator reads this as fast as possible and gives me an answer, feel free to delete the code once you read it.
btw, it could exist some print lines, the purpose of those lines is only for debugging.

<< code removed>>

ValueError Traceback (most recent call last)
11 print(“cache_conv[0][1][2][3] =\n”, cache_conv[0][1][2][3])
—> 13 conv_forward_test(conv_forward)

~/work/release/W1A1/ in conv_forward_test(target)
65 b = np.random.randn(1, 1, 1, 8)
—> 67 Z, cache_conv = target(A_prev, W, b, {“pad” : 3, “stride”: 1})
68 Z_shape = Z.shape
69 assert Z_shape[0] == A_prev.shape[0], f"m is wrong. Current: {Z_shape[0]}. Expected: {A_prev.shape[0]}"

in conv_forward(A_prev, W, b, hparameters)
105 print(a_slice_prev.shape)
106 #print(weights.shape)
→ 107 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,keepdims = True)
27 Z = np.squeeze(np.add(Z,b))[()]

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

Hi @LuckyLolo123 , from a quick glance, have a look at the way you are slicing (the calculation of a_slice_prev) in your code. That might explain why the shapes are not matching.

I hope this resolves the issue. I took the liberty the remove the code snippit.

thanks a lot, I did a mistake interchanging h with horizontal, and h is height, thanks again

1 Like

Hi @LuckyLolo123 , good to see you managed to solve it!

same here! had me scratching my head for ages until I came across this post