Trouble slicing conv_forward

Hello, i’m having trouble with the conv_forward function, Week 1, Exercise 3.

From what i’ve found by printing shapes, it seems that, on the final positioning of the filter, the slicing is performed in an unexpected way:

The vert_start, vert_end, horiz_start, horiz_end indices are:
(6, 9, 0, 3), so the slice should be 3x3. And in fact it is 3x3 for the majority of the loop, but on the last position a_slice_prev turns out to be 1x3.

I’ve spent a long time debugging and I seem to be lost. Here is the error message:
Many thanks in advance!

IndexError Traceback (most recent call last)
in
6 “stride”: 2}
7
----> 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)
131
→ 132 Z[i, h, w, c] = conv_single_step(a_slice_prev, weights, biases)
133
134 # YOUR CODE ENDS HERE

IndexError: index 3 is out of bounds for axis 1 with size 3

Are you including the “stride” parameter when you compute vert_start and horiz_start?
If you aren’t, that can give you the wrong sizes of slice.

The start and end indices should change on every iteration through the for-loop. So they won’t be those fixed values you posted.

1 Like

Yes, the stride is included, and the limits update on every iteration. The ones I posted correspond to the problematic iteration of the loop, to show that the slice should be 3x3. But instead it is 1x3

Can you give some reason why you feel that the slice is the wrong size for that combination? I don’t see that in the error message you posted.

The shape of a_slice_prev should always be (3,3,4).

I put this line of code inside the inner for-loop to inspect the values:
print(“starts, ends, slice :”,vert_start,vert_end,horiz_start,horiz_end,np.shape(a_slice_prev))`

… and got this result:
starts, ends, slice : 6 9 0 3 (3, 3, 4)
starts, ends, slice : 6 9 0 3 (3, 3, 4)
starts, ends, slice : 6 9 0 3 (3, 3, 4)
starts, ends, slice : 6 9 0 3 (3, 3, 4)
starts, ends, slice : 6 9 0 3 (3, 3, 4)
starts, ends, slice : 6 9 0 3 (3, 3, 4)
starts, ends, slice : 6 9 0 3 (3, 3, 4)
starts, ends, slice : 6 9 0 3 (3, 3, 4)

Note that’s for the second test of the function - the first test doesn’t use 6 9 0 3.

My output is as follows:

starts, ends, slice : 0 3 0 3 (3, 3, 4)
starts, ends, slice : 0 3 0 3 (3, 3, 4)
starts, ends, slice : 0 3 0 3 (3, 3, 4)
starts, ends, slice : 0 3 0 3 (3, 3, 4)
starts, ends, slice : 0 3 0 3 (3, 3, 4)
starts, ends, slice : 0 3 0 3 (3, 3, 4)
starts, ends, slice : 0 3 0 3 (3, 3, 4)
starts, ends, slice : 0 3 0 3 (3, 3, 4)
starts, ends, slice : 0 3 2 5 (3, 3, 4)
starts, ends, slice : 0 3 2 5 (3, 3, 4)
starts, ends, slice : 0 3 2 5 (3, 3, 4)
starts, ends, slice : 0 3 2 5 (3, 3, 4)
starts, ends, slice : 0 3 2 5 (3, 3, 4)
starts, ends, slice : 0 3 2 5 (3, 3, 4)
starts, ends, slice : 0 3 2 5 (3, 3, 4)
starts, ends, slice : 0 3 2 5 (3, 3, 4)
starts, ends, slice : 0 3 4 7 (3, 3, 4)
starts, ends, slice : 0 3 4 7 (3, 3, 4)
starts, ends, slice : 0 3 4 7 (3, 3, 4)
starts, ends, slice : 0 3 4 7 (3, 3, 4)
starts, ends, slice : 0 3 4 7 (3, 3, 4)
starts, ends, slice : 0 3 4 7 (3, 3, 4)
starts, ends, slice : 0 3 4 7 (3, 3, 4)
starts, ends, slice : 0 3 4 7 (3, 3, 4)
starts, ends, slice : 2 5 0 3 (3, 3, 4)
starts, ends, slice : 2 5 0 3 (3, 3, 4)
starts, ends, slice : 2 5 0 3 (3, 3, 4)
starts, ends, slice : 2 5 0 3 (3, 3, 4)
starts, ends, slice : 2 5 0 3 (3, 3, 4)
starts, ends, slice : 2 5 0 3 (3, 3, 4)
starts, ends, slice : 2 5 0 3 (3, 3, 4)
starts, ends, slice : 2 5 0 3 (3, 3, 4)
starts, ends, slice : 2 5 2 5 (3, 3, 4)
starts, ends, slice : 2 5 2 5 (3, 3, 4)
starts, ends, slice : 2 5 2 5 (3, 3, 4)
starts, ends, slice : 2 5 2 5 (3, 3, 4)
starts, ends, slice : 2 5 2 5 (3, 3, 4)
starts, ends, slice : 2 5 2 5 (3, 3, 4)
starts, ends, slice : 2 5 2 5 (3, 3, 4)
starts, ends, slice : 2 5 2 5 (3, 3, 4)
starts, ends, slice : 2 5 4 7 (3, 3, 4)
starts, ends, slice : 2 5 4 7 (3, 3, 4)
starts, ends, slice : 2 5 4 7 (3, 3, 4)
starts, ends, slice : 2 5 4 7 (3, 3, 4)
starts, ends, slice : 2 5 4 7 (3, 3, 4)
starts, ends, slice : 2 5 4 7 (3, 3, 4)
starts, ends, slice : 2 5 4 7 (3, 3, 4)
starts, ends, slice : 2 5 4 7 (3, 3, 4)
starts, ends, slice : 4 7 0 3 (3, 3, 4)
starts, ends, slice : 4 7 0 3 (3, 3, 4)
starts, ends, slice : 4 7 0 3 (3, 3, 4)
starts, ends, slice : 4 7 0 3 (3, 3, 4)
starts, ends, slice : 4 7 0 3 (3, 3, 4)
starts, ends, slice : 4 7 0 3 (3, 3, 4)
starts, ends, slice : 4 7 0 3 (3, 3, 4)
starts, ends, slice : 4 7 0 3 (3, 3, 4)
starts, ends, slice : 4 7 2 5 (3, 3, 4)
starts, ends, slice : 4 7 2 5 (3, 3, 4)
starts, ends, slice : 4 7 2 5 (3, 3, 4)
starts, ends, slice : 4 7 2 5 (3, 3, 4)
starts, ends, slice : 4 7 2 5 (3, 3, 4)
starts, ends, slice : 4 7 2 5 (3, 3, 4)
starts, ends, slice : 4 7 2 5 (3, 3, 4)
starts, ends, slice : 4 7 2 5 (3, 3, 4)
starts, ends, slice : 4 7 4 7 (3, 3, 4)
starts, ends, slice : 4 7 4 7 (3, 3, 4)
starts, ends, slice : 4 7 4 7 (3, 3, 4)
starts, ends, slice : 4 7 4 7 (3, 3, 4)
starts, ends, slice : 4 7 4 7 (3, 3, 4)
starts, ends, slice : 4 7 4 7 (3, 3, 4)
starts, ends, slice : 4 7 4 7 (3, 3, 4)
starts, ends, slice : 4 7 4 7 (3, 3, 4)
starts, ends, slice : 6 9 0 3 (1, 3, 4)

As you can see, in the last case the shape of the slice is (1, 3, 4) instead of the expected (3, 3, 4), and that’s why the error is being raised. But I don’t understand what might be causing this.

Also I only have one test for this function, called “conv_forward_test”

Thank you very much!

1 Like