# Course 4 week 1 exercise 3: Make sure you include stride in your calculation

I am getting the below error:
(2, 7, 9, 4)
Z’s mean =
0.8837799868977424
Z[0,2,1] =
[10.76716029 6.05837302 2.14966486 10.34035645 9.51530963 -7.63337335
9.38081853 10.43136103]
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.8837799868977424 . 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: [10.76716029 6.05837302 2.14966486 10.34035645 9.51530963 -7.63337335
9.38081853 10.43136103] Make sure you include stride in your calculation

I am following the pseudocode in Course 4 Week 1 Exercise 3 - conv_forward Error: Wrong output for variable in position 0

Log statements:
A_prev shape 2 5 7 4
W shape 3 3 4 8
stride 2
CurrentActiation shape n_H 3 n_W 4
(2, 7, 9, 4)
After padding (2, 7, 9, 4)
Vertical index start end 0 3
Horizontal index start end 0 3
Z[0, 0, 0, 0] : 0.7241873303624353
Z[0, 0, 0, 1] : 2.307171162194142
Z[0, 0, 0, 2] : -7.906716521478254
Z[0, 0, 0, 3] : 0.3470750543542468
Z[0, 0, 0, 4] : -2.9066213975367274
Z[0, 0, 0, 5] : -1.9186764956632123
Z[0, 0, 0, 6] : -5.863902821556679
Z[0, 0, 0, 7] : 5.507995511689806
Horizontal index start end 2 5
Z[0, 0, 1, 0] : 0.9317234177885863
Z[0, 0, 1, 1] : 11.187300240410675
Z[0, 0, 1, 2] : -5.440624960986517
Z[0, 0, 1, 3] : 6.051901455253639
Z[0, 0, 1, 4] : -9.002540685114775
Z[0, 0, 1, 5] : 9.842356435587629
Z[0, 0, 1, 6] : 8.205274720495641
Z[0, 0, 1, 7] : -12.38173397865049
Horizontal index start end 4 7
Z[0, 0, 2, 0] : 4.878712949792419
Z[0, 0, 2, 1] : -5.232036872200803
Z[0, 0, 2, 2] : -3.899450318317641
Z[0, 0, 2, 3] : 2.5983499701045476
Z[0, 0, 2, 4] : -4.924545983869402
Z[0, 0, 2, 5] : 1.8722957212252815
Z[0, 0, 2, 6] : -2.713912694519111
Z[0, 0, 2, 7] : 3.7450691023544587
Horizontal index start end 6 9
Z[0, 0, 3, 0] : -1.4689599242762585
Z[0, 0, 3, 1] : -1.0959640434354934
Z[0, 0, 3, 2] : -2.927371430153968
Z[0, 0, 3, 3] : 7.8542852033166115
Z[0, 0, 3, 4] : -2.0563732773304833
Z[0, 0, 3, 5] : 1.3995967925071269
Z[0, 0, 3, 6] : 0.18075062019193364
Z[0, 0, 3, 7] : 2.151939424188021
Vertical index start end 2 5
Horizontal index start end 0 3
Z[0, 1, 0, 0] : 1.4380457566621803
Z[0, 1, 0, 1] : -2.268323841237544
Z[0, 1, 0, 2] : 11.113685698059546
Z[0, 1, 0, 3] : 3.390876521946942
Z[0, 1, 0, 4] : 6.741950026752265
Z[0, 1, 0, 5] : 2.868266722504116
Z[0, 1, 0, 6] : -0.6300550067059175
Z[0, 1, 0, 7] : 1.9891811545936577
Horizontal index start end 2 5
Z[0, 1, 1, 0] : -0.2959685916566741
Z[0, 1, 1, 1] : -6.758984698596256
Z[0, 1, 1, 2] : 7.22676156581304
Z[0, 1, 1, 3] : 5.368940033741598
Z[0, 1, 1, 4] : -8.800325500736939
Z[0, 1, 1, 5] : 11.43237343797162
Z[0, 1, 1, 6] : 7.201928532024472
Z[0, 1, 1, 7] : 8.312070063362382
Horizontal index start end 4 7
Z[0, 1, 2, 0] : -8.570346584299436
Z[0, 1, 2, 1] : -3.093274197766301
Z[0, 1, 2, 2] : -12.007040937567004
Z[0, 1, 2, 3] : -5.280780211701437
Z[0, 1, 2, 4] : 9.986320167081837
Z[0, 1, 2, 5] : -4.2934579269424535
Z[0, 1, 2, 6] : 5.693738392366574
Z[0, 1, 2, 7] : -10.605612124573897
Horizontal index start end 6 9
Z[0, 1, 3, 0] : 3.1066303018641444
Z[0, 1, 3, 1] : 5.105200447944384
Z[0, 1, 3, 2] : 0.6630966252526478
Z[0, 1, 3, 3] : 10.308801562475612
Z[0, 1, 3, 4] : 9.06988771742324
Z[0, 1, 3, 5] : 3.0298411488966996
Z[0, 1, 3, 6] : -5.816814573410771
Z[0, 1, 3, 7] : 4.1945945167550285
Vertical index start end 4 7
Horizontal index start end 0 3
Z[0, 2, 0, 0] : -6.119644137183075
Z[0, 2, 0, 1] : -5.239029540924501
Z[0, 2, 0, 2] : -12.165147552800256
Z[0, 2, 0, 3] : -9.731191587576443
Z[0, 2, 0, 4] : -4.523781463179992
Z[0, 2, 0, 5] : 5.297958093669381
Z[0, 2, 0, 6] : -0.44560345572184656
Z[0, 2, 0, 7] : 3.469113550629437
Horizontal index start end 2 5
Z[0, 2, 1, 0] : 10.767160292175628
Z[0, 2, 1, 1] : 6.058373023165004
Z[0, 2, 1, 2] : 2.149664856175172
Z[0, 2, 1, 3] : 10.340356454745782
Z[0, 2, 1, 4] : 9.51530963404043
Z[0, 2, 1, 5] : -7.633373353307459
Z[0, 2, 1, 6] : 9.38081852722986
Z[0, 2, 1, 7] : 10.431361028442366
Horizontal index start end 4 7
Z[0, 2, 2, 0] : -5.1604210189795285
Z[0, 2, 2, 1] : 8.69408699632557
Z[0, 2, 2, 2] : -0.39142128609396915
Z[0, 2, 2, 3] : -4.494756396025378
Z[0, 2, 2, 4] : -15.157737367200072
Z[0, 2, 2, 5] : 9.005500201435607
Z[0, 2, 2, 6] : 2.8280971870500338
Z[0, 2, 2, 7] : 8.31306508576437
Horizontal index start end 6 9
Z[0, 2, 3, 0] : 6.426007374312955
Z[0, 2, 3, 1] : -3.435489775627811
Z[0, 2, 3, 2] : 3.1179746202483476
Z[0, 2, 3, 3] : -3.6720330696313397
Z[0, 2, 3, 4] : 2.5965862782786058
Z[0, 2, 3, 5] : -0.8319059038739147
Z[0, 2, 3, 6] : 0.48357725080367564
Z[0, 2, 3, 7] : 5.101644290791892
Z mean: 0.4418899934488712
Vertical index start end 0 3
Horizontal index start end 0 3
Z[1, 0, 0, 0] : 0.7241873303624353
Z[1, 0, 0, 1] : 2.307171162194142
Z[1, 0, 0, 2] : -7.906716521478254
Z[1, 0, 0, 3] : 0.3470750543542468
Z[1, 0, 0, 4] : -2.9066213975367274
Z[1, 0, 0, 5] : -1.9186764956632123
Z[1, 0, 0, 6] : -5.863902821556679
Z[1, 0, 0, 7] : 5.507995511689806
Horizontal index start end 2 5
Z[1, 0, 1, 0] : 0.9317234177885863
Z[1, 0, 1, 1] : 11.187300240410675
Z[1, 0, 1, 2] : -5.440624960986517
Z[1, 0, 1, 3] : 6.051901455253639
Z[1, 0, 1, 4] : -9.002540685114775
Z[1, 0, 1, 5] : 9.842356435587629
Z[1, 0, 1, 6] : 8.205274720495641
Z[1, 0, 1, 7] : -12.38173397865049
Horizontal index start end 4 7
Z[1, 0, 2, 0] : 4.878712949792419
Z[1, 0, 2, 1] : -5.232036872200803
Z[1, 0, 2, 2] : -3.899450318317641
Z[1, 0, 2, 3] : 2.5983499701045476
Z[1, 0, 2, 4] : -4.924545983869402
Z[1, 0, 2, 5] : 1.8722957212252815
Z[1, 0, 2, 6] : -2.713912694519111
Z[1, 0, 2, 7] : 3.7450691023544587
Horizontal index start end 6 9
Z[1, 0, 3, 0] : -1.4689599242762585
Z[1, 0, 3, 1] : -1.0959640434354934
Z[1, 0, 3, 2] : -2.927371430153968
Z[1, 0, 3, 3] : 7.8542852033166115
Z[1, 0, 3, 4] : -2.0563732773304833
Z[1, 0, 3, 5] : 1.3995967925071269
Z[1, 0, 3, 6] : 0.18075062019193364
Z[1, 0, 3, 7] : 2.151939424188021
Vertical index start end 2 5
Horizontal index start end 0 3
Z[1, 1, 0, 0] : 1.4380457566621803
Z[1, 1, 0, 1] : -2.268323841237544
Z[1, 1, 0, 2] : 11.113685698059546
Z[1, 1, 0, 3] : 3.390876521946942
Z[1, 1, 0, 4] : 6.741950026752265
Z[1, 1, 0, 5] : 2.868266722504116
Z[1, 1, 0, 6] : -0.6300550067059175
Z[1, 1, 0, 7] : 1.9891811545936577
Horizontal index start end 2 5
Z[1, 1, 1, 0] : -0.2959685916566741
Z[1, 1, 1, 1] : -6.758984698596256
Z[1, 1, 1, 2] : 7.22676156581304
Z[1, 1, 1, 3] : 5.368940033741598
Z[1, 1, 1, 4] : -8.800325500736939
Z[1, 1, 1, 5] : 11.43237343797162
Z[1, 1, 1, 6] : 7.201928532024472
Z[1, 1, 1, 7] : 8.312070063362382
Horizontal index start end 4 7
Z[1, 1, 2, 0] : -8.570346584299436
Z[1, 1, 2, 1] : -3.093274197766301
Z[1, 1, 2, 2] : -12.007040937567004
Z[1, 1, 2, 3] : -5.280780211701437
Z[1, 1, 2, 4] : 9.986320167081837
Z[1, 1, 2, 5] : -4.2934579269424535
Z[1, 1, 2, 6] : 5.693738392366574
Z[1, 1, 2, 7] : -10.605612124573897
Horizontal index start end 6 9
Z[1, 1, 3, 0] : 3.1066303018641444
Z[1, 1, 3, 1] : 5.105200447944384
Z[1, 1, 3, 2] : 0.6630966252526478
Z[1, 1, 3, 3] : 10.308801562475612
Z[1, 1, 3, 4] : 9.06988771742324
Z[1, 1, 3, 5] : 3.0298411488966996
Z[1, 1, 3, 6] : -5.816814573410771
Z[1, 1, 3, 7] : 4.1945945167550285
Vertical index start end 4 7
Horizontal index start end 0 3
Z[1, 2, 0, 0] : -6.119644137183075
Z[1, 2, 0, 1] : -5.239029540924501
Z[1, 2, 0, 2] : -12.165147552800256
Z[1, 2, 0, 3] : -9.731191587576443
Z[1, 2, 0, 4] : -4.523781463179992
Z[1, 2, 0, 5] : 5.297958093669381
Z[1, 2, 0, 6] : -0.44560345572184656
Z[1, 2, 0, 7] : 3.469113550629437
Horizontal index start end 2 5
Z[1, 2, 1, 0] : 10.767160292175628
Z[1, 2, 1, 1] : 6.058373023165004
Z[1, 2, 1, 2] : 2.149664856175172
Z[1, 2, 1, 3] : 10.340356454745782
Z[1, 2, 1, 4] : 9.51530963404043
Z[1, 2, 1, 5] : -7.633373353307459
Z[1, 2, 1, 6] : 9.38081852722986
Z[1, 2, 1, 7] : 10.431361028442366
Horizontal index start end 4 7
Z[1, 2, 2, 0] : -5.1604210189795285
Z[1, 2, 2, 1] : 8.69408699632557
Z[1, 2, 2, 2] : -0.39142128609396915
Z[1, 2, 2, 3] : -4.494756396025378
Z[1, 2, 2, 4] : -15.157737367200072
Z[1, 2, 2, 5] : 9.005500201435607
Z[1, 2, 2, 6] : 2.8280971870500338
Z[1, 2, 2, 7] : 8.31306508576437
Horizontal index start end 6 9
Z[1, 2, 3, 0] : 6.426007374312955
Z[1, 2, 3, 1] : -3.435489775627811
Z[1, 2, 3, 2] : 3.1179746202483476
Z[1, 2, 3, 3] : -3.6720330696313397
Z[1, 2, 3, 4] : 2.5965862782786058
Z[1, 2, 3, 5] : -0.8319059038739147
Z[1, 2, 3, 6] : 0.48357725080367564
Z[1, 2, 3, 7] : 5.101644290791892
Z mean: 0.8837799868977424

