# Course 4, week 1, Convolution_model_Step_by_Step_v1, Exercise 3 - unable to overcome shapes error

I tried almost all the methods possible but still unable to solve the mentioned error for the past few days. Will really appreciate it if someone could help me regarding this. Thanks

Here is the snippet of the error.

Here is the snippet of the code:

{moderator edit - solution code removed}

please ensure you iterate over the outputβs height (n_H) and weight (n_W)

2 Likes

Yes, your loops for h and w are incorrect as @Mohammed_ALAshqar points out, but thatβs only part of the problem. The error is a mismatch on the channel dimension, right? So my guess is that means your A_prev_pad is the wrong shape. Print all the dimensions early on, before the big loop and make sure that the nC_prev dimensions of A_prev_pad and W match.

Also note that we arenβt supposed to leave solution source code sitting out there on the forums. It definitely helps for debugging purposes, but once you get this figured out please edit your original post to remove the solution code.

2 Likes

I had the same issue with the use of n_H & n_W as well. And nC_prev dimensions of A_prev_pad and W does match.
Thatβs why I mentioned in my question that I tried many things before posting here.

P.S. I am unable to edit my post atm.

Did you find a solution? Stuck at the same problem, yet I already iterate over the correct values. I am stuck with dimensions of 3, 0, 4.

Iβve also seen cases in which the studentβs zero_pad routine was incorrect: e.g. references a global variable as the input instead of the actual parameter, so that it passes the test in the notebook but fails when you actually use the function.

I added print statements to show the shapes of things and hereβs what I get when I run the conv_forward test cell:

``````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]
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.2238796505752447
Z[1,8,10,7] = -0.47458986707940803
New dimensions = 2 by 3
Shape Z = (2, 2, 3, 8)
Shape A_prev_pad = (2, 5, 7, 4)
Z[0,0,0,0] = 3.14880664541713
Z[1,1,2,7] = 1.0956417259542868
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.5096687406137471
Z[1,12,14,7] = -0.3247422640409677
(2, 13, 15, 8)
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
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
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
All tests passed.
``````

Thank you for the tip about the zero_pad function, yet Iβm using only the variables X and pad in the line of the function, with the given example code

``````a = np.pad(a, ((0,0), (1,1), (0,0), (3,3), (0,0)), mode='constant', constant_values = (0,0))
``````

I tried some of those print statements myself, but they differ a lot from yours. First of all, the shape of Z and A_prev_pad remain static, and after the seventh iteration of c during the fourth iteration of w, the ValueError occurs for me:

