Help with indices/slices in C4 W1 A1 E3

Having spent quite some time in debugging, I’m not sure what the error log means, and would appreciate pointers to where exactly the code went wrong.

That error is referring to the Z array on the LHS of that assignment. The value you have passed for c is 4, but that axis only has 4 elements so 3 is the highest index that would be valid.

So either your Z array is initialized incorrectly to the wrong size in that dimension or your looping logic for the output channel index is incorrect. So the first question is answer is “what shape is your Z value?”

Well, I put a np.shape query for Z before Z[i, h, w, c] in the innermost loop. Can you please help me figure out what went wrong?

Here is my output from that test cell with some added prints:

stride 2 pad 1
New dimensions = 3 by 4
Shape Z = (2, 3, 4, 8)
Shape A_prev_pad = (2, 7, 9, 4)
Z[0,0,0,0] = -2.651123629553914
Z[1,2,3,7] = 0.4427056509973153
Z's mean =
 0.5511276474566768
Z[0,2,1] =
 [-2.17796037  8.07171329 -0.5772704   3.36286738  4.48113645 -2.89198428
 10.99288867  3.03171932]
cache_conv[0][1][2][3] =
 [-1.1191154   1.9560789  -0.3264995  -1.34267579]
First Test: All tests passed!
stride 1 pad 3
New dimensions = 9 by 11
Shape Z = (2, 9, 11, 8)
Shape A_prev_pad = (2, 11, 13, 4)
Z[0,0,0,0] = 1.4306973717089302
Z[1,8,10,7] = -0.6695027738712113
stride 2 pad 0
New dimensions = 2 by 3
Shape Z = (2, 2, 3, 8)
Shape A_prev_pad = (2, 5, 7, 4)
Z[0,0,0,0] = 8.430161780192094
Z[1,1,2,7] = -0.2674960203423288
stride 1 pad 6
New dimensions = 13 by 15
Shape Z = (2, 13, 15, 8)
Shape A_prev_pad = (2, 17, 19, 4)
Z[0,0,0,0] = 0.5619706599772282
Z[1,12,14,7] = -1.622674822605305
Second Test: All tests passed!

The shape that I compute for Z in that very first test is 2 x 3 x 4 x 8. It looks like you used the shape of A_prev. Part of what you need to do is to compute the output size from the input size, rather than just using the input size. Prof Ng discussed this in the lectures and it is shown in the instructions in the notebook as well. Here’s the formula for computing the h and w values:

n_{out} = \displaystyle \lfloor \frac {n_{in} + 2p - f}{s} \rfloor + 1

The number of output channels is determined by the last dimension of the W and b values: that is the number of filters and that determines the number of output channels (one per filter).

Thanks @paulinpaloalto! In Z initialisation with zeros, I had used A_prev for dimensions. Rest of the code was fine.

That’s good news! So you had the hard part figured out already. :nerd_face: Thanks for confirming.