Max Pooling - CNNs & Computer Vision
Implement Max Pooling in Python with NumPy. medium exercise with examples, hints, and instant feedback. No setup or signup required.
Max Pooling
What you'll build
Non-overlapping 2D max pooling on a single-channel (H, W) feature map: split it into pool_size x pool_size windows (stride = window size) and keep the maximum of each.
Background
Pooling shrinks feature maps, which cuts compute in later layers, grows the receptive field, and makes the network a little tolerant to small shifts (a feature that moves by one pixel inside a window gives the same max). Classic CNNs (LeNet, VGG, the ResNet stem) use 2x2 or 3x3 max pooling; many modern networks use strided convolutions instead.
Formula
With p = pool_size (window size and stride):
out[i, j] = max(image[i*p:(i+1)*p, j*p:(j+1)*p])
H_out = H // p
W_out = W // p
Inputs
image: 2D array of shape(H, W)(ints or floats; a nested list is fine).pool_size: positive intp, default2.
Expected return format
A NumPy array of shape (H // p, W // p) holding the window maxima (same dtype as the input; float inputs stay floats).
Edge cases and conventions
- If
HorWis not a multiple ofp, the leftover rows/columns at the bottom/right are dropped (floor), likeF.max_pool2dwith no padding andceil_mode=False. - Negative values: the max of
[-1, -2]is-1; don't initialise the output with zeros and compare.
Constraints
NumPy only. A double loop over windows is fine; the reshape trick is fully vectorised.
Follow-up
- In the backward pass, which input receives the gradient of each output?
- How do average pooling and global average pooling differ, and why did global average pooling replace big fully connected heads?
Examples
Input: [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]] Output: [[6, 8], [14, 16]]
The four 2x2 windows have maxima 6, 8, 14 and 16.
Input: image = np.arange(30).reshape(5, 6) * 0.5, pool_size = 3 Output: [[7.0, 8.5]]
5 // 3 = 1 row and 6 // 3 = 2 columns of windows; the last two rows are dropped.
Practice in Python
import numpy as np
def max_pool2d(image, pool_size=2):
"""
Apply 2D max pooling with window = stride = pool_size.
Args:
image: Input feature map, shape (H, W)
pool_size: Window size and stride p (default 2)
Returns:
output: Array of shape (H // p, W // p); leftover rows/columns are dropped
"""
# Your code here
pass
The interactive editor loads with Python and NumPy. Run your implementation against 6 tests, or use the progressive hints.