Z Mean incorrect

I’ve read through the forum and I think my dimensions and loops are correct but I’m getting the following error:

shape of Z = (2, 3, 4, 8)
shape of A_prev_pad = (2, 7, 9, 4)
number of test images = 2
n_H = 3
n_W = 4
n_C = 8
n_H = 3
n_W = 4
n_C = 8
shape of Z = (2, 3, 4, 8)
Z’s mean =
0.3546941001261432
Z[0,2,1] =
[ 1.67079542 0.29511197 -2.15347143 6.51296288 1.1659603 0.73753314
1.54560046 4.69041001]
cache_conv[0][1][2][3] =
[-1.1191154 1.9560789 -0.3264995 -1.34267579]
First Test: Z’s mean is incorrect. Expected: 0.5511276474566768
Your output: 0.3546941001261432

First Test: Z[0,2,1] is incorrect. Expected: [-2.17796037, 8.07171329, -0.5772704, 3.36286738, 4.48113645, -2.89198428, 10.99288867, 3.03171932]
Your output: [ 1.67079542 0.29511197 -2.15347143 6.51296288 1.1659603 0.73753314
1.54560046 4.69041001]

Any suggestions are greatly appreciated.

Please click my name and message your notebook as an attachment.

Notice that the first test there as a stride of 2. The most common thing that trips people up here is not implementing the stride correctly. The second test has stride = 1. Do you get the correct answers there?

The most common mistake is to ignore the stride, but it doesn’t look like that is what happened in your case. The other type of mistake is to use it in the “range” for the for loops over height and width, but that ends up skipping positions in the output space.

Thanks Paul, I’m not using the stride in the range for the loop. I’m just using it in the calculation of the starts and ends. I don’t get the correct answer for test 2, that gives me a dimension error in the width dimension:

ValueError Traceback (most recent call last)
in
15
16 conv_forward_test_1(z_mean, z_0_2_1, cache_0_1_2_3)
—> 17 conv_forward_test_2(conv_forward)

~/work/release/W1A1/public_tests.py in conv_forward_test_2(target)
85 b = np.random.randn(1, 1, 1, 8)
86
—> 87 Z, cache_conv = target(A_prev, W, b, {“pad” : 3, “stride”: 1})
88 Z_shape = Z.shape
89 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)
91 biases = b[:,:,:,c]
92 # print("shape of biases = ", np.shape(biases))
—> 93 Z[i, h, w, c] = conv_single_step(a_slice_prev, weights, biases)
94
95 # YOUR CODE ENDS HERE

in conv_single_step(a_slice_prev, W, b)
23 # Z = None
24 # YOUR CODE STARTS HERE
—> 25 Z = np.sum(np.multiply(a_slice_prev, W))+np.float(b)
26
27 # YOUR CODE ENDS HERE

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

That’s interesting. And you don’t get that dimension mismatch in the first test case? The first case has pad = 1, stride = 2 and the second one has pad = 3, stride = 1. Here are all my outputs for those two tests (including a few added prints):

stride 2 pad 1
New dimensions = 3 by 4
Shape Z = (2, 3, 4, 8)
Shape A_prev_pad = (2, 7, 9, 4)
Z[0,0,0,0] = -2.651123629553914
Z[1,2,3,7] = 0.4427056509973153
Z's mean =
 0.5511276474566768
Z[0,2,1] =
 [-2.17796037  8.07171329 -0.5772704   3.36286738  4.48113645 -2.89198428
 10.99288867  3.03171932]
cache_conv[0][1][2][3] =
 [-1.1191154   1.9560789  -0.3264995  -1.34267579]
First Test: All tests passed!
stride 1 pad 3
New dimensions = 9 by 11
Shape Z = (2, 9, 11, 8)
Shape A_prev_pad = (2, 11, 13, 4)
Z[0,0,0,0] = 1.4306973717089302
Z[1,8,10,7] = -0.6695027738712113
stride 2 pad 0
New dimensions = 2 by 3
Shape Z = (2, 2, 3, 8)
Shape A_prev_pad = (2, 5, 7, 4)
Z[0,0,0,0] = 8.430161780192094
Z[1,1,2,7] = -0.2674960203423288
stride 1 pad 6
New dimensions = 13 by 15
Shape Z = (2, 13, 15, 8)
Shape A_prev_pad = (2, 17, 19, 4)
Z[0,0,0,0] = 0.5619706599772282
Z[1,12,14,7] = -1.622674822605305
Second Test: All tests passed!

So you can see that the output shape for that test case is (2, 9, 11, 8). Notice that the height dimension is less than the width dimension. Are you sure you’re not reversing the roles of h and w? Remember that h stands for height not horizontal. :nerd_face: Or if that’s not it, maybe check your padding implementation and double check that your output shape agrees with what I show above.

1 Like

@Tom_Allen Initialization of a_prev_pad is incorrect inside the for loop. n_H_prev and n_W_prev are dimensions of A_prev that doesn’t include padding. When padding, pad number of zeros are added on each side of the original array.

@balaji.ambresh yes that was it!!! Thank you so much for getting me over this hump.