PyTorch nn.MaxPool2d Function
PyTorch torch.nn Reference Manual
torch.nn.MaxPool2dIt is a module in PyTorch for 2D max pooling.
The pooling layer can reduce the spatial size of feature maps, decrease computation, 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 of the pooling window movement. Defaults tokernel_size。padding(int or tuple): The padding size at the input edge. 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 for MaxUnpool2d. Defaults to False.ceil_mode(bool): Whether to use ceil instead of floor to compute the output size. Defaults to False.
Usage Examples
Example 1: Basic Usage
Create a max pooling layer:
Example
import torch.nn as nn
# Create a max pooling layer: window 2x2, stride 2
max_pool = nn.MaxPool2d(kernel_size=2, stride=2)
# Create input tensor
input_tensor = torch.randn(1, 1, 4, 4)
print("Input:n", input_tensor.squeeze().tolist())
# Forward propagation
output = max_pool(input_tensor)
print("nOutput:n", output.squeeze().tolist())
print("nInput shape:", input_tensor.shape)
print("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])
It can be seen that the maximum value in each 2x2 window is retained.
Example 2: Different kernel_size and stride
Adjust the pooling parameters:
Example
import torch.nn as nn
# 3x3 pooling, 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)
print("Input shape:", input_tensor.shape)
print("3x3 pooling output:", pool3x3(input_tensor).shape)
print("Rectangular pooling output:", pool_rect(input_tensor).shape)
Example 3: Using padding
Edge padding can preserve edge information to some extent:
Example
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)
print("Input shape:", input_tensor.shape)
print("Output shape (with padding):", output.shape)
Example 4: Returning Indices
Using return_indices can restore positions in the decoder:
Example
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)
print("Output shape:", output.shape)
print("Index shape:", indices.shape)
print("Index values:", indices.squeeze().tolist())
Example 5: Using in CNN
The position of the pooling layer in a typical CNN structure:
Example
import torch.nn as nn
class SimpleCNN(nn.Module):
def __init__(self):
super(SimpleCNN, self).__init__()
# Convolutional layer
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)
print("Input shape:", input_image.shape)
print("Output shape:", output.shape)
Max Pooling vs Average Pooling
| Type | Formula | Features | Use Cases |
|---|---|---|---|
MaxPool2d |
max(region) | Retains salient features and is more robust to noise | Image classification, object detection (commonly used) |
AvgPool2d |
mean(region) | Smoothes features and preserves background information | Global average pooling, feature extraction |
FAQ
Q1: Can the pooling layer 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, the pooling windows do not overlap; when stride < kernel_size, the pooling windows overlap.
Use Cases
nn.MaxPool2dThe main application scenarios include:
- Image classification networks: Gradually reduce resolution and extract high-level features
- Object detection: Preserve salient feature positions
- Reduce computation: Reduce the size of feature maps
- Increase receptive field: Allow subsequent layers to see a larger range of features
Other Extensions