Kernel size is the spatial extent of the sliding window used by a convolution or pooling layer. It determines how many adjacent input values form the local patch summarized at each step. section 10.1.3 section 10.7
For a one-dimensional kernel of size 3, each output combines three successive input elements. A stride controls how far the window moves between steps, while padding controls what happens at the boundaries. In two dimensions, a 2 × 2 kernel examines a two-by-two patch. section 10.1.3 section 10.8
Pooling with a 2 × 2 kernel
A 2 × 2 max-pooling kernel with stride 2 summarizes each non-overlapping two-by-two patch by its maximum value, producing an output feature map half the input height and width. Average pooling instead returns each patch's mean. section 10.7
Figure 10.19 Max pooling using a 2 × 2 kernel with stride 2. The resulting output feature map is half the size of the input feature map. Each value of the output feature map is a max of the corresponding local patch in the input feature map.
Use the figure to inspect how a 2 × 2 kernel visits local patches and produces a smaller feature map through max pooling. section 10.7
Listing 10.9 PyTorch code for max and average pooling
import torch
X = torch.tensor([ #1
[0, 12, 26, 39],
[6, 19, 31, 44],
[12, 25, 38, 50],
[18, 31, 43, 57]
], dtype=torch.float32).unsqueeze(0).unsqueeze(0)
max_pool_2d = torch.nn.MaxPool2d( #2
kernel_size=2, stride=2)
out_max_pool = max_pool_2d(X) #3
avg_pool_2d = torch.nn.AvgPool2d( #4
kernel_size=2, stride=2)
out_avg_pool = avg_pool_2d(X) #5
Use the PyTorch listing to compare MaxPool2d and AvgPool2d with kernel_size=2 and stride=2. section 10.7



