liveBook · Manning

Kernel size

As explained in Learn Generative AI with PyTorch

The spatial size of a convolutional kernel or receptive window applied to an input.

Kernel size is the spatial size of the filter, or receptive window, that a convolutional layer moves across an input image. In PyTorch, kernel_size=3 creates a 3 × 3 filter, while kernel_size=2 creates a 2 × 2 filter. Smaller windows inspect local regions; the selected size helps determine which spatial patterns the layer can extract. section 4.2.1 section 4.2

Read kernel size alongside the settings that control how a filter scans the image:

  • Stride specifies how many pixels the filter moves horizontally or vertically after each operation; larger strides downsample more. section 4.2.1
  • Padding adds rows of zeros around the input; its default is zero and it changes how the filter fits near image boundaries. section 4.2.1
  • Kernel size differs from transposed convolution: convolution extracts features, whereas transposed convolution upsamples feature maps. section 4.0 section 4.2

A 2 × 2 convolution

A 3 × 3 grayscale image can be passed to nn.Conv2d with in_channels=1, out_channels=1, kernel_size=2, and stride=1. Replacing the learned weights with [[1, 2], [3, 4]] and setting the bias to 0 produces a 2 × 2 output tensor: [[7, 14], [54, 50]]. The output is smaller because the 2 × 2 window moves across the 3 × 3 input without padding. section 4.2.1

Figure 4.5 A numerical example of how convolutional operations work, with stride equal to 1 and no padding

Use the numerical figure to trace a 2 × 2 filter across a 3 × 3 input with stride 1 and no padding. section 4.2.1

How Learn Generative AI with PyTorch covers it

How deep it goes
Defines kernel size, relates it to stride and padding, and works through a fixed-weight 2 × 2 convolution without deriving output formulas.
Examples
Demonstrates a 2 × 2 kernel scanning a 3 × 3 grayscale tensor with stride 1, zero padding, fixed weights, and zero bias.
What it assumes
Assumes basic image tensors and CNN terminology while explaining channels, filters, kernels, stride, and padding directly.
What you can do after
Build a PyTorch Conv2d layer by selecting kernel_size, stride, channels, padding, and optional bias.
How it connects
Connects kernel size with convolutional layers, filters, stride, padding, downsampling, feature extraction, and transposed convolution.

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 Learn Generative AI with PyTorch. The section links point to the parts of the book it draws on.