Pooling and Receptive Fields
A unit deep inside a CNN "sees" only part of the input image: its receptive field. Early units see a few pixels, and every layer, stride and pooling operation enlarges the field. Knowing how fast it grows tells you whether a network can, in principle, relate distant parts of an image, and pooling is the cheapest way to grow it while adding a little invariance to small shifts.
Pooling
Max pooling outputs the largest value in each window; average pooling outputs the mean. A window with stride 2 halves height and width and has no parameters. Max pooling's gradient goes only to the winning position (a "switch"), like the max gate in Computational Graphs.
Global average pooling averages each channel over the whole map, turning a tensor into a -vector before the classifier; it replaced large dense heads in modern CNNs.
import numpy as np
X = np.array([[1, 3, 2, 0],
[4, 6, 1, 1],
[0, 2, 9, 5],
[1, 1, 3, 7]], dtype=float)
blocks = X.reshape(2, 2, 2, 2).transpose(0, 2, 1, 3) # (row block, col block, 2, 2)
print("max pool:\n", blocks.max(axis=(2, 3)))
print("avg pool:\n", blocks.mean(axis=(2, 3)))
shifted = np.roll(X, 1, axis=1); shifted[:, 0] = 0
print("max pool of the image shifted right by one pixel:\n", shifted.reshape(2, 2, 2, 2).transpose(0, 2, 1, 3).max(axis=(2, 3)))
print("global average pool:", X.mean())max pool:
[[6. 2.]
[2. 9.]]
avg pool:
[[3.5 1. ]
[1. 6. ]]
max pool of the image shifted right by one pixel:
[[4. 6.]
[1. 9.]]
global average pool: 2.875
Shifting the image by one pixel changed three of the four pooled values. The 9 survived because it stayed inside its window: max pooling is robust to shifts that keep a maximum within its window, so it provides only partial invariance to small shifts. Stacking several pooling stages makes the invariance stronger.
Receptive field arithmetic
Track two numbers through the layers: the receptive field (in input pixels) and the jump (distance in input pixels between adjacent units of the current layer). Starting from , , a layer with kernel and stride gives
Stacking stride-1 convolutions grows by only 2 per layer, while every stride-2 layer doubles the growth rate of all later layers.
def receptive_field(layers):
r, j = 1, 1
for name, k, s in layers:
r, j = r + (k - 1) * j, j * s
print(f"{name:14s} k={k} s={s}: receptive field {r:4d}, jump {j}")
return r
print("five 3x3 convs, stride 1:")
receptive_field([(f"conv{i}", 3, 1) for i in range(1, 6)])
print("VGG-style with pooling:")
receptive_field([("conv", 3, 1), ("conv", 3, 1), ("pool", 2, 2), ("conv", 3, 1), ("conv", 3, 1), ("pool", 2, 2), ("conv", 3, 1), ("conv", 3, 1), ("pool", 2, 2)])five 3x3 convs, stride 1:
conv1 k=3 s=1: receptive field 3, jump 1
conv2 k=3 s=1: receptive field 5, jump 1
conv3 k=3 s=1: receptive field 7, jump 1
conv4 k=3 s=1: receptive field 9, jump 1
conv5 k=3 s=1: receptive field 11, jump 1
VGG-style with pooling:
conv k=3 s=1: receptive field 3, jump 1
conv k=3 s=1: receptive field 5, jump 1
pool k=2 s=2: receptive field 6, jump 2
conv k=3 s=1: receptive field 10, jump 2
conv k=3 s=1: receptive field 14, jump 2
pool k=2 s=2: receptive field 16, jump 4
conv k=3 s=1: receptive field 24, jump 4
conv k=3 s=1: receptive field 32, jump 4
pool k=2 s=2: receptive field 36, jump 8
Two 3x3 versus one 5x5
Two stacked convolutions have the same receptive field as one () but use weights per channel pair instead of 25, and include an extra nonlinearity. Three layers match a with 27 instead of 49. This is the design argument behind VGG and most later CNNs.
Effective receptive field
The theoretical field is an upper bound. The gradient of a unit with respect to input pixels is concentrated near the center of its field, roughly Gaussian, so the effective receptive field is much smaller. Attention layers sidestep the issue: every token can attend to every other in one layer (Scaled Dot-Product Attention).
In software
nn.MaxPool2d(2),nn.AvgPool2d(2),nn.AdaptiveAvgPool2d(1)(global average pooling for any input size).- Dilated convolutions grow the receptive field exponentially without downsampling, used in WaveNet and segmentation.
- Receptive fields can be measured empirically by backpropagating from one output unit and looking at which input pixels get nonzero gradient.
Exercises
Exercise 1: Pool by hand
Max-pool with a window. Which entry receives the gradient?
Answer
Output 5; only the entry 5 gets the gradient.
Solution
The max gate routes the upstream gradient to its argmax; the other three entries get 0.
Exercise 2: Receptive field
Compute the receptive field after: conv stride 1, conv stride 2, conv stride 1.
Answer
Solution
: () () .
Exercise 3: Weight savings (code)
For 64 input and 64 output channels, compare the weight counts of one convolution and three stacked convolutions.
Answer
200,704 versus 110,592 (ignoring biases).
Solution
C = 64
print(C * C * 49, 3 * C * C * 9)200704 110592
Test yourself
How many parameters does a max-pooling layer have?
- 4
- 0
- one per channel
- depends on the image size
Two stacked stride-1 convolutions have the receptive field of…
- one
- one
- one
- one
In max pooling, the backward pass sends the gradient…
- equally to all inputs in the window
- only to the input that had the maximum
- to no input
- to the minimum input