liveBook · Manning

Max pooling

As explained in Deep Learning with PyTorch, Second Edition

Max pooling is an aggregation operation that replaces each local or grouped region with its maximum value, often to downsample a representation.

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

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Explains 2 × 2 maximum selection, checks resulting tensor shapes, and places pooling in 2D and 3D convolution blocks.
Examples
Demonstrates a 32 × 32 RGB image reduced to 16 × 16, a two-pool image classifier, and a 3D convolution block.
What it assumes
Builds on images and convolutional feature maps while explaining downsampling through neighboring pixel values.
What you can do after
Apply it via PyTorch `nn.MaxPool2d(2)` to halve spatial dimensions between convolutional layers.
How it connects
Connects downsampling to feature maps, convolutions, activation functions, and fully connected layers.

Also explained in

Compare how 10 books explain max pooling →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with PyTorch, Second Edition. The section links point to the parts of the book it draws on.