Course 4, Assignment 1 exercise 3 Cons_Forward

I’ve been scratching my head on this for several hours and not getting any traction. Does anyone have any hints or suggestions?

IndexError                                Traceback (most recent call last)
<ipython-input-13-7e580406a9e8> in <module>
      6                "stride": 2}
      7 
----> 8 Z, cache_conv = conv_forward(A_prev, W, b, hparameters)
      9 z_mean = np.mean(Z)
     10 z_0_2_1 = Z[0, 2, 1]

<ipython-input-12-949e3b29daba> in conv_forward(A_prev, W, b, hparameters)
     64     A_prev_pad = zero_pad(A_prev, pad)
     65     for i in range(m):
---> 66         a_prev_pad = A_prev_pad[i,n_H_prev,n_W_prev,n_C_prev]
     67         for h in range(n_H-f):
     68             vert_start = h*stride

IndexError: index 4 is out of bounds for axis 3 with size 4type or paste code here

The index values you are using for A_prev_pad are incorrect. You are using the size of h, w and c (prev) dimensions as the index values there. There are two things wrong with that approach:

  1. What you want to be doing there is selecting “all” of every dimension other than the “samples” dimension.
  2. Indexing in python is “zero based”, so if I have an array myArray with one dimension of size 4, then the first element is myArray[0] and the last element is myArray[3], right? If you say myArray[4], you’ll get an index error, because that is off the end of the array. But you can say myArray[0:4] or myArray[:] and you’ll get the entire array. That’s a hint for how to approach that line of code. :nerd_face:

Also note that your “range” on the for loop over h is incorrect. The loops are over the output space and you must touch each position of the output space.

Thank you Paul. I should have seen that.