C4:W1:A1: pool_forward


ValueError Traceback (most recent call last)
in
4 hparameters = {“stride” : 1, “f”: 3}
5
----> 6 A, cache = pool_forward(A_prev, hparameters, mode = “max”)
7 print(“mode = max”)
8 print("A.shape = " + str(A.shape))

in pool_forward(A_prev, hparameters, mode)
68
69 if mode == “max”:
—> 70 A[i, h, w, c] = np.max(a_prev_slice)
71 elif mode == “average”:
72 A[i, h, w, c] = np.mean(a_prev_slice)

<array_function internals> in amax(*args, **kwargs)

/opt/conda/lib/python3.7/site-packages/numpy/core/fromnumeric.py in amax(a, axis, out, keepdims, initial, where)
2666 “”"
2667 return _wrapreduction(a, np.maximum, ‘max’, axis, None, out,
→ 2668 keepdims=keepdims, initial=initial, where=where)
2669
2670

/opt/conda/lib/python3.7/site-packages/numpy/core/fromnumeric.py in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
88 return reduction(axis=axis, out=out, **passkwargs)
89
—> 90 return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
91
92

ValueError: zero-size array to reduction operation maximum which has no identity

I can tell from the error that my a_prev_slice has some shape issue , I think it’s from slicing poorly but I am unable to find a mistake in it.
I am slicing the first two from the start to end and the third one ‘channel’ from c to c to make sure I only use 1 channel at a time while pooling.
I think the issue is somewhere here. But I can’t put a finger on it.

You don’t need to “slice” the channel dimension for pooling, right? You just specify the channel you want. You are looping over each channel individually.

But if that’s not enough to get you to a solution, the first thing to do when you have a dimension problem is to figure out what the dimension actually is. I added some logic to my pool_forward to print the shape of a_slice_prev and here’s what I see when I run that test cell:

a_prev_slice.shape (3, 3)
mode = max
A.shape = (2, 3, 3, 3)
A[1, 1] =
 [[1.96710175 0.84616065 1.27375593]
 [1.96710175 0.84616065 1.23616403]
 [1.62765075 1.12141771 1.2245077 ]]
a_prev_slice.shape (3, 3)
mode = average
A.shape = (2, 3, 3, 3)
A[1, 1] =
 [[ 0.44497696 -0.00261695 -0.31040307]
 [ 0.50811474 -0.23493734 -0.23961183]
 [ 0.11872677  0.17255229 -0.22112197]]
a_prev_slice.shape (2, 2)
a_prev_slice.shape (5, 5)
a_prev_slice.shape (2, 2)
a_prev_slice.shape (2, 2)
All tests passed!

What do you see? How did it get that way?

2 Likes

Understood , I was slicing the channel rather than looping over it and I had done the same for ‘i’ as well.
After looping it rather than slicing , it worked properly. Thanks alot