# DL4 -Week1,Assignment 1,Excercise3

ipython-input-47-e9a8fc127504> in conv_forward(A_prev, W, b, hparameters)
84 weights= W[:,:,:,c]
85 bias= b[:,:,:,c].reshape(1,1,1)
—> 86 Z[i, h, w, c]=conv_single_step(a_slice_prev, weights, bias)
87 print(Z[i, h, w, c])
88 # YOUR CODE ENDS HERE

IndexError: index 4 is out of bounds for axis 3 with size 4
My question here is why is it saying out of bounds .
Is my code is correct?I am not able to understand the mistake.
Need Help!!!

Either a_slice_prev or weights or bias are the wrong shape.

I don’t think you need to reshape the bias.

Consider whether your code for “bias = …” is giving the correct value.

In fact this I got as shape of a_slice_prev and value of Z[i,h,w,c] .It had gone well with index 3 but it is not oroceeding further.I think I have a mistake in logic.
(3, 3, 4)
-2.651123629553914
(3, 3, 4)
-0.37849176551833935
(3, 3, 4)
-1.970549285291896
(3, 3, 4)
-1.9623529930298282
(3, 3, 4)
Can you please give me a hint?

I added a bunch of print statements to my conv_forward logic and here’s what I see when I run the test case for that:

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!
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
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
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!


Notice that the shape of Z is wider than it is tall. Maybe you switched the meaning of h and w. Remember that h is for “height” not “horizontal”.

Also it would be good to print the shape of your Z value, since that is what the error is complaining about.

I don’t think so my final Z value is the problem because c in n_C is not completing its 5th iteration. That’s the problem here .
I am not getting why its not completing its iteration.
That’s why if there is any logical mistakes
h and w is fine here

I’m not talking about the value of Z, but the shape. Please add this statement early in your conv_forward:

print(f"Z.shape = {Z.shape}")

Note that there are 8 output channels, so the highest c value in your loop will be 7, right? So if your Z is the correct shape, then why does it fail when c is 4 or 5 (whichever you mean by “fifth iteration”)?

sorry ,I got Z.shape = (2, 5, 7, 4)
(3, 3, 4)
-2.651123629553914
Z.shape = (2, 5, 7, 4)
(3, 3, 4)
-0.37849176551833935
Z.shape = (2, 5, 7, 4)
(3, 3, 4)
-1.970549285291896
Z.shape = (2, 5, 7, 4)
(3, 3, 4)
-1.9623529930298282
Z.shape = (2, 5, 7, 4)
(3, 3, 4)
Is my Z shape is correct?
Now I am getting little confused.

I showed the correct shape in my earlier post:

stride 2 pad 1
New dimensions = 3 by 4
Shape Z = (2, 3, 4, 8)
Shape A_prev_pad = (2, 7, 9, 4)


So it looks like your h and w dimensions are wrong as well as the c dimension. For c you’ve used the input channels, but it should be the output channels (8 in this case).

For h and w, the formula is:

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

for h in range(n_H):
{moderator edit: code removed}


this is basically I have done

n_H=3
n_W=4
-2.651123629553914
Z.shape = (2, 5, 7, 4)
-0.37849176551833935
Z.shape = (2, 5, 7, 4)
-1.970549285291896
Z.shape = (2, 5, 7, 4)
-1.9623529930298282
Z.shape = (2, 5, 7, 4)


I don’t know why Z.shape is not showing (2,3,4,8)

Where is the shape of Z determined? You have to predefine it before you start the nested “for” loops, right?