Week 1 exercise 3 forward pass conv_forward


Although my problem is similar to a few I have found in the community I have not been able to resolve my problem. I have tried taking many of the steps that have been suggested in other similar posts but have not found luck. Here is my error.

IndexError Traceback (most recent call last)
6 “stride”: 2}
----> 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]

in conv_forward(A_prev, W, b, hparameters)
19 # Retrieve dimensions from A_prev’s shape (≈1 line)
20 # (m, n_H_prev, n_W_prev, n_C_prev) = None
—> 21 (m, n_H_prev, n_W_prev, n_C_prev) = (A_prev[0], A_prev[1], A_prev[2], A_prev[3])
22 # Retrieve dimensions from W’s shape (≈1 line)
23 # (f, f, n_C_prev, n_C) = None

IndexError: index 2 is out of bounds for axis 0 with size 2

I’ve implemented stride here and there and figured out the tricky parts of n_W/n_H I think as well as the vert_start deal with stride and f. When possible I have implemented previous functions as well.


Hi @zac_builta,

There are other things to look at for your problem.

  1. The assignment’s requirement, which is this line # Retrieve dimensions from A_prev’s shape (≈1 line). Note that it is asking for the shape of the variable A_prev

  2. From the error traceback, you are not doing the step to get the shape of A_prev.

  3. For how to do it, you may either google “how to get the shape of an array” or refer to some previous assignments because getting the shape is quitely commonly required.

Good luck.


Thank you for your comment.

As for A_prev’s shape I know that it is a 4 dimensional array whose dimensions are m, … etc. So I was trying to “retrieve” those dimensions by indexing into the A_prev array. I did the same thing with the ‘W’ array by indexing into this array in order to ‘retrieve’ each dimensions values. This may be wrong but it seemed like the best way to retrieve these values.

I might be doing that wrong. I know that in order to get a shape I could do np.shape(X) or X.shape. I could even just count them and hardcode them in: A_prev shape = 1x4, or W shape is 1x4. I was under the impression that I was trying to retrieve those values though for use later on in the cell.