TExcelTAD
Home › Math for Deep Learning & LLMs › Pooling and Receptive Fields

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 2×22 \times 2 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 C×H×WC \times H \times W tensor into a CC-vector before the classifier; it replaced large dense heads in modern CNNs.

python · try it yourself
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 rr (in input pixels) and the jump jj (distance in input pixels between adjacent units of the current layer). Starting from r0=1r_0 = 1, j0=1j_0 = 1, a layer with kernel kk and stride ss gives

rl=rl−1+(kl−1) jl−1,jl=jl−1 slr_l = r_{l-1} + (k_l - 1)\,j_{l-1}, \qquad j_l = j_{l-1}\,s_l

Stacking 3×33 \times 3 stride-1 convolutions grows rr by only 2 per layer, while every stride-2 layer doubles the growth rate of all later layers.

python · try it yourself
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 3×33 \times 3 convolutions have the same receptive field as one 5×55 \times 5 (r=5r = 5) but use 2⋅9=182 \cdot 9 = 18 weights per channel pair instead of 25, and include an extra nonlinearity. Three 3×33 \times 3 layers match a 7×77 \times 7 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 (1532)\begin{pmatrix} 1 & 5 \\ 3 & 2 \end{pmatrix} with a 2×22 \times 2 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 3×33 \times 3 stride 1, conv 3×33 \times 3 stride 2, conv 3×33 \times 3 stride 1.

Answer
Solution

rr: 1→31 \to 3 (j=1j = 1) →5\to 5 (j=2j = 2) →5+2⋅2=9\to 5 + 2 \cdot 2 = 9.

Exercise 3: Weight savings (code)

For 64 input and 64 output channels, compare the weight counts of one 7×77 \times 7 convolution and three stacked 3×33 \times 3 convolutions.

Answer

200,704 versus 110,592 (ignoring biases).

Solution
python · try it yourself
C = 64
print(C * C * 49, 3 * C * C * 9)
200704 110592

Test yourself

How many parameters does a 2×22 \times 2 max-pooling layer have?

  1. 4
  2. 0
  3. one per channel
  4. depends on the image size

Two stacked 3×33 \times 3 stride-1 convolutions have the receptive field of…

  1. one 3×33 \times 3
  2. one 5×55 \times 5
  3. one 6×66 \times 6
  4. one 9×99 \times 9

In max pooling, the backward pass sends the gradient…

  1. equally to all inputs in the window
  2. only to the input that had the maximum
  3. to no input
  4. to the minimum input