Course 4 week 1 assignment 1 exercise 8 issue

Dear community,

I have an issue with my code. Here is the error message:

TypeError Traceback (most recent call last)
7 dA = np.random.randn(5, 4, 2, 2)
----> 9 dA_prev1 = pool_backward(dA, cache, mode = “max”)
10 print(“mode = max”)
11 print('mean of dA = ', np.mean(dA))

in pool_backward(dA, cache, mode)
93 mask = create_mask_from_window((f,f))
—> 95 dA_prev[i, vert_start: vert_end, horiz_start: horiz_end, c] +=,distribute_value(a_prev_slice, a_prev_slice))
97 elif mode == “average”:

in distribute_value(dz, shape)
22 (n_H, n_W) = shape
23 average = dz/(n_H * n_W)
—> 24 a = average*np.ones((n_H,n_W))
25 print(a)

/opt/conda/lib/python3.7/site-packages/numpy/core/ in ones(shape, dtype, order)
206 “”"
→ 207 a = empty(shape, dtype, order)
208 multiarray.copyto(a, 1, casting=‘unsafe’)
209 return a

TypeError: only integer scalar arrays can be converted to a scalar index

Thanks in advance!

The arguments you are passing to distribute_value are incorrect. The first should be a scalar, which is one element of dA, and the second should be a “shape” tuple. You’re passing in a floating point tensor for both, which (as we see) does not end well.

Also note that there should be no case in which you use both mask and distribute_value in the same computation, right? The “create mask” routine is for the “max” pooling case and the “distribute value” is for the “average” pooling case. Please have a careful look at the explanations and then the comments in the template code.