liveBook · Manning

Kernel size

As explained in Math and Architectures of Deep Learning

The spatial extent of the sliding window used by a pooling layer, determining the patch size summarized at each step.

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

How Math and Architectures of Deep Learning covers it

How deep it goes
Explains kernel size through sliding one-dimensional and two-dimensional windows, but defers detailed output-size formulas.
Examples
Uses size-3 one-dimensional convolution and 2 × 2 max- and average-pooling examples.
What it assumes
Assumes familiarity with input arrays and feature maps while establishing sliding windows through concrete one- and two-dimensional examples.
What you can do after
Apply it via PyTorch MaxPool2d or AvgPool2d by selecting kernel_size and stride values.
How it connects
Connects kernel size to convolution, pooling, stride, padding, local patterns, and feature-map dimensions.

Also explained in

Compare how 4 books explain kernel size →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Math and Architectures of Deep Learning. The section links point to the parts of the book it draws on.