Search

Search papers and lessons

Max Pooling

Level 2 · 9 min read

Max pooling shrinks a grid of numbers by keeping only the largest value in each small window, so the output keeps strong responses and tolerates small shifts.

On this page

  • Colour = topic mix
  • Linear algebra
  • Architectures
  • Discrete math
  • Sign in to see your progress

TL;DR

Max pooling takes a grid of numbers (a Matrix), cuts it into small non-overlapping windows, and keeps the largest value in each window. The result is a smaller grid that records where the strongest responses were, without caring about their exact position inside the window. It has no weights to learn.

Where you'll see it: between the convolution blocks of image classifiers such as early convolutional networks, where it halves the width and height of each feature map after every stage.

Intuition

Imagine a grid of numbers that says how strongly each small patch of an image matched a pattern, say a vertical edge. A high number means "the edge is here". For many questions a network asks, the exact pixel where the edge sits does not matter. What matters is that an edge exists somewhere in this region.

Max pooling answers that question directly. Take each small region, keep the strongest signal, and throw away the rest. The grid gets smaller, so later layers do less work, and each remaining value now covers a wider piece of the image. Because the output is the maximum, moving the feature slightly inside the window leaves the output unchanged. That gives the network a little tolerance to small shifts, which is useful when the same object can appear a few pixels to the left or right.

The pipeline usually looks like this:

flowchart LR
  A["Input image"] --> B["Convolution (feature detection)"]
  B --> C["Nonlinearity (ReLU)"]
  C --> D["Max pooling (downsample)"]
  D --> E["Next convolution layer"]

Convolution finds patterns, and pooling compresses the map of where those patterns are strongest.

The math

Setup

Let XX be an input grid of height HH and width WW, with entries xi,jx_{i,j}. Choose a window size kk (the side of a square window) and a stride ss (how far the window moves between placements). Pooling produces an output YY with entries

ya,b=max⁡0≤p<k, 0≤q<k  xas+p,  bs+q.y_{a,b} = \max_{0 \le p \lt k,\ 0 \le q \lt k} \; x_{a s + p,\; b s + q}.

For each output position (a,b)(a, b), the window covers the input entries xas+p, bs+qx_{as+p,\,bs+q} with offsets pp and qq running from 00 to k−1k-1. The output takes the largest of those k2k^2 values.

The output height and width follow from how many full windows fit:

Hout=⌊H−ks⌋+1,Wout=⌊W−ks⌋+1.H_{\text{out}} = \left\lfloor \frac{H - k}{s} \right\rfloor + 1, \qquad W_{\text{out}} = \left\lfloor \frac{W - k}{s} \right\rfloor + 1.

The symbol ⌊⋅⌋\lfloor \cdot \rfloor means rounding down to a whole number. With k=2k = 2 and s=2s = 2 (the usual setting), windows do not overlap and each dimension is halved when it is even.

Common settings

When s=ks = k the windows tile the grid with no overlap. When s<ks \lt k they overlap. AlexNet used overlapping windows of size 3 with stride 2. Padding can be added around the input to control the output size, but most pooling layers use no padding.

Gradient through the max

Pooling has no weights, but it is still part of the computation that Backpropagation must differentiate. Let LL be the loss and let ∂L/∂ya,b\partial L / \partial y_{a,b} be the gradient arriving at an output. Only one input in each window influences its output: the one that was the maximum. Call that position (a∗,b∗)(a^\ast, b^\ast) for the window. Then

