Global Average Pooling (GAP) takes average pooling to its extreme: instead of a small local window, it averages an entire feature map down to a single number โ one value per channel. This single change dramatically reduces parameters compared to flattening and fully connecting, and has become standard in many modern CNN architectures.
Formula
For each channel \(c\), average every single spatial value in that entire feature map โ collapsing a \((C, H, W)\) tensor down to just \((C, 1, 1)\), or equivalently a flat vector of length \(C\).
Why This Matters: The Parameter Savings
Consider a final feature map of shape \((512, 7, 7)\) before a classification head with 1,000 output classes. Flattening and fully connecting (see Flattening and Fully Connected Layer) would need a weight matrix of shape \((1000, 512\times7\times7) = (1000, 25{,}088)\) โ over 25 million parameters, just for this one layer. Global Average Pooling instead collapses \((512,7,7)\) directly to a 512-length vector, so the following fully-connected layer only needs a \((1000, 512)\) weight matrix โ about 512,000 parameters, roughly 50 times fewer.
Numerical Example
A single 3ร3 feature map for one channel: \(\begin{bmatrix}2&4&6\\8&1&3\\5&7&9\end{bmatrix}\). GAP for this channel: \(\frac{2+4+6+8+1+3+5+7+9}{9} = \frac{45}{9}=5\) โ this one number becomes that channel's entire contribution to the classification head, regardless of how large the original feature map was.
Code
import torch
import torch.nn as nn
x = torch.randn(1, 512, 7, 7) # a typical final feature map before classification
gap = nn.AdaptiveAvgPool2d(output_size=(1, 1)) # GAP is a special case: output size fixed to 1x1
output = gap(x)
print(output.shape) # torch.Size([1, 512, 1, 1])
flattened = output.flatten(1)
print(flattened.shape) # torch.Size([1, 512]) -- ready for a small final Linear layer
The Additional Benefit: Reduced Overfitting
Because GAP eliminates tens of millions of parameters that a flatten-plus-fully-connected approach would otherwise introduce, it also substantially reduces overfitting risk โ fewer parameters generally means less capacity to memorize training-specific noise (see Overfitting), an important practical advantage on top of the raw efficiency gain.
Common Mistakes
- Assuming GAP discards useful spatial information without any tradeoff โ it does discard exact spatial layout within each feature map, which is a genuine, deliberate tradeoff; the substantial parameter and overfitting reduction is generally judged well worth it for final classification layers.
- Confusing GAP with regular average pooling โ regular average pooling uses a small, fixed local window; GAP's "window" is the entire feature map, collapsing all spatial dimensions to exactly 1ร1.
Interview Relevance
Q: "Why do many modern CNN architectures use Global Average Pooling instead of flattening followed by a large fully-connected layer?" Flattening a large final feature map and connecting it fully to the output layer requires an enormous weight matrix, proportional to the feature map's total spatial size times its channels. GAP collapses each channel's entire feature map to a single average value first, so the following fully-connected layer only needs weights proportional to the (much smaller) number of channels โ dramatically reducing parameters and overfitting risk, with only a modest, generally acceptable loss of exact spatial detail.
Practice Question
A final feature map has shape \((256, 14, 14)\), feeding into a classifier with 100 output classes. Compare the number of weight parameters needed for (a) flatten + fully connected, versus (b) Global Average Pooling + fully connected.