Pooling
A pooling layer shrinks a feature map by summarizing each window of positions with a single number. It acts on each channel separately, so the number of channels does not change, and it has no learnable parameters. There are four common summaries:
- Subsampling keeps one value per window, which is what a convolution with stride does.
- Max pooling keeps the largest value.
- Average pooling, also called mean pooling, keeps the mean.
- Global average pooling (GAP) averages each channel over all of its positions and returns one number per channel, a vector of length .
With a window, stride , no padding and indices from , each channel of a max-pooled output is
and average pooling replaces the maximum by . Each spatial size becomes
The standard choice is . It halves the height and the width and keeps a quarter of the positions.Global average pooling usually sits after the last convolution and feeds a small dense layer. It works for any input size, and it makes the network invariant to circular shifts when every layer before it is equivariant to them.
In the backward pass, each method routes the upstream gradient differently:
- Max pooling sends it to the single position that held the maximum and sends zero to the rest of the window.
- Average pooling spreads evenly over the window.
- Subsampling passes it only to the kept position.
- Global average pooling gives each of the positions of channel the share .
Pooling serves three purposes:
- Larger receptive fields: it doubles the spacing between units, so every later convolution grows the receptive field twice as fast.
- Less computation: at a fixed channel count, it cuts the computation of every later layer by a factor of four.
- Partial invariance: max pooling makes the output partly invariant to small translations, because a maximum that moves within its window does not change the result.
Intuition
A convolutional network has to turn a large image into a small summary, such as one label. Convolutions alone barely shrink the representation, so the network needs a separate step that throws positions away. The question is which value to keep from each window. Keeping an arbitrary one, as subsampling does, ignores whatever happened at the other positions. Keeping the maximum asks whether the feature appears anywhere in the window. Keeping the mean asks how strongly it appears on average. The figure applies both to the same map, with dashed lines marking the four windows.
\usepackage{tikz}
\begin{document}
\definecolor{hot}{HTML}{D9483B}
\definecolor{cold}{HTML}{2F80ED}
\begin{tikzpicture}[x=0.75cm,y=0.75cm]
\foreach \r/\row in {0/{1,4,2,0},1/{3,2,5,1},2/{0,6,1,1},3/{2,1,3,7}} {
\foreach \v [count=\c from 0] in \row {
\draw (\c,-\r) rectangle (\c+1,-\r-1);
\node at (\c+0.5,-\r-0.5) {$\v$};
}
}
\draw[very thick,dashed] (2,0) -- (2,-4);
\draw[very thick,dashed] (0,-2) -- (4,-2);
\foreach \x/\y in {1/0,2/1,1/2,3/3} \draw[hot,very thick] (\x+0.1,-\y-0.1) rectangle (\x+0.9,-\y-0.9);
\node at (2,0.6) {input};
\draw[->,thick] (4.4,-1.2) -- (6.1,-0.6);
\draw[->,thick] (4.4,-2.8) -- (6.1,-3.4);
\foreach \v [count=\c from 0] in {4,5} {\draw[hot,very thick] (6.5+\c,0) rectangle (7.5+\c,-1); \node at (7+\c,-0.5) {$\v$};}
\foreach \v [count=\c from 0] in {6,7} {\draw[hot,very thick] (6.5+\c,-1) rectangle (7.5+\c,-2); \node at (7+\c,-1.5) {$\v$};}
\node[right] at (8.7,-1) {max pooling};
\foreach \v [count=\c from 0] in {2.5,2} {\draw[cold,very thick] (6.5+\c,-2.6) rectangle (7.5+\c,-3.6); \node at (7+\c,-3.1) {$\v$};}
\foreach \v [count=\c from 0] in {2.25,3} {\draw[cold,very thick] (6.5+\c,-3.6) rectangle (7.5+\c,-4.6); \node at (7+\c,-4.1) {\small $\v$};}
\node[right] at (8.7,-3.6) {average pooling};
\end{tikzpicture}
\end{document}Pooling trades resolution for reach. After a pooling, neighboring units sit two input pixels apart, so the next convolution covers twice the distance it covered before. It also leaves a quarter of the positions, which makes room to double the number of channels at the same cost. At the very end of a classifier, global average pooling discards position entirely. The network then reports how strongly each feature appeared anywhere in the image, which is the question a classifier needs answered.
Properties
(Gradient Routing) Max pooling sends each upstream gradient to one winner per window, while average pooling splits it evenly.
Write for the upstream gradient and for the gradient with respect to an input. For non-overlapping windows, input lies in the window of output with and , and it affects no other output. If the maximum in each window is unique, then
for max and average pooling respectively. Max pooling must remember which position won during the forward pass. When windows overlap (), an input can belong to several windows, and its gradient is the sum of its shares from each of them. Under max pooling, three of every four inputs receive zero gradient on a given step.
(Partial Invariance) Max pooling ignores a shift that keeps the maximum inside its window but not one that moves it across a window boundary.
Consider a single active pixel on a background of zeros. If it moves within its window, the max-pooled map does not change. If it moves one pixel across a window boundary, its value jumps to the neighboring output. A max pool is therefore invariant to some one-pixel shifts and not to others, depending on where the feature sits relative to the window grid. Stacking several pooling layers widens the range of shifts that leave the output unchanged, but the invariance stays approximate. Average pooling changes under every shift that crosses a boundary. The change is small for a smooth feature, while a single active pixel moves its whole contribution to the neighboring output.
(Pooling Before Doubling Channels) Halving the resolution and then doubling the channels keeps the computation of the next convolution unchanged.
A convolutional layer performs one multiply-accumulate for every combination of output position, kernel offset, input channel and output channel, in total. A pooling divides by , and doubling both and multiplies the count by . The feature map itself holds half as many values as before the pooling. This is why convolutional networks halve the resolution and double the channels at the same points. Each later layer costs about the same as the one before, while its receptive field grows twice as fast.
(Max Versus Average) Max pooling preserves the strongest local response, and average pooling preserves the overall level of a region.
Max pooling suits features whose presence matters more than their exact location, such as an edge somewhere in a window. It keeps sharp, sparse responses that averaging would dilute, and it tolerates small shifts. Average pooling keeps information about the whole window, which suits smooth changes and texture-like statistics. It is the usual choice for the final global pooling. Neither is better in general. On a small edge-detection dataset, max pooling can win when training data are scarce, and the two can match once data are plentiful.
(Max Pooling Is Biased Under Noise) The maximum of noisy values is larger on average than the maximum of their means.
For any random values, , and the gap is positive when either value can be the larger one, as with independent noise. Using , two independent noise terms with distribution give
A max pool applied to a flat, noisy region therefore reports a positive value even though the clean signal is zero. Average pooling is unbiased for the clean mean and halves the noise variance. In a denoising network this bias shifts the outputs, which the later layers or the skip connections must correct.
Examples
(Easy) Three Ways to Downsample
A single-channel feature map is pooled with windows and stride :
Compute the subsampled, max-pooled and average-pooled outputs. , , and . Subsampling keeps the top-left entry of each window, the maximum keeps the largest, and the average divides each sum by : Subsampling misses every large value in this map, which is why pooling usually keeps the maximum or the mean instead.
The four windows are
(Medium) Backpropagating Through Pooling
The pooled outputs from the previous example receive the upstream gradient
Find for max pooling and for average pooling. Max pooling sends each entry of to the position of the maximum in its window: the at , the at , the at and the at . Average pooling gives every position a quarter of its window's gradient: Each window of either matrix sums to the matching entry of , so both matrices sum to , as does. Max pooling concentrates the gradient on four inputs, and average pooling spreads it over all sixteen.
(Medium) Global Average Pooling Saves Parameters
A classifier for images applies a convolution with filters and padding , a max pool, a convolution with filters and padding , and another max pool. A dense layer then produces class scores.
Trace the shapes and count the parameters, once with global average pooling before the dense layer and once with flattening instead. with parameters. Pooling gives . The second convolution gives with parameters, and pooling gives . Global average pooling reduces this to a vector of length , and the dense layer has parameters, for in total. Flattening instead gives a vector of length , and the dense layer needs parameters, for in total. The flattened version ties each weight to one position, so it also loses the invariance that the pooled version has.
The first convolution gives
(Hard) Max Pooling a Noisy Flat Region
A denoising network sees a region of an image whose clean value is everywhere. Each pixel carries independent noise with distribution . A layer pools pairs of pixels.
What are the mean and variance of a pooled value under average pooling and under max pooling of two pixels? , with mean and variance . It is unbiased and halves the noise variance. Max pooling gives . The difference has distribution , and the mean absolute value of is , so the mean is . Its second moment is , because the cross term is zero by symmetry. The variance is therefore . Max pooling reports about where the truth is , and it removes less noise than averaging.
Average pooling gives
See Also
- 5.2.1a. Receptive Field, for the receptive field rule that pooling speeds up.
- 5.2.1b. Equivariance and Invariance, for the invariance that pooling adds.
- 5.2.3. Upsampling, including max unpooling, which reverses a max pool.
- 5.2.4. CNNs, where pooling alternates with convolutions.
- 2.3. Resampling, the image-processing view of downsampling.