Week1 Assignment 1 conv_forward

I get dimensions error using conv_single_step when f=5 or for the second convolutional layer but not for f=3 for the first layer. I think its due to the my for loop ranges being wrong but I don’t understand why. Could you help me out ?

Heyo, I don’t think you should get a dimension error on conv_single_step unless you’re passing the wrong slice. Can you post the error you’re getting from the tests?

I created a new cell in my notebook and then imported that test case from public_tests.py. You have to make a couple of small changes:

np.random.seed(1)
A_prev = np.random.randn(2, 5, 7, 4)
W = np.random.randn(5, 5, 4, 8)
b = np.random.randn(1, 1, 1, 8)
Z, cache_conv = conv_forward(A_prev, W, b, {"pad" : 6, "stride": 1})
Z_shape = Z.shape
print(Z_shape)
assert Z_shape[0] == A_prev.shape[0], f"m is wrong. Current: {Z_shape[0]}.  Expected: {A_prev.shape[0]}"
assert Z_shape[1] == 13, f"n_H is wrong. Current: {Z_shape[1]}.  Expected: 13"
assert Z_shape[2] == 15, f"n_W is wrong. Current: {Z_shape[2]}.  Expected: 15"
assert Z_shape[3] == W.shape[3], f"n_C is wrong. Current: {Z_shape[3]}.  Expected: {W.shape[3]}"

I also added print statements to show the shapes of all the relevant things that my code produces. Here’s what I get when I run that test with my code:

New dimensions = 13 by 15
Shape Z = (2, 13, 15, 8)
Shape A_prev_pad = (2, 17, 19, 4)
Shape a_slice_prev = (5, 5, 4)
Shape a_slice_prev = (5, 5, 4)
Shape a_slice_prev = (5, 5, 4)

That repeats on all iterations. So it looks like your a_slice_prev is the wrong shape. The only question is whether that happens on the very first iteration of the nested loops or perhaps later. That should tell you where to look …

I no longer receive dimensions error but a value error for the output. What are the correct outputs for that exercise?

I added the same output prints from the other test case in the notebook to the cell I showed above:

print("Z's mean =\n", np.mean(Z))
print("Z[0,2,1] =\n", Z[0, 2, 1])
print("cache_conv[0][1][2][3] =\n", cache_conv[0][1][2][3])

Here’s what I get from those statements with code that passes the grader:

New dimensions = 13 by 15
Shape Z = (2, 13, 15, 8)
Shape A_prev_pad = (2, 17, 19, 4)
(2, 13, 15, 8)
Z's mean =
 0.37765563119199264
Z[0,2,1] =
 [ 0.62717704  0.16280825  0.28861672  0.05830738  1.63193585 -0.40178883
 -0.19993939  0.00738898]
cache_conv[0][1][2][3] =
 [-1.1191154   1.9560789  -0.3264995  -1.34267579]

I have the correct cahe_conv values but i can not set the Z values for some reason meaning most of my z matrix is 0s.

The values are set inside the triple-nested for loop but disappear outside of that scope.

It is true that Z is a local variable in the conv_forward function, but it is also one of the return values of the function, right? What variable does that return value get assigned to at global scope? My guess is that the real problem is the indexing of Z within the scope of conv_forward. But in any case it should be logical to start by confirming that is happening correctly. If you can confirm that is correct, then you can worry about the scope issues.