liveBook · Manning

Convolution kernel

As explained in Learn Generative AI with PyTorch

A learned convolutional kernel whose weights slide across local input regions to produce feature responses.

A convolution kernel is a learned matrix of weights that slides across local regions of an input, multiplies corresponding values, and sums them to produce a feature map. Shared weights let one kernel detect the same pattern at different positions, such as an edge or texture. section 4.2.1 section 4.2

Unlike a fully connected layer, a convolution kernel sees a small input region rather than every input value. This local connectivity reduces parameters and supports efficient image processing. Convolutional layers extract features; transposed convolutional layers instead upsample or generate higher-resolution feature maps. section 4.2 section 4.2

The main convolution settings determine how the kernel interacts with the input:

  • in_channels identifies the input channels: 1 for grayscale and 3 for RGB images. section 4.2.1
  • out_channels sets how many output feature channels, and therefore learned feature detectors, the layer produces. section 4.2.1
  • kernel_size controls the kernel dimensions, such as 2 × 2, 3 × 3, or 4 × 4. section 4.2.1
  • stride controls the movement between scanned positions, while zero-padding controls how borders participate in the operation. section 4.2

A 2 × 2 kernel in PyTorch

Represent a 3 × 3 grayscale image with shape (1, 1, 3, 3), then create nn.Conv2d(in_channels=1, out_channels=1, kernel_size=2, stride=1). Replace its weights with [[1, 2], [3, 4]] and its bias with 0. Applying the layer produces a 2 × 2 feature map: [[7, 14], [54, 50]]. Each value comes from element-wise multiplication over one local 2 × 2 region followed by summation.

How Learn Generative AI with PyTorch covers it

How deep it goes
Explains sliding multiplication, shared weights, tensor shapes, layer arguments, parameter replacement, and numerical output, but not kernel training dynamics.
Examples
Demonstrates a 2 × 2 kernel scanning a 3 × 3 grayscale tensor and producing a 2 × 2 output.
What it assumes
Assumes basic tensors and images while introducing convolutional scanning, channels, kernel dimensions, stride, and padding.
What you can do after
Build it from scratch in PyTorch by defining Conv2d arguments, assigning kernel weights, and checking the resulting feature map.
How it connects
Connects kernels to convolutional layers, feature maps, fully connected layers, transposed convolutional layers, and translation invariance.

Also explained in

Compare how 11 books explain convolution kernel →

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.