I'm stuck in Convolutional Neural Network week one exercise 1 conv_forward

Hi everyone, please I am stuck on the conv_forward, I have checked various similar problems in the community but I couldn’t solve my issue

My problem is I am confused on how to filter a_prev_pad, how to set vert_start,vert_end,horiz_start,horiz_end values

Though I have fully implement every aspects of the function but I am getting this error

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-54-7e580406a9e8> in <module>
      6                "stride": 2}
      7 
----> 8 Z, cache_conv = conv_forward(A_prev, W, b, hparameters)
      9 z_mean = np.mean(Z)
     10 z_0_2_1 = Z[0, 2, 1]

<ipython-input-53-c70e15ae235b> in conv_forward(A_prev, W, b, hparameters)
    106                     weights = W[:,:,:,c]
    107                     biases = b[0,0,0,c]
--> 108                     Z[i, h, w, c] = conv_single_step(a_slice_prev, weights, biases)
    109 
    110 

<ipython-input-45-5e88d7b4426d> 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 = np.float_(b) + Z

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

I have tried so many things, I have tried adding strides to the vert_start,vert_end,horiz_start,horiz_end variables but I’m still getting the same error, now I am now confused on what else to do to fix this error, please I have been on it since last week, though I left it and move on to complete the course and now I have completed the course but it’s this assignment that’s preventing me from getting certificate, please help me

Here’s a thread which gives a nice complete explanation of how this works. Please have a look and see if that gets you to a solution. If not, let us know and we can talk in more detail.

thanks for your response, yes I will do that really quick

I have followed the thread, it is very helpful, but I am now getting a different error

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-32-7e580406a9e8> in <module>
      6                "stride": 2}
      7 
----> 8 Z, cache_conv = conv_forward(A_prev, W, b, hparameters)
      9 z_mean = np.mean(Z)
     10 z_0_2_1 = Z[0, 2, 1]

<ipython-input-31-7b7d8ceae9f3> in conv_forward(A_prev, W, b, hparameters)
    105                     weights = W[:,:,:,c]
    106                     biases = b[0,0,0,c]
--> 107                     Z[i, h, w, c] = conv_single_step(a_slice_prev, weights, biases)
    108 
    109 

IndexError: too many indices for array

What is the shape of your Z value? Try this:

print(Z.shape)

Debugging is part of the job. The first thing to do is understand what the error message says. Then investigate how it happened.

Yes thanks, I passed only m to initialize Z before like np.zeros(m) but after I checked the guide again I have corrected it and the output of Z shape is now (2, 3, 4, 8)

now I am now getting this error

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-67-7e580406a9e8> in <module>
      6                "stride": 2}
      7 
----> 8 Z, cache_conv = conv_forward(A_prev, W, b, hparameters)
      9 z_mean = np.mean(Z)
     10 z_0_2_1 = Z[0, 2, 1]

<ipython-input-66-6aaac0470eb7> in conv_forward(A_prev, W, b, hparameters)
    107                     biases = b[0,0,0,c]
    108                     print(Z.shape)
--> 109                     Z[i, h, w, c] = conv_single_step(a_slice_prev, weights, biases)
    110 
    111 

<ipython-input-60-73b47d09ea17> 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 = np.float64(b) + Z

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

That indicates that you have not gotten the vert_start and horiz_start calculations right yet. Please read over the instructions on that thread one more time. Also note that h is for “height” or vertical, not horizontal. Make sure you haven’t reversed the meaning of h and w.

I’ve gotten a new progress, I got another error now I don’t really know what is causing it

Z's mean =
 -0.15054316428881753
Z[0,2,1] =
 [-2.17796037  8.07171329 -0.5772704   0.          0.          0.
  0.          0.        ]
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.15054316428881753 . 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: [-2.17796037  8.07171329 -0.5772704   0.          0.          0.
  0.          0.        ] Make sure you include stride in your calculation

f 3
f 3
f 5

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-91-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)
    117                              [-0.47552486, -0.16577702, -0.64971742,  1.63138295]])
    118 
--> 119     assert np.isclose(Z_means, expected_Z), f"Wrong Z mean. Expected: {expected_Z} got: {Z_means}"
    120     assert np.allclose(cache_conv[0][1, 2], expected_conv), f"Values in Z are wrong"
    121 

AssertionError: Wrong Z mean. Expected: -0.5384027772160062 got: -0.2502243266209062

I think it’s time to look at your code. I sent you a DM about how we can do that.

To close the loop on the public thread, everything was correct except for one detail, which was the “range” value on the loop over the channels. That value needs to be the number of output channels, which is n_C.

1 Like

Hello, I’m experiencing a similar issue even if the range for the channel loop uses n_C. I spent a good hour writing and rewriting the code, but it seems I can’t see my error.
I also tried to print the coordinates of the window sliding on A_prev and everything seems correct. Shapes are… in good shape :wink: and I also double-checked conv_single_step() - which anyway passes the test.

Z’s mean =
-0.26830164045131716
Z[0,2,1] =
[ -5.0137909 4.46627327 -11.02531271 -0.3181254 1.45668754
2.18425493 1.39474083 -8.02128505]
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.26830164045131716 . 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: [ -5.0137909 4.46627327 -11.02531271 -0.3181254 1.45668754
2.18425493 1.39474083 -8.02128505] Make sure you include stride in your calculation

There are lots of moving parts here and many ways to go off the rails. The hardest parts are dealing with the “input space” versus the “output space”. Here’s a thread which gives a really nice description of the algorithms in words.

The key point is that the loops are over the output space and we must touch every position in the output space. The “striding” happens in the input space. That means all the for loops have a “step” of 1, right?

Right.
I write in (h,w) belonging to (n_H, n_W), moving with step 1.
The “sliding window” moves on the input with (h,w) * (stride,stride).
I printed all the pair of vertices and they are OK.
It seems that I’m doing exactly what the thread suggests… so I am really blind and don’t see my mistake yet.

To close the loop on the public thread, it turns out the bug was really subtle: the weights W were being “sliced” differently than expected.

1 Like