A kernel is the small matrix of learnable weights that gets slid across the input during convolution. A filter is a complete set of kernels โ one per input channel โ that together produce one output feature map. Understanding this precise distinction clarifies how convolution scales to multi-channel inputs.
Kernel โ The Basic Building Block
A kernel is typically small โ 3ร3 and 5ร5 are the most common sizes in modern architectures. Every value in the kernel is a learnable parameter, updated by gradient descent just like any other network weight.
Classic Hand-Designed Kernels (For Intuition)
| Kernel Purpose | Example 3ร3 Values | Effect |
|---|---|---|
| Vertical edge detection | \(\begin{bmatrix}1&0&-1\\1&0&-1\\1&0&-1\end{bmatrix}\) | Responds strongly to vertical intensity changes, near-zero on flat regions |
| Blur (box blur) | \(\frac{1}{9}\begin{bmatrix}1&1&1\\1&1&1\\1&1&1\end{bmatrix}\) | Averages every pixel with its neighbors, smoothing the image |
| Sharpen | \(\begin{bmatrix}0&-1&0\\-1&5&-1\\0&-1&0\end{bmatrix}\) | Emphasizes a pixel relative to its neighbors, increasing local contrast |
These classic image-processing kernels were historically hand-designed by engineers. In a CNN, the network learns its own kernel values via backpropagation โ it discovers whichever patterns (edges, textures, or far more abstract features in deeper layers) turn out to be useful for the task, rather than having them specified in advance.
Filter โ A Kernel Per Input Channel
For a multi-channel input (like an RGB image with 3 channels), a single "filter" actually consists of one kernel per input channel โ e.g. a 3ร3 filter applied to a 3-channel input has shape \((3, 3, 3)\): 3ร3 spatial size, times 3 input channels. The filter's output is the sum across all input channels' individual convolution results, collapsing the multi-channel input down to a single-channel output feature map (covered in detail in Channels).
Multiple Filters โ Producing Multiple Feature Maps
A convolutional layer typically applies many filters simultaneously (e.g. 64 filters), each independently learning to detect a different pattern โ producing 64 separate output feature maps, which are then stacked together as the 64 channels of that layer's output.
Code โ Inspecting a Layer's Learned Kernels
import torch.nn as nn
conv = nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, padding=1)
print(conv.weight.shape) # torch.Size([64, 3, 3, 3])
# 64 filters, each with 3 kernels (one per input channel), each 3x3
Common Mistakes
- Using "kernel" and "filter" completely interchangeably without the precise distinction โ for a single-channel input, a filter and its one kernel are the same thing, which is often where the terms get conflated; for multi-channel inputs, they're meaningfully different (a filter contains multiple kernels).
- Assuming a CNN's learned kernels must resemble intuitive, hand-designed ones (like edge detectors) โ early layers often do learn edge/texture-like kernels, but deeper layers typically learn far more abstract patterns that don't correspond to any simple, human-interpretable filter.
Interview Relevance
Q: "What's the precise difference between a kernel and a filter in a CNN?" A kernel is a single small weight matrix applied to one input channel. A filter is the complete set of kernels โ one per input channel โ that together produce one output feature map, by summing each channel's individual convolution result. A convolutional layer typically has many filters, each producing its own feature map, which are stacked together as that layer's output channels.
Practice Question
A convolutional layer has 32 filters, each of size 5ร5, applied to a 3-channel RGB input. What is the shape of this layer's weight tensor?