C4 W1 A1 a_slice_prev.shape changes late in nested for loop


As far as I can tell my code doesn’t have any gaping holes and the shapes of all the arrays are correct based on previous comments.

However, as my code executes and the nested for loop for h,w and c does it’s thing, the shape of a_slice_prev changes from 3,3,4 to 3,0,4 after 10 or so iterations.

Any help on fixing this issue would be hugely appreciated!

(3,0,4) sounds wrong.
There is probably an error in your start or end values for the horizontal and vertical dimensions.

Tip: The start values should include multiplication by “stride”.
The end values differ from the start values by adding ‘f’.

1 Like

Right! The key thing to remember in this chunk of code is that the loops are over the output space, not the input space. As Tom describes, you compute the start and end position in the input space based on the loop variable (h or w in the output space) and then taking into account the stride and the filter size. The symptom you show means that you are running off the end of the input array in the w dimension. Note that the test case here is not typical of actual problems in that the nH_{prev} and nW_{prev} values are not symmetrical. The nW_{prev} is bigger. So perhaps you have to h and w reversed or something of that nature.

As one way to debug, try adding some print statements to show the various shapes that you get. For comparison, here’s what I get for the one test test for conv_block with some print statements as I am suggesting:

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 =
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]

Thanks. My for loop is on the output value and my start values include multiplication by stride. My end values are the same +f.

The shape of the arrays also match Paul’s comment above. I don’t suppose I could direct message either of you my code to take a look where I’m going wrong?

Send me your code via a private message.

1 Like