``````Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  -2.651123629553914
Z's mean =  -0.006903967785296651
i:  0 h:  0 w:  0 c:  0

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  -3.0296153950722533
Z's mean =  -0.007889623424667326
i:  0 h:  0 w:  0 c:  1

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  -5.000164680364149
Z's mean =  -0.013021262188448304
i:  0 h:  0 w:  0 c:  2

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  -6.962517673393977
Z's mean =  -0.01813155644113015
i:  0 h:  0 w:  0 c:  3

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  -8.685116389747225
Z's mean =  -0.022617490598300066
i:  0 h:  0 w:  0 c:  4

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  -8.217447091570953
Z's mean =  -0.021399601800966023
i:  0 h:  0 w:  0 c:  5

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  -14.651787254184034
Z's mean =  -0.03815569597443759
i:  0 h:  0 w:  0 c:  6

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  -13.544137315201041
Z's mean =  -0.035271190925002714
i:  0 h:  0 w:  0 c:  7

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  -8.867208030950682
Z's mean =  -0.023091687580600734
i:  0 h:  0 w:  1 c:  0

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  -4.568553879006972
Z's mean =  -0.011897275726580656
i:  0 h:  0 w:  1 c:  1

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  -5.929356980188387
Z's mean =  -0.015441033802573924
i:  0 h:  0 w:  1 c:  2

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  -5.124028393562162
Z's mean =  -0.013343823941568132
i:  0 h:  0 w:  1 c:  3

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  -8.008829474546838
Z's mean =  -0.02085632675663239
i:  0 h:  0 w:  1 c:  4

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  0.9439708679464243
Z's mean =  0.00245825746861048
i:  0 h:  0 w:  1 c:  5

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  6.270248940073855
Z's mean =  0.01632877328144233
i:  0 h:  0 w:  1 c:  6

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  4.443896358092878
Z's mean =  0.011572646765866869
i:  0 h:  0 w:  1 c:  7

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  2.385084617975479
Z's mean =  0.006211157859311144
i:  0 h:  0 w:  2 c:  0

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  5.793682570738749
Z's mean =  0.015087715027965493
i:  0 h:  0 w:  2 c:  1

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  6.143910773065906
Z's mean =  0.015999767638192466
i:  0 h:  0 w:  2 c:  2

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  6.8269470308811
Z's mean =  0.017778507892919532
i:  0 h:  0 w:  2 c:  3

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  4.943666378808224
Z's mean =  0.012874131194813085
i:  0 h:  0 w:  2 c:  4

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  3.0688646386373346
Z's mean =  0.007991834996451392
i:  0 h:  0 w:  2 c:  5

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  8.869736736238327
Z's mean =  0.02309827275062064
i:  0 h:  0 w:  2 c:  6

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  8.9398285391289
Z's mean =  0.023280803487314844
i:  0 h:  0 w:  2 c:  7

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  5.438410627626942
Z's mean =  0.014162527676111827
i:  0 h:  0 w:  3 c:  0

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  8.142696632280225
Z's mean =  0.021204939146563087
i:  0 h:  0 w:  3 c:  1

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  8.426110093586647
Z's mean =  0.021942995035381895
i:  0 h:  0 w:  3 c:  2

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  12.582484201076461
Z's mean =  0.03276688594030328
i:  0 h:  0 w:  3 c:  3

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  12.116725860407579
Z's mean =  0.0315539735948114
i:  0 h:  0 w:  3 c:  4

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  11.680037620795233
Z's mean =  0.030416764637487586
i:  0 h:  0 w:  3 c:  5

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  6.111376561344699
Z's mean =  0.01591504312850182
i:  0 h:  0 w:  3 c:  6

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  7.834256891947722
Z's mean =  0.020401710656113858
i:  0 h:  0 w:  3 c:  7

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  6.4354440732812455
Z's mean =  0.016758968940836578
i:  0 h:  0 w:  4 c:  0

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  6.517211892086862
Z's mean =  0.016971905968976204
i:  0 h:  0 w:  4 c:  1

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  6.057269061246174
Z's mean =  0.01577413818032858
i:  0 h:  0 w:  4 c:  2

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  6.701622727276496
Z's mean =  0.01745214251894921
i:  0 h:  0 w:  4 c:  3

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  7.073293018488362
Z's mean =  0.01842003390231344
i:  0 h:  0 w:  4 c:  4

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  8.926302503557741
Z's mean =  0.023245579436348283
i:  0 h:  0 w:  4 c:  5

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  9.068553876084058
Z's mean =  0.0236160257189689
i:  0 h:  0 w:  4 c:  6

Shape Z =  (2, 4, 6, 8)
Shape A_prev_pad =  (2, 7, 9, 4)
Sum of Z: =  9.582059356072605
Z's mean =  0.024953279573105742
i:  0 h:  0 w:  4 c:  7

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-97-182241fd5e53> in <module>
6                "stride": 2}
7
----> 8 Z, cache_conv = conv_forward(A_prev, W, b, hparameters)
9 print("Z's mean =\n", np.mean(Z))
10 print("Z[0,2,1] =\n", Z[0, 2, 1])

<ipython-input-96-dd5f5ae21149> in conv_forward(A_prev, W, b, hparameters)
84                     weights = W[...,c]
85                     biases = b[...,c]
---> 86                     Z[i, h, w, c] = conv_single_step(a_slice_prev, weights, biases)
87
88                     #print("New dimensions = ", a_slice_prev.shape)

<ipython-input-14-6f40795bc83a> in conv_single_step(a_slice_prev, W, b)
23     # Z = None
24     # YOUR CODE STARTS HERE
---> 25     s = np.multiply(a_slice_prev, W)
26     Z = np.sum(s)
27     Z = Z + float(b)

ValueError: operands could not be broadcast together with shapes (3,0,4) (3,3,4)
``````
1 Like

Yes, the shapes of Z and A_prev_pad are static, so there is no point in printing them out on every iteration of your inner loop. Maybe you are not taking the stride into account correctly somehow. It looks like you are correctly using the output space as the loop limits, but the question is how you compute where that maps to back in the input space. Maybe you should show the nH_{prev} values in your loop to see how they are moving as you loop through.

As an example, I added a print statement in the inner loop and hereβs enough of the output to see what is going on:

