Pooling
Calculate a convolutional output and explain filters, stride, padding, pooling and residual connections.
THE IDEA
Pooling summarises a neighbourhood
Max pooling slides a window (usually 2×2, stride 2) and keeps only the largest value in each. The map gets smaller (4×4 becomes 2×2), later layers do less work, and small shifts of a pattern inside a window change nothing. Average pooling keeps the mean instead. Global average pooling averages each whole channel into a single number, which is a common way to end a convolutional network.
Pooling keeps the strongest response in each window, so the dot can move inside its window without changing the output. It is not completely shift-proof: crossing a window boundary changes it.
Predict first. What is max pooling over the window [1, 4, 2, 3]?
Chapter context
A convolutional layer reuses a local filter across positions. Weight sharing encodes a structural assumption about repeated local patterns while greatly reducing parameters compared with a fully connected map over all pixels.
TRY IT YOURSELF
Part 1. Do it hands-on
Pool a grid by hand, then test what shifting does. The Python part writes max, average and global pooling.
Pooling keeps the strongest response in each window, so the dot can move inside its window without changing the output. It is not completely shift-proof: crossing a window boundary changes it.
- To do: Max-pool the 4×4 grid with a 2×2 window and stride 2. Type the four results in reading order (top-left, top-right, bottom-left, bottom-right)
- To do: A bright dot starts at (1, 1). Move it one pixel so the pooled 2×2 output does NOT change
- To do: Now move it one pixel so the pooled output DOES change
Part 2. Do it in code
- max_pool2d(x, k, s=None): x is a 2-D array. Take the maximum of every k×k window, moving by s (default s = k). Windows must fit entirely inside x (so leftover rows and columns are ignored). Return a 2-D array.
- avg_pool2d(x, k, s=None): the same with the mean.
- global_avg_pool(x): x has shape (C, H, W). Return a vector of length C with the mean of each channel.
Stuck, or something not working? You can come back to it any time.
CHECK YOUR UNDERSTANDING
What is max pooling over [1,4,2,3]?
Chapter reference: math, worked example & code
This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.
Math and notation
For one spatial dimension, output size is
Here n is input size, k kernel size, p padding, d dilation and s stride. Cross-correlation is . A residual block has form when shapes agree.
Worked example
For input [1,2,3,4] and kernel [1,−1], stride-one valid cross-correlation returns [−1,−1,−1]. Separately, n=7,k=3,p=1,s=2,d=1 gives floor((7+2−2−1)/2)+1=4 output positions.
Reference code
The following example uses the Python standard library and does not call a remote service.
x,w = [1,2,3,4],[1,-1]
y = [sum(x[t+i]*w[i] for i in range(len(w))) for t in range(len(x)-len(w)+1)]
assert y == [-1,-1,-1]
n,k,p,s,d = 7,3,1,2,1
assert (n+2*p-d*(k-1)-1)//s+1 == 4
Python examples are provided for study and local execution; this page does not execute Python.
Common mistakes
Padding does not reveal missing real pixels. Kernel flipping conventions change calculations. A residual shortcut needs a defined shape-compatible path.