Max pooling shrinks a feature map by keeping the largest value in each local region. A common setup takes the maximum of each non-overlapping 2 × 2 tile, halving the feature map’s height and width. Strong responses to detected features survive; the other values are discarded. section 8.2.3
# In[21]: pool = nn.MaxPool2d(2) output = pool(img.unsqueeze(0)) img.unsqueeze(0).shape, output.shape # Out[21]: (torch.Size([1, 3, 32, 32]), torch.Size([1, 3, 16, 16]))
In PyTorch, `nn.MaxPool2d(2)` changes a batch of RGB images from shape `[1, 3, 32, 32]` to `[1, 3, 16, 16]`: the spatial dimensions shrink, while the batch and channel dimensions remain unchanged. section 8.2.3
Place pooling between convolutional layers to let later layers work on smaller feature maps. For example, two rounds of convolution, activation, and pooling reduce 32 × 32 features first to 16 × 16, then to 8 × 8. Those final features can be flattened for fully connected layers; their size determines the first linear layer’s input size. section 8.2.4 section 8.8
# In[22]:
model = nn.Sequential(
nn.Conv2d(3, 16, kernel_size=3, padding=1),
nn.Tanh(),
nn.MaxPool2d(2),
nn.Conv2d(16, 8, kernel_size=3, padding=1),
nn.Tanh(),
nn.MaxPool2d(2),
# ...
)
The `nn.Sequential` listing shows where two `nn.MaxPool2d(2)` modules sit among convolutions and `Tanh` activations. section 8.2.4
Pooling is not limited to 2D images. A 3D convolution block can apply two convolutions with activations, then use `nn.MaxPool3d(2, 2)` at the end. section 13.4.1