``````New dimensions = 3 by 4
Shape Z = (2, 3, 4, 8)
Shape A_prev_pad = (2, 7, 9, 4)
Z[0,0,0,0] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 0:3
Z[0,0,0,0] = -2.651123629553914
Z[0,0,0,1] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 0:3
Z[0,0,0,2] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 0:3
Z[0,0,0,3] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 0:3
Z[0,0,0,4] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 0:3
Z[0,0,0,5] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 0:3
Z[0,0,0,6] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 0:3
Z[0,0,0,7] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 0:3
Z[0,0,1,0] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 2:5
Z[0,0,1,1] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 2:5
Z[0,0,1,2] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 2:5
Z[0,0,1,3] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 2:5
Z[0,0,1,4] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 2:5
Z[0,0,1,5] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 2:5
Z[0,0,1,6] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 2:5
Z[0,0,1,7] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 2:5
Z[0,0,2,0] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 4:7
Z[0,0,2,1] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 4:7
Z[0,0,2,2] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 4:7
Z[0,0,2,3] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 4:7
Z[0,0,2,4] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 4:7
Z[0,0,2,5] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 4:7
Z[0,0,2,6] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 4:7
Z[0,0,2,7] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 4:7
Z[0,0,3,0] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 6:9
Z[0,0,3,1] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 6:9
Z[0,0,3,2] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 6:9
Z[0,0,3,3] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 6:9
Z[0,0,3,4] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 6:9
Z[0,0,3,5] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 6:9
Z[0,0,3,6] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 6:9
Z[0,0,3,7] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 6:9
Z[0,1,0,0] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 0:3
Z[0,1,0,1] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 0:3
Z[0,1,0,2] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 0:3
Z[0,1,0,3] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 0:3
Z[0,1,0,4] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 0:3
Z[0,1,0,5] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 0:3
Z[0,1,0,6] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 0:3
Z[0,1,0,7] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 0:3
Z[0,1,1,0] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 2:5
Z[0,1,1,1] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 2:5
Z[0,1,1,2] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 2:5
Z[0,1,1,3] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 2:5
Z[0,1,1,4] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 2:5
Z[0,1,1,5] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 2:5
Z[0,1,1,6] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 2:5
Z[0,1,1,7] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 2:5
Z[0,1,2,0] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 4:7
Z[0,1,2,1] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 4:7
Z[0,1,2,2] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 4:7
Z[0,1,2,3] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 4:7
Z[0,1,2,4] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 4:7
Z[0,1,2,5] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 4:7
Z[0,1,2,6] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 4:7
Z[0,1,2,7] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 4:7
Z[0,1,3,0] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 6:9
Z[0,1,3,1] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 6:9
Z[0,1,3,2] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 6:9
Z[0,1,3,3] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 6:9
Z[0,1,3,4] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 6:9
Z[0,1,3,5] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 6:9
Z[0,1,3,6] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 6:9
Z[0,1,3,7] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 6:9
Z[0,2,0,0] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 0:3
Z[0,2,0,1] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 0:3
Z[0,2,0,2] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 0:3
Z[0,2,0,3] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 0:3
Z[0,2,0,4] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 0:3
Z[0,2,0,5] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 0:3
Z[0,2,0,6] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 0:3
Z[0,2,0,7] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 0:3
Z[0,2,1,0] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 2:5
Z[0,2,1,1] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 2:5
Z[0,2,1,2] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 2:5
Z[0,2,1,3] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 2:5
Z[0,2,1,4] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 2:5
Z[0,2,1,5] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 2:5
Z[0,2,1,6] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 2:5
Z[0,2,1,7] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 2:5
Z[0,2,2,0] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 4:7
Z[0,2,2,1] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 4:7
Z[0,2,2,2] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 4:7
Z[0,2,2,3] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 4:7
Z[0,2,2,4] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 4:7
Z[0,2,2,5] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 4:7
Z[0,2,2,6] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 4:7
Z[0,2,2,7] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 4:7
Z[0,2,3,0] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 6:9
Z[0,2,3,1] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 6:9
Z[0,2,3,2] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 6:9
Z[0,2,3,3] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 6:9
Z[0,2,3,4] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 6:9
Z[0,2,3,5] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 6:9
Z[0,2,3,6] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 6:9
Z[0,2,3,7] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 6:9
Z[1,0,0,0] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 0:3
Z[1,0,0,1] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 0:3
Z[1,0,0,2] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 0:3
Z[1,0,0,3] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 0:3
Z[1,0,0,4] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 0:3
Z[1,0,0,5] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 0:3
Z[1,0,0,6] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 0:3
Z[1,0,0,7] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 0:3
Z[1,0,1,0] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 2:5
Z[1,0,1,1] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 2:5
Z[1,0,1,2] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 2:5
Z[1,0,1,3] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 2:5
Z[1,0,1,4] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 2:5
Z[1,0,1,5] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 2:5
Z[1,0,1,6] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 2:5
Z[1,0,1,7] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 2:5
Z[1,0,2,0] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 4:7
Z[1,0,2,1] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 4:7
Z[1,0,2,2] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 4:7
Z[1,0,2,3] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 4:7
Z[1,0,2,4] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 4:7
Z[1,0,2,5] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 4:7
Z[1,0,2,6] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 4:7
Z[1,0,2,7] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 4:7
Z[1,0,3,0] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 6:9
Z[1,0,3,1] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 6:9
Z[1,0,3,2] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 6:9
Z[1,0,3,3] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 6:9
Z[1,0,3,4] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 6:9
Z[1,0,3,5] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 6:9
Z[1,0,3,6] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 6:9
Z[1,0,3,7] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 6:9
``````
1 Like

