Course 4 week 1 conv_forward ValueError

Hi, can someone help me with this error:


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)
86 biases = b[:, :, :, c]
87 print(a_slice_prev.shape)
—> 88 Z[i, h, w, c] = conv_single_step(a_slice_prev, weights, biases)
89
90

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) + float(b)
27

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

I guess I made mistake in a_slice_prev = a_prev_pad[vert_start:vert_end, horiz_start:horiz_end, stride]
but still stuck

1 Like

The code for "a_slice_prev = …"does not use the “stride” parameter. The last argument should be a : character, to slice the a_prev_pad correctly.

1 Like

oh! sry for that mistake;
but now I am getting this:


AssertionError Traceback (most recent call last)
in
11 print(“cache_conv[0][1][2][3] =\n”, cache_conv[0][1][2][3])
12
—> 13 conv_forward_test(conv_forward)

~/work/release/W1A1/public_tests.py in conv_forward_test(target)
74 # Test 2
75 Z, cache_conv = target(A_prev, W, b, {“pad” : 0, “stride”: 2})
—> 76 assert(Z.shape == (2, 2, 3, 8)), “Wrong shape. Don’t hard code the pad and stride values in the function”
77
78 # Test 3

AssertionError: Wrong shape. Don’t hard code the pad and stride values in the function

Ok, there was a mistake in dividing with stride but now this error is coming:
Z’s mean =
0.28648854269868845
Z[0,2,1] =
[0. 0. 0. 0. 0. 0. 0. 0.]
cache_conv[0][1][2][3] =
[-1.1191154 1.9560789 -0.3264995 -1.34267579]
(2, 13, 15, 8)
Error: Wrong output for variable in position 0.
2 Tests passed
1 Tests failed

AssertionError Traceback (most recent call last)
in
11 print(“cache_conv[0][1][2][3] =\n”, cache_conv[0][1][2][3])
12
—> 13 conv_forward_test(conv_forward)

~/work/release/W1A1/public_tests.py in conv_forward_test(target)
118 ]
119
→ 120 multiple_test(test_cases, target)
121
122

~/work/release/W1A1/test_utils.py in multiple_test(test_cases, target)
151 print(’\033[91m’, len(test_cases) - success, " Tests failed")
152 raise AssertionError(
→ 153 “Not all tests were passed for {}. Check your equations and avoid using global variables inside the function.”.format(target.name))

AssertionError: Not all tests were passed for conv_forward. Check your equations and avoid using global variables inside the function.

All test passed​:grinning_face_with_smiling_eyes:
The major problem was in the loops; Finally I used the stride properly.