Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to Deep Learning Notes
Topic #353

Kernel and Filter

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 PurposeExample 3×3 ValuesEffect
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?

Related DL Notes

Want to go beyond the notes?

Join Coding Hubs School of AI's Deep Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →