Week 1 Exercise 3 - conv_forward

{mentor edit: code removed - not allowed by the Honor Code}


ValueError Traceback (most recent call last)
in
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])

in conv_forward(A_prev, W, b, hparameters)
72 print(‘cBBB’,biases.shape)
73 #print(a_slice_prev[:,:,4].shape)
—> 74 Z[i, h, w, c] = conv_single_step(a_slice_prev, weights, biases)
75 # YOUR CODE STARTS HERE
76

in conv_single_step(a_slice_prev, W, b)
23 # Z = None
24 # YOUR CODE STARTS HERE
—> 25 s = a_slice_prev *W
26 Z =np.sum(s)
27 Z= Z+float(b)

ValueError: operands could not be broadcast together with shapes (3,3) (3,3,4)

Help!!

The vert start and horiz start need to include multiplication by the stride.

1 Like

i already included the steps in the range, i tried multiplying as you told me but it didn’t work

You can’t put the stride value into the range, because it skips over some of the h and w values.

1 Like

It also looks like your a_slice_prev value is the wrong shape: you have included only the height and width dimensions, but it also needs the input channels dimension.

1 Like

Right! The key point you need to keep in mind is that the loops are over the output space and you need to touch every possible value. Then for each output position, you have to “back calculate” where that maps to in the input space.

1 Like

thanks both @TMosh and @paulinpaloalto , now i have another error

(m, n_H_prev, n_W_prev, n_C_prev)
2 5 7 4
f, f, n_C_prev, n_C
3 3 4 8
stride 2 pad 1
A_prev_pad shape (2, 7, 9, 4)
2
aa_prev_pad shape (7, 9, 4)
range(0, 3)
vert_start,vert_end 0 3
range(0, 4)
horiz_start,horiz_end 0 3
range 7
a_slice_prev shape (3, 3, 4)
[[[ 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)
[[[-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 shape (1, 1, 1)
[[[-1.39881282]]]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-192-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-191-841c1dafa400> in conv_forward(A_prev, W, b, hparameters)
     81                     print(biases)
     82 
---> 83                     Z[i, h, w, c] = conv_single_step(a_slice_prev, weights, biases)
     84     # YOUR CODE STARTS HERE
     85 

IndexError: too many indices for array

The question is what is the shape of Z? It looks like that’s what is not correct in that line that throws the exception. Here’s another recent thread on which I show the Z shape.

1 Like

@paulinpaloalto finally after 4+ hours i fixed it, really really thank you!!! i wasn’t defining the m dimension in Z.