The convolution operation is the mathematical core of every CNN โ sliding a small kernel across an input, computing a dot product at every position, and producing one output value per position.
Formula
\(I\) is the input (e.g. an image), \(K\) is the kernel (a small matrix of learnable weights). At each output position \((i,j)\), the kernel is overlaid on the corresponding region of the input, and every overlapping pair of values is multiplied and summed โ exactly a dot product (see Dot Product) between the flattened kernel and the flattened local input patch.
Numerical Example โ One Output Value, Computed by Hand
A 3ร3 patch of the input: \(\begin{bmatrix}1&2&3\\4&5&6\\7&8&9\end{bmatrix}\), and a 3ร3 kernel: \(\begin{bmatrix}1&0&-1\\1&0&-1\\1&0&-1\end{bmatrix}\) (a simple vertical edge detector).
This single number becomes one entry in the output feature map โ the result of the kernel "checking" whether this particular local pattern (a specific kind of horizontal intensity change, in this case) is present at this position.
Sliding Across the Whole Input
The kernel slides across every valid position in the input, computing one output value per position โ collectively building the full feature map.
Code
import torch
import torch.nn.functional as F
input_patch = torch.tensor([[[[1.,2.,3.,4.],
[5.,6.,7.,8.],
[9.,10.,11.,12.],
[13.,14.,15.,16.]]]]) # shape (1,1,4,4) -- batch, channel, H, W
kernel = torch.tensor([[[[1.,0.,-1.],
[1.,0.,-1.],
[1.,0.,-1.]]]]) # shape (1,1,3,3)
output = F.conv2d(input_patch, kernel)
print(output) # a 2x2 output feature map -- one convolution result per valid kernel position
Common Mistakes
- Confusing convolution with correlation โ the mathematical definition of "true" convolution flips the kernel before sliding it, but deep learning frameworks (PyTorch included) implement what's technically cross-correlation without flipping; this distinction almost never matters in practice since the kernel is learned either way, but it's worth knowing the terminology is used loosely.
- Forgetting that the output feature map is smaller than the input (unless padding is used, covered a few notes ahead) โ every valid kernel position requires the kernel to fully fit within the input.
Interview Relevance
Q: "Describe, mathematically, what happens at one position during a convolution operation." The kernel is overlaid on a local patch of the input of the same size, every corresponding pair of values is multiplied together, and all these products are summed into a single number โ exactly a dot product between the flattened kernel and the flattened input patch. This is repeated by sliding the kernel across every valid position, producing the full output feature map.
Practice Question
Using the same edge-detection kernel from the worked example, compute the convolution output for the input patch \(\begin{bmatrix}2&2&2\\2&2&2\\2&2&2\end{bmatrix}\) (a uniform, unchanging region). What does the result suggest about how this kernel responds to flat regions with no edges?