PyTorch nn.MaxPool2d Function
PyTorch torch.nn Reference Manual
torch.nn.MaxPool2d is a module in PyTorch for two-dimensional max pooling.
Pooling layers can reduce the spatial size of feature maps, lower computational cost, and provide a degree of translation invariance.
Function definition
torch.nn.MaxPool2d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)
Parameter Description:
kernel_size(int or tuple): The size of the pooling window.stride(int or tuple): The stride at which the pooling window moves. Defaults tokernel_size.padding(int or tuple): Padding size at the input edges. Defaults to 0.dilation(int or tuple): The spacing between window elements. Defaults to 1.return_indices(bool): Whether to return the indices of the maximum values. Used by MaxUnpool2d. Defaults to False.ceil_mode(bool): Whether to use ceil instead of floor when computing the output size. Defaults to False.
Usage examples
Example 1: Basic usage
Create a max pooling layer:
Example
import
torch
import
torch.
nn
as
nn
Create a max pooling layer: window 2x2, stride 2
max_pool
=
nn.
MaxPool2d
(
kernel_size
=
2
,
stride
=
2
)
Create an input tensor
input_tensor
=
torch.
randn
(
1
,
1
,
4
,
4
)
(
"Input:n"
,
input_tensor.
squeeze
(
)
.
tolist
(
)
)
Forward propagation
output
=
max_pool
(
input_tensor
)
(
"nOutput:n"
,
output.
squeeze
(
)
.
tolist
(
)
)
(
"nInput shape:"
,
input_tensor.
shape
)
(
"Output shape:"
,
output.
shape
)
The output is:
输入:
[[-0.4128, 0.2341, -0.9876, 0.4567],
[ 0.1234, 0.8765, -0.2345, 0.6789],
[-0.5678, 0.3456, 0.7890, -0.1234],
[ 0.9012, -0.4567, 0.2345, 0.5678]]
输出:
[[0.8765, 0.6789],
[0.9012, 0.7890]]
输入形状: torch.Size([1, 1, 4, 4])
输出形状: torch.Size([1, 1, 2, 2])
As you can see, the maximum value in each 2x2 window is retained.
Example 2: Different kernel_size and stride
Adjust the pooling parameters:
Example
import
torch
import
torch.
nn
as
nn
3x3 pooling with stride 1 (non-overlapping)
pool3x3
=
nn.
MaxPool2d
(
kernel_size
=
3
,
stride
=
1
)
Non-square pooling
pool_rect
=
nn.
MaxPool2d
(
kernel_size
=
(
2
,
3
)
,
stride
=
(
2
,
3
)
)
input_tensor
=
torch.
randn
(
1
,
1
,
6
,
9
)
(
"Input shape:"
,
input_tensor.
shape
)
(
"3x3 pooling output:"
,
pool3x3
(
input_tensor
)
.
shape
)
(
"Rectangular pooling output:"
,
pool_rect
(
input_tensor
)
.
shape
)
Example 3: Using padding
Edge padding can preserve edge information to some extent:
Example
import
torch
import
torch.
nn
as
nn
Pooling with padding
pool_padding
=
nn.
MaxPool2d
(
kernel_size
=
3
,
stride
=
1
,
padding
=
1
)
input_tensor
=
torch.
randn
(
1
,
1
,
4
,
4
)
output
=
pool_padding
(
input_tensor
)
(
"Input shape:"
,
input_tensor.
shape
)
(
"Output shape (with padding):"
,
output.
shape
)
Example 4: Returning indices
Using return_indices allows positions to be restored in the decoder:
Example
import
torch
import
torch.
nn
as
nn
Create a pooling layer that returns indices
pool_indices
=
nn.
MaxPool2d
(
kernel_size
=
2
,
stride
=
2
,
return_indices
=
True
)
input_tensor
=
torch.
randn
(
1
,
1
,
4
,
4
)
output
,
indices
=
pool_indices
(
input_tensor
)
(
"Output shape:"
,
output.
shape
)
(
"Index shape:"
,
indices.
shape
)
(
"Index values:"
,
indices.
squeeze
(
)
.
tolist
(
)
)
Example 5: Usage in a CNN
The position of pooling layers in a typical CNN architecture:
Example
import
torch
import
torch.
nn
as
nn
class
SimpleCNN
(
nn.
Module
)
:
def
init
(
self
)
:
super
(
SimpleCNN
,
self
)
.
init
(
)
Convolutional layers
self
.
conv1
=
nn.
Conv2d
(
3
,
32
,
kernel_size
=
3
,
padding
=
1
)
self
.
conv2
=
nn.
Conv2d
(
32
,
64
,
kernel_size
=
3
,
padding
=
1
)
self
.
relu
=
nn.
ReLU
(
)
Pooling layer: the size is halved each time
self
.
pool
=
nn.
MaxPool2d
(
2
,
2
)
def
forward
(
self
,
x
)
:
x
=
self
.
relu
(
self
.
conv1
(
x
)
)
32x32
x
=
self
.
pool
(
x
)
16x16
x
=
self
.
relu
(
self
.
conv2
(
x
)
)
16x16
x
=
self
.
pool
(
x
)
8x8
return
x
model
=
SimpleCNN
(
)
input_image
=
torch.
randn
(
1
,
3
,
32
,
32
)
output
=
model
(
input_image
)
(
"Input shape:"
,
input_image.
shape
)
(
"Output shape:"
,
output.
shape
)
Max pooling vs average pooling
| Type | Formula | Feature | Applicable scenarios |
|---|---|---|---|
MaxPool2d |
max(region) | Retains salient features and is more robust to noise | Image classification, object detection (commonly used) |
AvgPool2d |
mean(region) | Smooths features and preserves background information | Global average pooling, feature extraction |
Common questions
Q1: Can pooling layers be removed?
Modern networks such as ResNet and DenseNet tend to use smaller-stride convolutions instead of pooling, but pooling is still commonly used for fast downsampling.
Q2: What is the relationship between stride and kernel_size?
When stride = kernel_size, pooling windows do not overlap; when stride < kernel_size, pooling windows overlap.
Use Cases
nn.MaxPool2d Main application scenarios include:
- Image classification networks: Progressively reduce resolution and extract high-level features
- Object detection: Retain salient feature positions
- Reduce computational cost: Reduce feature map size
- Increase the receptive field: Let subsequent layers see features over a larger area