Week 1 cov_forward

I am getting following error.

IndexError Traceback (most recent call last)
in
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]
11 cache_0_1_2_3 = cache_conv[0][1][2][3]
12 print(“Z’s mean =\n”, z_mean)

IndexError: invalid index to scalar variable.

That error message is telling you that the Z value there which was returned by your conv_forward function is a scalar. But it should be an array with 4 dimensions. So you need to examine your code and figure out how that mistake happened.

np.random.seed(1)
A_prev = np.random.randn(2, 5, 7, 4)
W = np.random.randn(3, 3, 4, 8)
b = np.random.randn(1, 1, 1, 8)
hparameters = {"pad" : 1,
               "stride": 2}

The above code shows the shapes of the input data for this test case. From that, we can figure out what the output shape should be:

n_{out} = \displaystyle \lfloor \frac {n_{in} + 2p - f}{s} \rfloor + 1

Where n_{in} is 5 for the vertical and 7 for the horizontal and f = 3, s = 2 and p = 1.

Using that formula, the dimensions of Z should be 2 x 3 x 4 x 8, right? So what happened to make Z a scalar in your code?