Course 4 Week 1 Exercise 3 - conv_forward Error: Wrong output for variable in position 0

Your vert_start and horiz_start calculations are way too complicated. You only need the h or w value and the stride, right? The f value and the pad don’t affect that.

1 Like

Because I am iterating over n_H and n_W, which are output volume’s dimension. How to make connections to the input volume through the output index ‘h’ and ‘w’? If I simply use ‘vert_start = h * stride + f’, this creates another error.

1 Like

Your mistake is that vert_start does not depend on f, right? vert_end does. The first value of vert_start in the for loop needs to be 0, right?

1 Like

@paulinpaloalto Simply ‘vert_start = h * stride, vert_end = vert_start + f’ seems to give the correct indexes. But now I got a different error message below. This first example has a wrong Z value, and the 2nd example still gets a matrix mismatch.

m:2, n_H:3, n_W:4, n_C:8
b: [[[[-1.39881282  0.08176782 -0.45994283  0.64435367  0.37167029
     1.85300949  0.14225137  0.51350548]]]]
A_prev.shape: (2, 5, 7, 4) a_prev_pad: (5, 7, 4)
A_prev.shape: (2, 5, 7, 4) a_prev_pad: (5, 7, 4)
Z's mean =
 1.0644642603114827
Z[0,2,1] =
 [-5.42280893  1.88549165 -4.09974126 -3.48941271 -5.34564475  3.56732833
  1.77640635 -2.33586583]
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: 1.0644642603114827 

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: [-5.42280893  1.88549165 -4.09974126 -3.48941271 -5.34564475  3.56732833
  1.77640635 -2.33586583] 

m:2, n_H:9, n_W:11, n_C:8
b: [[[[ 1.43069737 -1.04137439 -0.81983341  0.13594038  0.9895728
     1.3391483   0.68062695 -0.66950277]]]]
A_prev.shape: (2, 5, 7, 4) a_prev_pad: (5, 7, 4)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-19-7e580406a9e8> in <module>
     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]}"

<ipython-input-18-620cd6ff07ea> in conv_forward(A_prev, W, b, hparameters)
     86                     bias = b[0, 0, 0, c]
     87                     #print(f'a_slice_prev.shape: {a_slice_prev.shape} weights: {weights.shape} bias:{bias}')
---> 88                     Z[i, h, w, c] = conv_single_step(a_slice_prev, weights, bias)
     89                     pass
     90     # YOUR CODE ENDS HERE

<ipython-input-16-70a66b9715f1> in conv_single_step(a_slice_prev, W, b)
     24     # YOUR CODE STARTS HERE
     25     #print(f'a_slice_prev.shape: {a_slice_prev.shape}, W.shape: {W.shape}')
---> 26     s = a_slice_prev * W
     27     Z = np.sum(s)
     28     Z += np.asscalar(b)

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

Are you sure that you did not include the stride in the loop ranges? If so, that would be a mistake. The looping happens in the output space and you must touch every position. But the striding happens in the input space, as determined by the vert_start and horiz_start values.

1 Like

@paulinpaloalto If you look at this log, it seems the indexes are right:

m:2, n_H:3, n_W:4, n_C:8
vstart: 0 vend:3
  hstart: 0 hend: 3
  hstart: 2 hend: 5
  hstart: 4 hend: 7
  hstart: 6 hend: 9
vstart: 2 vend:5
  hstart: 0 hend: 3
  hstart: 2 hend: 5
  hstart: 4 hend: 7
  hstart: 6 hend: 9
vstart: 4 vend:7
  hstart: 0 hend: 3
  hstart: 2 hend: 5
  hstart: 4 hend: 7
  hstart: 6 hend: 9
vstart: 0 vend:3
  hstart: 0 hend: 3
  hstart: 2 hend: 5
  hstart: 4 hend: 7
  hstart: 6 hend: 9
vstart: 2 vend:5
  hstart: 0 hend: 3
  hstart: 2 hend: 5
  hstart: 4 hend: 7
  hstart: 6 hend: 9
vstart: 4 vend:7
  hstart: 0 hend: 3
  hstart: 2 hend: 5
  hstart: 4 hend: 7
  hstart: 6 hend: 9
Z's mean =
 1.0644642603114827
Z[0,2,1] =
 [-5.42280893  1.88549165 -4.09974126 -3.48941271 -5.34564475  3.56732833
  1.77640635 -2.33586583]
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: 1.0644642603114827 

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: [-5.42280893  1.88549165 -4.09974126 -3.48941271 -5.34564475  3.56732833
  1.77640635 -2.33586583] 

In the assignment instruction, there is this tip:

Use array slicing (e.g.`varname[0:1,:,3:5]` ) for the following variables:
`a_prev_pad` ,`W` , `b`

I didn’t find where I can make use of this tip. Is that the reason?

1 Like

Here is my output from that test case:

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!

So your vstart:vend and hstart:hend values all look correct to me. You would apply that “slicing” hint to A_prev_pad. At least the hint about using the range syntax as I just did in that previous sentence. For W and b, you only need to select the appropriate output channel value and take all of the other dimensions (just use “:” as the range to say “take everything”).

1 Like

It turns out that I made a mistake by using A_prev, instead of A_prev_pad. Thanks.

2 Likes

I was stuck until I saw the above comment I had stupidly copied A_prev into a_prev_pad instead of using A_prev_pad, doing nested it loops is very hard glad there are frameworks for this! :grin:

1 Like

@Scott_Smith great hint to use range( , , ) !

1 Like

Hi all,

I was able to pass al tests on exercise 3, but I’m failing on exercise 4. I follow the same logic as I did in exercise 3, so i’m really confused as to why tests are not passing. Here is my loop

{moderator edit - solution code removed}

Here is the error I get

CASE 1:

mode = max
A.shape = (2, 3, 3, 3)
A[1, 1] =
[[1.96710175 1.96710175 1.96710175]
[1.96710175 1.96710175 1.96710175]
[1.62765075 1.62765075 1.62765075]]
mode = average
A.shape = (2, 3, 3, 3)
A[1, 1] =
[[0.04398565 0.04398565 0.04398565]
[0.01118852 0.01118852 0.01118852]
[0.0233857 0.0233857 0.0233857 ]]

AssertionError Traceback (most recent call last)
in
14 print(“A[1, 1] =\n”, A[1, 1])
15
—> 16 pool_forward_test_1(pool_forward)
17
18 # Case 2: stride of 2

~/work/release/W1A1/public_tests.py in pool_forward_test_1(target)
152 [0.31515939, 0.84616065, 0.07734007],
153 [0.69803203, 0.84616065, 1.2245077 ],
→ 154 [0.69803203, 1.12141771, 1.2245077 ]])), “Wrong value for A[1, 1]”
155
156 assert np.allclose(cache[0][1, 2], np.array([[ 0.16938243, 0.74055645, -0.9537006 ],

AssertionError: Wrong value for A[1, 1]

1 Like

The problem is that “pooling” layers work “channelwise”, meaning they handle each channel individually and give the same number of output channels as input channels. Whereas “conv” layers work across the input channels and give you one output channel for each filter.

Your logic does not handle the channels correctly for a pooling layer.

The striding logic for the loops over h and w is the same in both cases, but the channels are handled very differently.

1 Like

Thank you! That clarified it and it’s all good now!

1 Like