Problem With Indexes - Week 1 Assigment 1 Exc 3

Hi guys,
can You check my code? I’m getting the following error:

IndexError                                Traceback (most recent call last)
<ipython-input-127-182241fd5e53> in <module>
      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])

<ipython-input-126-48e0a9556746> in conv_forward(A_prev, W, b, hparameters)
     81                     weights = W[:,:,:,c]
     82                     biases = b[:,:,:,c]
---> 83                     Z[i, w, h, c] = conv_single_step(a_slice_prev, weights, biases)
     84 
     85 

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

My code is:

{moderator edit - solution code removed}

I believe my implementation of inner loops is correct, as well as all the slicing, but somehow this produces an error, I was not able to track it yest.

Also, the shapes and *_start, *_end values are as follow throughout the loop until the error occurs:

a_prev_pad.shape, vert_start,vert_end, horiz_start, horiz_end, a_slice_prev.shape, weights.shape, biases.shape

(7, 9, 4) 0 3 0 3 (3, 3, 4) (3, 3, 4) (1, 1, 1)
(7, 9, 4) 0 3 0 3 (3, 3, 4) (3, 3, 4) (1, 1, 1)
(7, 9, 4) 0 3 0 3 (3, 3, 4) (3, 3, 4) (1, 1, 1)
(7, 9, 4) 0 3 0 3 (3, 3, 4) (3, 3, 4) (1, 1, 1)
(7, 9, 4) 0 3 0 3 (3, 3, 4) (3, 3, 4) (1, 1, 1)
(7, 9, 4) 0 3 0 3 (3, 3, 4) (3, 3, 4) (1, 1, 1)
(7, 9, 4) 0 3 0 3 (3, 3, 4) (3, 3, 4) (1, 1, 1)
(7, 9, 4) 0 3 0 3 (3, 3, 4) (3, 3, 4) (1, 1, 1)
(7, 9, 4) 0 3 2 5 (3, 3, 4) (3, 3, 4) (1, 1, 1)
(7, 9, 4) 0 3 2 5 (3, 3, 4) (3, 3, 4) (1, 1, 1)
(7, 9, 4) 0 3 2 5 (3, 3, 4) (3, 3, 4) (1, 1, 1)
(7, 9, 4) 0 3 2 5 (3, 3, 4) (3, 3, 4) (1, 1, 1)
(7, 9, 4) 0 3 2 5 (3, 3, 4) (3, 3, 4) (1, 1, 1)
(7, 9, 4) 0 3 2 5 (3, 3, 4) (3, 3, 4) (1, 1, 1)
(7, 9, 4) 0 3 2 5 (3, 3, 4) (3, 3, 4) (1, 1, 1)
(7, 9, 4) 0 3 2 5 (3, 3, 4) (3, 3, 4) (1, 1, 1)
(7, 9, 4) 0 3 4 7 (3, 3, 4) (3, 3, 4) (1, 1, 1)
(7, 9, 4) 0 3 4 7 (3, 3, 4) (3, 3, 4) (1, 1, 1)
(7, 9, 4) 0 3 4 7 (3, 3, 4) (3, 3, 4) (1, 1, 1)
(7, 9, 4) 0 3 4 7 (3, 3, 4) (3, 3, 4) (1, 1, 1)
(7, 9, 4) 0 3 4 7 (3, 3, 4) (3, 3, 4) (1, 1, 1)
(7, 9, 4) 0 3 4 7 (3, 3, 4) (3, 3, 4) (1, 1, 1)
(7, 9, 4) 0 3 4 7 (3, 3, 4) (3, 3, 4) (1, 1, 1)
(7, 9, 4) 0 3 4 7 (3, 3, 4) (3, 3, 4) (1, 1, 1)
(7, 9, 4) 0 3 6 9 (3, 3, 4) (3, 3, 4) (1, 1, 1)

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)
81 biases = b[:,:,:,c]
82 print(a_prev_pad.shape, vert_start,vert_end, horiz_start, horiz_end, a_slice_prev.shape, weights.shape, biases.shape)
—> 83 Z[i, w, h, c] = conv_single_step(a_slice_prev, weights, biases)
84
85

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

I agree that your loop logic and the handling of stride and all that looks correct. Note that the index values shown in the last step for the w dimension should be valid. Are you sure that your Z value is the correct shape? Here’s what I have for that test case:

Shape Z = (2, 3, 4, 8)

Yes, shape of Z is always (2, 3, 4, 8). Until the final case, where horiz_start, horiz_end are 6, 9. This is the case where loop breaks.

Okay, Im not sure what happened but I just rewrote the code and it worked…

Interesting. Well, one possible theory is that you had not re-executed the function after fixing it. Just typing new code and then calling the function again doesn’t do anything: it just runs the old code. You have to click “Shift-Enter” on the actual function cell to activate the changed code. Or do something like “Kernel → Restart and Clear Output” followed by “Cell → Run All”.