liveBook · Manning

Convolution kernel

As explained in Deep Learning with PyTorch, Second Edition

A small learned filter window applied over local regions of an input to compute weighted sums.

A convolution kernel is a small grid of weights that moves across an image. At each location, it multiplies its weights by the corresponding input values and adds the results to produce one output value. Reusing the same weights across the image lets a filter respond to a local pattern wherever it appears. section 8.1.1

o11 = i11 * w00 + i12 * w01 + i13 * w02 +
      i21 * w10 + i22 * w11 + i23 * w12 +
      i31 * w20 + i32 * w21 + i33 * w22

A 3 × 3 example shows how one output value is calculated from nine nearby input values and nine kernel weights. section 8.1.1

The weights can be learned rather than chosen by hand: they start with random values and change through backpropagation. For an RGB image, a kernel has weights for all three input channels. In PyTorch, `nn.Conv2d(3, 16, kernel_size=3)` applies 16 filters to three-channel input, producing 16 output channels; its weight tensor has shape `[16, 3, 3, 3]`. Here, `kernel_size=3` means a 3 × 3 window. section 8.1.1 section 8.2

Blur or detect an edge

Set every weight in a 3 × 3 kernel to `1/9`, with zero bias, and each output value averages a neighborhood: the image blurs. Instead, negative weights on the left and positive weights on the right make vertical boundaries stand out. These hand-set filters demonstrate what kernels can detect; training normally learns the useful weights. section 8.2.2

Small kernels keep the operation local, but one small window cannot capture every large shape. Stacking convolutions and downsampling between them lets later layers work with a broader view. Making a kernel as large as the whole image would lose the useful locality of convolution. section 8.2.3

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Works through a 3 × 3 weighted sum, multichannel weight shapes, and learned versus hand-set filters; leaves continuous convolution aside.
Examples
Demonstrates RGB-image convolution, a neighborhood-averaging blur, and a vertical-edge detector.
What it assumes
Introduces the kernel’s weighted sum from an image neighborhood while using tensor notation and basic PyTorch modules.
What you can do after
Apply it via PyTorch `nn.Conv2d` to specify kernel size and channel counts, and inspect the resulting weight shape.
How it connects
Connects convolution kernels to backpropagation, feature detection, output channels, stacked convolutions, and pooling.

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 Deep Learning with PyTorch, Second Edition. The section links point to the parts of the book it draws on.