Exercise 4 - pool_forward

may i ask how to correct my mode=max and mode=average problem?


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)
46 # Use np.max and np.mean.
47 if mode == “max”:
—> 48 A[i, h, w, c] = np.max(a_prev_slice)
49 elif mode == “average”:
50 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

Hey @Lucy_Hui,
Your traceback suggests that you have selected a_prev_slice incorrectly. You can print the dimensions of a_prev_slice before passing it to np.max() to ensure that it has the expected dimensions. If you still face the issue, feel free to DM your code to me. I hope this helps.

Regards,
Elemento