∂L∂xi,j={∂L∂ya,bif (i,j) is the arg-max of window (a,b),0otherwise.\frac{\partial L}{\partial x_{i,j}} = \begin{cases} \dfrac{\partial L}{\partial y_{a,b}} & \text{if } (i,j) \text{ is the arg-max of window } (a,b), \\[6pt] 0 & \text{otherwise.} \end{cases}

The gradient is copied to the winning position and zero is sent to the others. Ties are handled by choosing one winner, which is a convention rather than something the math decides. When windows overlap (s<ks \lt k), one input can win in several windows, and its gradient is the sum of the gradients from all of them.

Worked examples

Example
Pooling a 4 by 4 grid

Take the input with window k=2k = 2 and stride s=2s = 2:

X=(1321485022971634).X = \begin{pmatrix} 1 & 3 & 2 & 1 \\ 4 & 8 & 5 & 0 \\ 2 & 2 & 9 & 7 \\ 1 & 6 & 3 & 4 \end{pmatrix}.
  1. Output size: Hout=⌊(4−2)/2⌋+1=2H_{\text{out}} = \lfloor (4 - 2)/2 \rfloor + 1 = 2, so the output is 2×22 \times 2.
  2. Top-left window holds {1,3,4,8}\{1, 3, 4, 8\}. Maximum is 88.
  3. Top-right window holds {2,1,5,0}\{2, 1, 5, 0\}. Maximum is 55.
  4. Bottom-left window holds {2,2,1,6}\{2, 2, 1, 6\}. Maximum is 66.
  5. Bottom-right window holds {9,7,3,4}\{9, 7, 3, 4\}. Maximum is 99.
  6. Result: Y=(8569)Y = \begin{pmatrix} 8 & 5 \\ 6 & 9 \end{pmatrix}. The 4×44 \times 4 grid of 16 numbers became 4 numbers, and the largest value in the image (9) survived.
Example
A one-dimensional backward pass

Use a one-dimensional input [3,1,4,1,5,9][3, 1, 4, 1, 5, 9] with window k=2k = 2 and stride s=2s = 2. The forward pass gives windows (3,1)(3, 1), (4,1)(4, 1), (5,9)(5, 9) and outputs [3,4,9][3, 4, 9].

Suppose the loss gradients arriving at the outputs are [1,2,3][1, 2, 3].

  1. Window one: the maximum 33 is at position 1 (counting from 1). It receives gradient 11. Position 2 receives 00.
  2. Window two: the maximum 44 is at position 3. It receives gradient 22. Position 4 receives 00.
  3. Window three: the maximum 99 is at position 6. It receives gradient 33. Position 5 receives 00.
  4. Input gradient: [1,0,2,0,0,3][1, 0, 2, 0, 0, 3].
  5. Interpretation: only the winning entries affect the loss, so only they are updated. The value 11 at position 2 changed nothing in the output, so nudging it would change nothing either. This is why max pooling is cheap in both directions.

Tradeoffs and failure modes

Max pooling is simple, fast and parameter-free. Its main cost is that it discards information: every value in a window except the largest is ignored, including how many strong responses there were. Its tolerance to shifts is also only local, because a shift that moves the peak into a neighbouring window still changes the output.

SituationProblemBetter choice
Need the average strength of a regionMax keeps only the peakAverage pooling
Output must keep the input size (segmentation, generation)Pooling shrinks the gridFewer or no pooling layers (padded convolutions keep size), or restore size later with upsampling or transposed convolution
Fine spatial detail matters (small objects)Repeated pooling blurs it awayFewer pooling layers, or dilated convolution
Want learned downsamplingMax has nothing to tuneStrided convolution with learned weights
Want invariance to global positionLocal windows keep position informationGlobal average or global max pooling over the whole map

A common failure is stacking too many pooling layers on a small input. Each halving removes spatial resolution, and after a few steps a 28×2828 \times 28 image has only a few cells left to describe it. On ordering: the usual sequence is convolution, then activation, then pooling. For a non-decreasing activation such as ReLU, taking the max and applying the activation commute, so pooling first gives the same result with fewer activations to compute. For activations that are not monotone the order changes the answer.

History

Pooling in neural networks grew out of earlier work on local averaging. Fukushima's Neocognitron (1980) used layers that combined nearby responses to build tolerance to position. LeCun and colleagues' convolutional networks of the late 1980s and 1990s used subsampling layers to shrink feature maps; the LeNet paper of 1998 is the standard reference for that design. The Riesenhuber and Poggio HMAX model of visual cortex (1999) argued that taking a maximum over a neighbourhood matched how some visual neurons respond, which helped motivate max pooling. It became standard after the 2012 AlexNet paper, which used overlapping max pooling in a large convolutional network trained on GPUs and won the ImageNet competition by a wide margin.

Check yourself

QuestionA 6×66 \times 6 input is pooled with window k=2k = 2 and stride s=2s = 2. What is the output size?Show answer

Hout=⌊(6−2)/2⌋+1=2+1=3H_{\text{out}} = \lfloor (6 - 2)/2 \rfloor + 1 = 2 + 1 = 3. The output is 3×33 \times 3, which is 9 numbers from 36. Windows do not overlap, and each one covers a 2×22 \times 2 block.

QuestionFor the window (1,7,3,2)(1, 7, 3, 2), which value does max pooling output, and which input entries receive zero gradient on the backward pass?Show answer

The output is 77. Only the entry 77 receives the incoming gradient; the entries 11, 33 and 22 receive zero gradient because a small change to them would not change the maximum.

QuestionWhy does max pooling give some tolerance to shifts of the input, and when does that tolerance break down?Show answer

If a feature moves by a pixel but stays inside the same window, the maximum is unchanged, so the output is unchanged. The tolerance breaks down when the peak moves across a window boundary; then the window containing it changes, and the output changes. It is also only local, so large shifts still matter.

QuestionA 3 by 3 window with stride 1 is applied to a 5×55 \times 5 grid. Compute the output size and say whether the windows overlap.Show answer

Hout=⌊(5−3)/1⌋+1=3H_{\text{out}} = \lfloor (5 - 3)/1 \rfloor + 1 = 3, so the output is 3×33 \times 3. The windows overlap heavily, since each window shares two columns and two rows with its neighbours.

QuestionA network needs its output to be the same size as its input for per-pixel labels. Why is max pooling a poor choice for that layer, and what would you use instead?Show answer

Pooling shrinks the grid, so the output no longer lines up with the input pixels. A transposed convolution (or plain upsampling) can restore resolution, or the network can use dilated convolutions to keep resolution while enlarging the receptive field. Max pooling is fine inside an encoder, but the decoder must put resolution back.

Sources