Why I don't get an "index out of bounds" error in week 1 assignment

So I need to ask for my clarity that why I dont get an error in forward pass function in week 1 assignment when I think I should get an error message.

For simplicity assume stride = 1. Even then when calculating vert_start and vert_end for example, I am basically looping over all n_H values with a for loop. This means that when vert_start > n_H-f, then vert_end is basically greater than n_H. And I believe I shoud get an index out of bounds error in a_slice_prev. But it runs just fine.

Strangely, when I took care of this in for loop itself like ;
for h in range(0, n_H-f, stride)

then the output results were different.

Can anybody please help?

You can’t use stride in the for-loop iterator, because you’d be skipping over many of the output values that you need to create.

Can you please explain as I cant see the point. Like what is the difference between using stride in for loop vs in vert_start? Aren’t both ways essentially the same;

for h in range(0, n_H-f, stride)
vert_start = h
vert_end = h+f

vs

for h in range(n_H)
vert_start = h*stride
vert_end = vert_start + f

Also, what about no error even when vert_end is greater than n_H?

In your first example, if stride is 2, then ‘h’ will only take on even values. All the odd values will be skipped.
Your second example is correct - because all ‘h’ values will be used.

1 Like

Please have a look and let me know that whether the following is incorrect?

if n _H =n_W= 10, f = 3, s = 2

then 1st iteration: vert_start = 0, vert_end = 3
then 2nd iteration: vert_start = 2, vert_end = 5
then 3rd iteration: vert_start = 4, vert_end = 7
then 4th iteration: vert_start = 6, vert_end = 9
then 5th iteration: vert_start = 8, vert_end = 11
then 6th iteration: vert_start = 10, vert_end = 13
then 7th iteration: vert_start = 12, vert_end = 15
then 8th iteration: vert_start = 14, vert_end = 17
then 9th iteration: vert_start = 16, vert_end = 19
then 10th iteration: vert_start = 18, vert_end = 21

The way I see it both chunks of code will produce identical vert_start and vert_end till 4th iteration. After that former will stop (as it should since vert_end greater than 9 should throw an index out of bounds error), while the later will keep going on.

So, it seems like,

  1. There is no issue of skipping any odd values.
  2. The later one should throw out an error which it isn’t.

It doesn’t throw the error because the limit on your “for” range stops it from going off the end in your first sample code. But that code is incorrect for the reasons that Tom explained:

The loops here are over the output space, not the input space. We must touch every position in the output space and not skip any. Then for each output position, we have to compute where that comes from in the input space. That is where the stride comes in. It is a fundamental mistake to use the stride in the range of the for loop.

The second set of code should work. Of course the horizontal and vertical logic should look the same.

1 Like

Many Thanks!! I was just looping it over input grid instead of output grid in my head.