Also note that your Z shape is incorrect.

1 Like

It reads, as the nHprev values should be changing throughout the loop, yet the remain static for me (value = 5).

``````Shape of Z:  (2, 4, 6, 8)
``````

Regarding Z: So, my m and n_C-value seem to fit, yet my n_H and n_W are offβ¦

5 Minutes later

Oh my god, Iβve implemented

``````ππ»=βππ»ππππ£βπ+2Γπππ/π π‘ππππβ+1
ππ=βππππππ£βπ+2Γπππ/π π‘ππππβ+1
``````

wrong, the round brackets led the equation to divide the pad.

Thank you very much for the hint, it now works!

2 Likes

Yes, you definitely need some parentheses there in order to implement those formulas correctly. Glad to hear you figured it out! Thanks for confirming.

2 Likes

Yeah, I had them, but placed them poorlyβ¦

as same you:
the round brackets led the equation to divide the pad
thanks

After spending hours here, my solution finally meets this:

``````Z[0,0,0,0], Z[1,2,3,7]
# -2.651123629553914, 0.4427056509973153
``````

However, Iβm still getting:

``````# Test 2
---> 95     Z, cache_conv = target(A_prev, W, b, {"pad" : 0, "stride": 2})
96     assert(Z.shape == (2, 2, 3, 8)), "Wrong shape. Don't hard code the pad and stride values in the function"
``````

Stride and pad are not hard coded.

Would you please help me debuging it?

Well if the shape mismatches, the first question is βwhat is the actual shape of Zβ? Then the next question is βhow did it get that way?β This is how debugging works, right? You start from the evidence and then work backwards one step at a time.

If uβre still stuck, please take a look at the following hints cuz I had the same issue:

Follow these instructions carefully which I obtained from here:

Preparation

Retreive information (m, n_H_prev, n_W_prev, n_C_prev) from A_prev and (f, f, n_C_prev, n_C) from W using .shape
Retreive stride and padding from hparameters python dictionnary
calculate n_H and n_W using the formula and the int() function as floor
initialize Z using .zeros with dimensions (m ,n_H, n_W, n_C)
use zero_pad() function to add padding to A_prev (result: A_prev_pad)

Loops:

first for loop:
loop over m Examples using index i and extract a_prev_pad from A_prev_pad using index in the first axis position

second for loop:
loop over vertical output layer axis (n_H) using index h and calculate (vert_start, vert_end):
multiply stride to index h for vert_start
add filter height f for vert_end

third for loop:
loop over horizontal output layer axis (n_W) using index w and calculate (horiz_start, horiz_end):
multiply stride to index w for horiz_start
add filter width f for horiz_end

fourth for loop:
loop over f (= number of output channels) using index c
slice out a_slice_prev from a_prev_pad using [vert_start:vert_end, horiz_start:horiz_end, all]
slice out weights from W using [all, all, all, c]
slice out biases from b using [0, 0, 0, c]
calculate Z[i, h, w, c] using function conv_single_step() with inputs a_slice_prev, weights and biases

Please pay specific attention to the bold texts and also bold + italic in my comment! Uβd definitely fix the problem
Cheers,

7 Likes

Thanks for the excellent instructions that describe what to do without actually writing the code!

fourth loop I had been using range(n_C_prev) instead of range(n_C)

I was getting the correct value for: Z[0,0,0,0] but zero for Z[1,2,3,7].

Thanks for the walkthroughβ¦ I was close but stuck on an easy to overlook detail.

1 Like

Please, I thing my .zeros is good but I get this error

That is not the correct syntax for calling `np.zeros`. Have a look at the documentation page. It takes a single positional argument which is a python βtupleβ giving the requested dimensions. Weβve been using `np.zeros` since Course 1 and the syntax for that is different than the syntax for the `np.random` functions. Those take a variable list of dimensions.