Max pooling forward and backward
2x2 max pooling keeps the largest value in each window. In the backward pass, the gradient for a window goes only to the position that held the max.
Write maxpool(x, dout) for x of shape (H, W) with H and W even, 2x2 windows, stride 2. dout has shape (H/2, W/2). Return (out, dx). If a window has ties, send the gradient to the first max in row-major order.
def maxpool(x, dout)Understand the idea
2x2 max pooling splits the input into non-overlapping 2 by 2 windows and keeps the biggest value from each, halving height and width. In the backward pass, only the winning position actually affected the output, so it alone receives that window's gradient; the other three get 0. Like a relay race where only the runner who carried the baton gets the credit. Worked example: x = [[1, 3], [3, 2]], dout = [[10]]. The window max is 3, so out = [[3]]. The 3 appears at (0, 1) and (1, 0). Tie rule: the first max in row-major order, which is (0, 1). So dx = [[0, 10], [0, 0]]. Spreading 10 evenly as 2.5 each would be average pooling's backward pass, not max pooling. Rule: out[i, j] = max of x[2i:2i+2, 2j:2j+2]; dx gets dout[i, j] at the first argmax of that window and 0 elsewhere. Classic CNNs such as VGG use max pooling.