Error for C4, week 1, exercise 3


I’m receiving the following error. But I don’t know what’s wrong with my code.

IndexError Traceback (most recent call last)
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]
11 cache_0_1_2_3 = cache_conv[0][1][2][3]
12 print(“Z’s mean =\n”, z_mean)

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

I took a look into other posts with similar issues and my code doesn’t have those problems.

I have no idea where I went wrong. Can you please help?


I think your code for “loop” seems to be OK. (Posting code is not recommended. Please remove them.)

The problem is;

—> 10 z_0_2_1 = Z[0, 2, 1]
IndexError: index 1 is out of bounds for axis 2 with size 1

This said that your Z is not created correctly.
And, Z is created from m, n_H, n_W and n_C. Most likely your calculation for n_H and n_W is not correct. Those are dimensions for output.

Please double check equations to calculate n_H and n_W.

Thank you @anon57530071 . I followed the formula in the descriptions for n_H and n_W. For example following is the n_H formula.
n_H = int((n_H_prev-f+2*pad)/stride)+1

That’s axis=1. :slight_smile: The problem is axis=2, n_W.
What is your Z.shape ?

Sorry @anon57530071. I sent that one through because formula for n_H and n_W are similar.
n_W = int((n_W_prev-f2pad)/stride)+1
Z= np.zeros((m, n_H, n_W, n_C), dtype= float)
Z.shape for the example is =(2, 3, 1, 8)

I think there are some truncation by this system, but, that’s mostly for “asterisk”.
So, I think your n_W is using * instead of +.

Sorry @anon57530071 . There was a typo. Problem solved. thanks.