Thank you!

First, please don’t post your code on the forum. That’s not allowed by the Code of Conduct.

The first problem is that “A_prev_pad[…]” is incorrect. Check your upper and lower case letters.

The second problem is the number of index values you’re using. Should be 5, you have 6.

The other key here is how you compute the start and end positions.

1 Like

I corrected the first problem “A_prev_pad” shape.
Still the Z value for [0, 0, 0, 0] is wrong.

A_prev shape 2 5 7 4
W shape 3 3 4 8
stride 2
CurrentActiation shape n_H 3 n_W 4
(2, 7, 9, 4)
After padding (2, 7, 9, 4)
Vertical index start end 0 3
Horizontal index start end 0 3
A current slice [[[[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]]

[[ 0. 0. 0. 0. ]
[ 1.62434536 -0.61175641 -0.52817175 -1.07296862]
[ 0.86540763 -2.3015387 1.74481176 -0.7612069 ]]

[[ 0. 0. 0. 0. ]
[-0.26788808 0.53035547 -0.69166075 -0.39675353]
[-0.6871727 -0.84520564 -0.67124613 -0.0126646 ]]]

[[[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]]

[[ 0. 0. 0. 0. ]
[-0.18656977 -0.10174587 0.86888616 0.75041164]
[ 0.52946532 0.13770121 0.07782113 0.61838026]]

[[ 0. 0. 0. 0. ]
[ 2.19069973 -1.89636092 -0.64691669 0.90148689]
[ 2.52832571 -0.24863478 0.04366899 -0.22631424]]]]
Weights: [[[-0.78191168 -1.11647002 0.417302 -0.27909772]
[ 1.3887794 -0.13597733 -0.23794194 -2.03720123]
[-0.10999149 -0.19505734 -0.55749472 0.58464661]]

[[ 2.13782807 0.61798553 -0.47537288 -1.30653407]
[ 0.45161595 -0.77785883 2.0546241 0.84086156]
[ 0.45128402 -0.38483225 -0.82246719 -0.0693287 ]]

[[-0.19899818 0.5154138 1.04008915 0.52887975]
[-0.25898285 0.16986926 1.44287693 0.63658341]
[ 0.68400133 -2.22711263 1.01120706 0.85328219]]] biases: -1.3988128186664763
Z[0, 0, 0, 0] : 0.7241873303624353

