Exercise 3 conv_forward() error

Please help me resolve the error below. Been stuck for more than a week. I have gone over my calculation/code of n_H several times.

Z's mean =
 0.35921433084831406
Z[0,2,1] =
 [ 3.45398536 10.75599271  1.22365951  0.67852709  0.9981441  10.58016142
  6.19458048 -2.97289736]
cache_conv[0][1][2][3] =
 [-1.1191154   1.9560789  -0.3264995  -1.34267579]
First Test: Z's mean is incorrect. Expected: 0.5511276474566768 
Your output: 0.35921433084831406 . Make sure you include stride in your calculation

First Test: Z[0,2,1] is incorrect. Expected: [-2.17796037, 8.07171329, -0.5772704, 3.36286738, 4.48113645, -2.89198428, 10.99288867, 3.03171932] 
Your output: [ 3.45398536 10.75599271  1.22365951  0.67852709  0.9981441  10.58016142
  6.19458048 -2.97289736] Make sure you include stride in your calculation


---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-10-7e580406a9e8> in <module>
     15 
     16 conv_forward_test_1(z_mean, z_0_2_1, cache_0_1_2_3)
---> 17 conv_forward_test_2(conv_forward)

~/work/release/W1A1/public_tests.py in conv_forward_test_2(target)
     88     Z_shape = Z.shape
     89     assert Z_shape[0] == A_prev.shape[0], f"m is wrong. Current: {Z_shape[0]}.  Expected: {A_prev.shape[0]}"
---> 90     assert Z_shape[1] == 9, f"n_H is wrong. Current: {Z_shape[1]}.  Expected: 9"
     91     assert Z_shape[2] == 11, f"n_W is wrong. Current: {Z_shape[2]}.  Expected: 11"
     92     assert Z_shape[3] == W.shape[3], f"n_C is wrong. Current: {Z_shape[3]}.  Expected: {W.shape[3]}"

AssertionError: n_H is wrong. Current: 5.  Expected: 9

The equation is here in the notebook:

When you convert it to python code, you have to be careful where you put the parenthesis.
And wrap the whole calculation inside “int(...)”

Yes, I am quite sure of calculation of n_H in my code and I have wrapped the whole calculation in int().

I am kindly asking to share my code with you?? I think the cause of the error is not my implementation of n_H but I cannot figure it out.

My first suggestion with any problem in conv_forward is to have a look at this post from a couple of years ago. It gives a very clear description of all the complexities in this function in words.

2 Likes

One other more specific thing to note:

The assertion that is throwing is looking at the dimensions of your Z array. So one thing to consider is that your n_H is correct, but the problem is in how you are initializing Z.

I put a bunch of print statements in my code to see what is going on and here’s what I get when I run the test cell for conv_forward:

stride 2 pad 1
New dimensions = 3 by 4
Shape Z = (2, 3, 4, 8)
Shape A_prev = (2, 5, 7, 4)
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 = (2, 5, 7, 4)
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 = (2, 5, 7, 4)
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 = (2, 5, 7, 4)
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 your code passed on the first test, which had pad = 1, but failed on the second test which has pad = 3. Based on that I wouldn’t be so confident that your n_H computation is actually correct.

I had already gone through the post you are recommending countless times but I decided to go through it one more time and there it was! When retrieving the stride and pad from hparameters dictionary, I mistakenly provided the same key (“stride”) in both cases (copy and paste). I have corrected it and my code now runs without errors. Thank you.

3 Likes

That’s great news that you finally were able to find the solution. Onward! :nerd_face:

1 Like