Deep LearningDeep Learning Comparison Series
Max Pooling vs Average Pooling
Question 310 of 314 · Updated
On this page
- 1. What pooling does
- 2. Numerical example
- 3. Mathematical operations
- 4. Output shapes
- 5. What information is preserved?
- 6. Translation tolerance is not perfect invariance
- 7. Gradient behavior
- 8. PyTorch implementations
- 9. Computational and memory considerations
- 10. Common mistakes
- 11. When should you use each?
- 12. Quick comparison
Max pooling keeps the strongest activation in a local region; average pooling keeps the region's mean activation. Both reduce a CNN feature map's spatial dimensions, but preserve different information.
1. What pooling does#
A CNN produces feature maps: grids of activation values showing how strongly learned filters respond at different image locations. Pooling slides a small window over each map and replaces its values with one summary. Max pooling chooses the largest value; average pooling adds the values and divides by their count. Pooling normally operates independently on each channel, reducing height and width while preserving channel count.
2. Numerical example#
Consider a 2 × 2 region:
- Max pooling: max(1, 3, 2, 8) = 8.
- Average pooling: (1 + 3 + 2 + 8) / 4 = 3.5.
Max pooling preserves the strongest response; average pooling summarizes the whole region.
For a quieter region:
Its maximum is 3 and its average is 2. A small but meaningful edge or texture response can survive max pooling even when nearby activations are weak; average pooling reduces its influence by combining it with surrounding values. These values are illustrative: whether a high activation is useful depends on what the convolutional filter learned.
3. Mathematical operations#
For a pooling region R containing K values:
Max pooling
Average pooling
For 2D pooling, the window has height kH and width kW. With non-overlapping 2 × 2 windows and stride 2, each output summarizes four input values. A smaller stride creates overlapping windows.
4. Output shapes#
For input (N, C, H, W), pooling normally returns (N, C, Hout, Wout). N is batch size, C is channels, and H/W are spatial dimensions.
For no padding, dilation 1, and floor rounding:
Here sH and sW are strides. For example, (8, 64, 32, 32) pooled with a 2 × 2 window and stride 2 becomes (8, 64, 16, 16). Both methods produce the same shape. Padding and ceil-mode settings change exact dimensions; max pooling also supports dilation.
5. What information is preserved?#
Max pooling preserves strong local evidence. It is useful when a feature's presence matters more than its exact local intensity distribution. A strong edge, corner, or texture response can survive even if nearby activations are weak.
Average pooling preserves aggregate response. It retains information about how strongly a feature responds across the whole window. It can be useful when broad, distributed activation matters rather than a single peak.
Neither method preserves all information. Both compress a region into one number, so original spatial detail generally cannot be reconstructed.
6. Translation tolerance is not perfect invariance#
Pooling can make a representation less sensitive to small shifts. If a strong activation moves within the same pooling window, max pooling may produce the same output. Average pooling can also be stable for some shifts when the same values remain in a window.
But pooling does not guarantee translation invariance:
- A feature crossing a window boundary may move into another output cell.
- A shift larger than the window changes pooled locations.
- Stride and window alignment matter.
- Average pooling can change when values enter or leave a window.
Pooling offers a degree of local shift tolerance, not perfect position independence.
7. Gradient behavior#
Max pooling: With a unique maximum, the gradient flows to the input element that won; other elements in that window receive zero gradient from that pooled output. For tied maxima, framework-specific tie handling determines the gradient path.
Average pooling: The gradient is distributed across the elements. For a four-element window, each receives one quarter of the output gradient under ordinary unpadded averaging.
Thus max pooling creates sparse, winner-focused gradient paths, while average pooling spreads learning signals more evenly. With overlapping windows, an input may receive contributions from multiple pooled outputs.
8. PyTorch implementations#
Use nn.MaxPool2d and nn.AvgPool2d for standard 2D feature maps:
PYTHONimport torch import torch.nn as nn x = torch.tensor([[[[1.0, 3.0], [2.0, 8.0]]]]) max_pool = nn.MaxPool2d(kernel_size=2, stride=2) avg_pool = nn.AvgPool2d(kernel_size=2, stride=2) print(max_pool(x)) # tensor([[[[8.]]]]) print(avg_pool(x)) # tensor([[[[3.5000]]]])
A CNN can use either operation after convolution and activation:
PYTHONclass CNNWithMaxPool(nn.Module): def __init__(self, num_classes=10): super().__init__() self.features = nn.Sequential( nn.Conv2d(3, 32, kernel_size=3, padding=1), nn.ReLU(), nn.MaxPool2d(kernel_size=2, stride=2), nn.Conv2d(32, 64, kernel_size=3, padding=1), nn.ReLU(), nn.MaxPool2d(kernel_size=2, stride=2), ) self.classifier = nn.Sequential( nn.Flatten(), nn.Linear(64 * 8 * 8, num_classes), ) def forward(self, x): return self.classifier(self.features(x))
For a 32 × 32 input, two 2 × 2, stride-2 pooling layers reduce spatial dimensions to 8 × 8. Replacing MaxPool2d with AvgPool2d keeps the shape but changes values and gradients.
Inspect shapes directly:
PYTHONx = torch.randn(8, 64, 32, 32) pool = nn.MaxPool2d(kernel_size=2, stride=2) print(pool(x).shape) # torch.Size([8, 64, 16, 16])
9. Computational and memory considerations#
Both operations reduce spatial size and therefore activation data passed to later layers. For a fixed window, both perform work proportional to the number of values they examine.
- Max pooling compares values and retains the winning value or index needed for backward computation.
- Average pooling accumulates values and scales the result; backward distributes gradients across the window.
- Both reduce downstream activation memory and computation when they reduce feature-map dimensions.
- Actual latency depends on tensor layout, hardware kernels, window size, precision, and implementation. Benchmark on the target device rather than assuming one is always faster.
Max pooling can optionally return indices, useful with max unpooling when an architecture needs to route activations back to winning locations.
10. Common mistakes#
- Assuming pooling changes channels: Standard 2D pooling changes spatial dimensions, not channel count.
- Confusing kernel size with stride: A 2 × 2 kernel with stride 1 creates overlapping windows; stride 2 usually creates non-overlapping windows.
- Calling pooling perfectly translation-invariant: Window boundaries and stride still matter.
- Assuming average pooling always blurs the whole image: It averages feature activations within each local window; the effect depends on the learned feature map.
- Ignoring padding behavior: Average pooling has count_include_pad; padded zeros may be included in the divisor. Max pooling uses negative-infinity padding in its documented operation.
- Assuming max pooling always wins: It can discard useful evidence about the broader activation pattern.
- Comparing mismatched configurations: Use the same kernel, stride, padding, and rounding settings when evaluating the two methods.
11. When should you use each?#
Choose max pooling when strong local feature presence is important, a distinctive edge/corner/texture response should survive, or winner-focused gradient routing is useful.
Choose average pooling when overall activation level matters, evidence is distributed across a region, smoothing local variation is desirable, or you want gradients distributed across contributing values.
There is no universally best choice. Compare validation performance, robustness to shifts and noise, latency, and memory on the actual task. Consider alternatives such as strided convolutions, adaptive pooling, or architectures that preserve higher-resolution features.
12. Quick comparison#
| Property | Max pooling | Average pooling |
|---|---|---|
| Operation | Largest value | Mean value |
| Main emphasis | Strongest local response | Overall local response |
| Gradient | Mainly to maximum location | Shared across window |
| Local shift tolerance | Often preserves a peak within a window | Depends on values and window |
| Detail lost | Non-maximum values | Individual values beyond their mean |
| Useful when | Feature presence matters | Aggregate activation matters |
Key takeaway: Max pooling keeps the strongest activation in each region; average pooling keeps the mean. Both reduce spatial dimensions but encode different summaries. Choose based on the feature evidence your CNN needs to retain, then validate experimentally.
References
- 1.PyTorch MaxPool2d documentationdocs.pytorch.org
- 2.PyTorch AvgPool2d documentationdocs.pytorch.org
- 3.PyTorch max_pool2d functional documentationdocs.pytorch.org
- 4.PyTorch avg_pool2d functional documentationdocs.pytorch.org
Related questions
Explore other topics:Machine LearningLarge Language Models