Could you please explain what is the second problem, I didn’t get it?

Thank you!

My analysis of your initial post is that your loop logic looks correct. But I’m a little worried that you missed Tom’s point about the `A_prev_pad` handling. It looks like you were using the 4D full `A_prev_pad` and selecting all elements along the “samples” axis. That will give the wrong answer. The point is that the outer loop is over the samples, right? So you index `A_prev_pad` at the beginning of each iteration of the outer loop and select only the appropriate value of the first index (the “samples” index) of `A_prev_pad`, which gives you a 3D array. That is what you use (indexed appropriately) as the input when you compute each Z value in the innermost loop.

1 Like

Now the values of Z is not matching.
For example Z[0,0,0,0]

A_prev shape 2 5 7 4
W shape 3 3 4 8
stride 2
CurrentActiation shape n_H 3 n_W 4
Shape of Z: (2, 3, 4, 8)
(2, 7, 9, 4)
Vertical index start end 0 3
Horizontal index start end 0 3
a_slice_prev shape: (3, 3, 4) value: [[[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]]

[[ 0. 0. 0. 0. ]
[ 1.62434536 -0.61175641 -0.52817175 -1.07296862]
[ 0.86540763 -2.3015387 1.74481176 -0.7612069 ]]

[[ 0. 0. 0. 0. ]
[-0.26788808 0.53035547 -0.69166075 -0.39675353]
[-0.6871727 -0.84520564 -0.67124613 -0.0126646 ]]]
Weights.shape: (3, 3, 4)
biases: (1, 1, 1)
Z[0, 0, 0, 0] : -2.651123629553914

I got it fixed. All test passed!

Z[1, 12, 14, 4] -1.526863330872842 Z[1, 12, 14, 5] -0.9182084566541844 Z[1, 12, 14, 6] -0.39510966923547597 Z[1, 12, 14, 7] -1.622674822605305

Second Test: All tests passed!

Thank you so much @TMosh @paulinpaloalto

That’s great news. Thanks for confirming. Onward!

1 Like