Week 1 - pool_forward

I’m getting this error:

ValueError                                Traceback (most recent call last)
<ipython-input-20-034ff9e73c4d> in <module>
      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))

<ipython-input-19-392b1d14c159> in pool_forward(A_prev, hparameters, mode)
     72                     print("A[",i,",",h,",",w,",",c,"]","vert:",vert_start,":",vert_end,"horiz:",horiz_start,":",horiz_end)
     73                     if (mode == "max"):
---> 74                         A[i, h, w, c] = np.max(a_prev_slice)
     75                     elif mode == "average":
     76                         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 did this to help debug, right after a_prev_slice = A_prev[vert_start:vert_end, horiz_start:horiz_end,:]

A[ 0 , 0 , 0 , 0 ] vert: 0 : 3 horiz: 0 : 3
A[ 0 , 0 , 0 , 1 ] vert: 0 : 3 horiz: 0 : 3
A[ 0 , 0 , 0 , 2 ] vert: 0 : 3 horiz: 0 : 3
A[ 0 , 0 , 1 , 0 ] vert: 0 : 3 horiz: 1 : 4
A[ 0 , 0 , 1 , 1 ] vert: 0 : 3 horiz: 1 : 4
A[ 0 , 0 , 1 , 2 ] vert: 0 : 3 horiz: 1 : 4
A[ 0 , 0 , 2 , 0 ] vert: 0 : 3 horiz: 2 : 5
A[ 0 , 0 , 2 , 1 ] vert: 0 : 3 horiz: 2 : 5
A[ 0 , 0 , 2 , 2 ] vert: 0 : 3 horiz: 2 : 5
A[ 0 , 1 , 0 , 0 ] vert: 1 : 4 horiz: 0 : 3
A[ 0 , 1 , 0 , 1 ] vert: 1 : 4 horiz: 0 : 3
A[ 0 , 1 , 0 , 2 ] vert: 1 : 4 horiz: 0 : 3
A[ 0 , 1 , 1 , 0 ] vert: 1 : 4 horiz: 1 : 4
A[ 0 , 1 , 1 , 1 ] vert: 1 : 4 horiz: 1 : 4
A[ 0 , 1 , 1 , 2 ] vert: 1 : 4 horiz: 1 : 4
A[ 0 , 1 , 2 , 0 ] vert: 1 : 4 horiz: 2 : 5
A[ 0 , 1 , 2 , 1 ] vert: 1 : 4 horiz: 2 : 5
A[ 0 , 1 , 2 , 2 ] vert: 1 : 4 horiz: 2 : 5
A[ 0 , 2 , 0 , 0 ] vert: 2 : 5 horiz: 0 : 3

The shape of your a_slice_prev is incorrect. You have handled it the same was as with a “conv” layer, but pooling handles the channel dimension differently, right? You end up not noticing, because the np.max just gives you a single number, but the input is not what it should be.