Course 4, Week 1, Exercise 8

I have been having a dimension mismatch in optional exercise 8 in the first of two programming assignments. I cannot get “mask” to broadcast. Here are the troublesome lines of code and the error message:

{moderator edit - solution code removed}

np.random.seed(1)
A_prev = np.random.randn(5, 5, 3, 2)
hparameters = {“stride” : 1, “f”: 2}
A, cache = pool_forward(A_prev, hparameters)
print(A.shape)
print(cache[0].shape)
dA = np.random.randn(5, 4, 2, 2)

dA_prev1 = pool_backward(dA, cache, mode = “max”)
print(“mode = max”)
print('mean of dA = ', np.mean(dA))
print('dA_prev1[1,1] = ', dA_prev1[1, 1])
print()
dA_prev2 = pool_backward(dA, cache, mode = “average”)
print(“mode = average”)
print('mean of dA = ', np.mean(dA))
print('dA_prev2[1,1] = ', dA_prev2[1, 1])

assert type(dA_prev1) == np.ndarray, “Wrong type”
assert dA_prev1.shape == (5, 5, 3, 2), f"Wrong shape {dA_prev1.shape} != (5, 5, 3, 2)"
assert np.allclose(dA_prev1[1, 1], [[0, 0],
[ 5.05844394, -1.68282702],
[ 0, 0]]), “Wrong values for mode max”
assert np.allclose(dA_prev2[1, 1], [[0.08485462, 0.2787552],
[1.26461098, -0.25749373],
[1.17975636, -0.53624893]]), “Wrong values for mode average”
print(“\033[92m All tests passed.”)
(5, 4, 2, 2)
(5, 5, 3, 2)

ValueError Traceback (most recent call last)
in
7 dA = np.random.randn(5, 4, 2, 2)
8
----> 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)
102
103 # Set dA_prev to be dA_prev + (the mask multiplied by the correct entry of dA) (≈1 line)
→ 104 dA_prev[i, vert_start: vert_end, horiz_start: horiz_end, c] += mask * dA[i, h, w, c]
105
106 elif mode == “average”:

ValueError: non-broadcastable output operand with shape (2,2) doesn’t match the broadcast shape (2,2,2)

Can you help me please?

The key point is that pooling layers work differently than conv layers. In a conv layer, the filter matches all the input channels. But in a pooling layer, each channel is handled independently one at a time. That means that the number of output channels always equals the input channels for a pooling layer. I think that’s probably where the issue is.