TL;DR
Convolution combines two functions, or two lists of numbers, by sliding one over the other. At each position you multiply the overlapping values and add them up, then move on to the next position. The result is a smoothed or filtered version of the input, where each output value depends on its neighbours.
When one of the two inputs is a small pattern of weights (a kernel), convolution is a local detector: it responds strongly wherever the input looks like the pattern.
Where you'll see it: in image blur and edge detection, in audio filters and reverb, in the probability density of a sum of two independent random variables, and in the convolutional layers at the heart of image-recognition networks (convolutional neural networks).
Intuition
A moving window of weights
Take a noisy stream of temperature readings and replace each reading with the average of itself and its two neighbours. That is a moving average, and it is already a convolution. The window of weights is . You slide it along the data, and at each place you take a weighted sum of what sits under it. Change the weights and you change the filter: give the centre more weight and you get a gentler smoothing, give the two neighbours opposite signs and you get a detector for change.
Why does this idea matter? Two reasons.
- Local structure is what most signals contain. A sharp edge in a photo, a note onset in a song and a sudden jump in a sensor all live in a few neighbouring values. A window of weights looks at exactly that size of thing.
- The same weights are reused everywhere. Sliding the window means one small set of numbers handles every position in the signal. That is far fewer parameters than a separate rule for each place, and it means a pattern is detected wherever it appears. This is a large part of why image networks work.
The plot below shows a step edge smeared by the moving average. Each output value is the average of three inputs, so the sharp jump becomes a ramp two positions wide.
- input signal
- after 3-point average
The math
Definition for sequences
For two sequences and indexed by integers, their convolution is the sequence
For finite sequences the sum runs only over the values that exist.
Here is the output position, and is a dummy index that runs over the input. The term is read backwards and shifted to . So the recipe is: flip , slide it to position , multiply the overlapping entries, and sum. The flip is the part that is easy to forget, and it is what makes convolution commutative: .
Definition for functions
For functions of a real variable the sum becomes an integral, which is why definite integrals are a prerequisite:
The variable plays the role of , and plays the role of .
Worked example in one dimension
Let and , both starting at index 0. Only the terms where both indices are in range contribute, so the output has entries.
So . As a check, the entries of sum to 6 and those of sum to 1.5, and . The output entries also sum to . The total is always the product of the two sums.
Two dimensions and the image case
For an image and a small kernel of size , the 2D convolution is
Deep-learning libraries compute a slightly different operation called cross-correlation, which skips the flip:
Each output is the dot product of the kernel with the patch of the image under it. Since a network learns its kernels, flipping them or not makes no practical difference. Keep the two versions distinct when you read a textbook, though, because they differ by a sign in some examples.
Take the image and the kernel
With no padding, the output is . Each output value is , computed with the cross-correlation form.
- Top-left: .
- Top-right: .
- Bottom-left: .
- Bottom-right: .
The output is . Large magnitudes mark places where the image changes along the diagonal. The strict convolution would flip first, giving the output with the opposite sign everywhere, so the detected pattern is the same either way.
A common 3 by 3 Gaussian-like blur kernel is
Its entries sum to . Suppose a patch of the image is flat, every pixel value 10. Each output is , so a flat region stays flat and keeps its brightness. That is the defining property of a blur: the weights sum to 1, so the average level survives while sharp detail is softened. Near a sharp edge the centre pixels, which carry the most weight, pull the value towards their neighbours, so the edge is softened.
Output size with padding and stride
For an input of length , a kernel of length , padding (zeros added on each side) and stride (how far the window moves each step):
For , , , this gives , which matches the edge example above.
Key properties
- Commutative and associative: , and .
- Distributes over addition: .
- Identity: convolving with the unit impulse (1 at position 0, zero elsewhere) returns the input unchanged.
- Convolution theorem: the Fourier transform (which re-expresses a signal as a sum of sine waves of different frequencies) of is the pointwise product of the transforms of and . This is why filtering by convolution can be done as multiplication in the frequency domain, which is much faster for long signals.
Convolution of probability densities
If and are independent continuous random variables with densities and , the density of their sum is the convolution of the two densities:
Take and uniform on . For , the integrand is nonzero only where , so the integral is . For , it is . The result is a triangle peaking at with height 1. The plot shows it.
- density of X + Y
The same rule explains why adding independent Gaussian variables gives another Gaussian: convolving two Gaussian densities produces a Gaussian whose variance is the sum of the two.
Worked examples
Let (a three-point moving sum) and .
- Output length is .
- .
- .
- .
- .
- The result is . Its sum is 9, which is , the product of the sums of the inputs.
Convolving the box filter with itself gives .
- Each entry is a sum of products of with . For example, the centre entry gets three pairs, each worth , so .
- The result is a wider, softer bump than one box filter. Repeated smoothing approaches a Gaussian shape, which is one face of the central limit theorem at work on a filter.
Tradeoffs and failure modes
Convolution is the right tool when the signal is local and the same pattern can occur anywhere. It is the wrong tool when it is not.
| Situation | Problem | Better choice |
|---|---|---|
| Patterns depend on absolute position (a face must be in a certain place) | A local window cannot see global layout | Add pooling, or position encodings, or fully connected layers |
| Long-range dependencies in a sequence | The kernel only sees a few neighbours | Attention, or dilated convolution to widen the window |
| Kernel sizes are large | Direct computation grows with the product of input and kernel sizes | FFT-based convolution |
| Upsampling a feature map | Ordinary convolution only shrinks or keeps size | Transposed Convolution |
Some practical traps:
- Border effects. Without padding, the output shrinks by each time you convolve. Stacking many layers with no padding can shrink a small image to nothing. Padding fixes the size but invents values at the border, usually zeros.
- Checkerboard artefacts. Strided transposed convolutions can leave a regular pattern in the output when kernel size and stride do not divide evenly.
- Sign and flip confusion. A library call named
convolvemay flip the kernel or not. Check which one you have before you hand-compute a result to compare against it.
History
The convolution integral appeared in analysis and probability long before it had a standard name; Laplace's work on sums of independent errors in the late 18th century already involves it. Its central idea, that the response of a linear system to an arbitrary input is a weighted sum of its responses to simple impulses, became the foundation of signal processing and linear systems theory in the 20th century.
In image processing, convolution with small kernels became standard in the 1960s and 1970s. The Sobel operator, a 3 by 3 kernel for finding edges, was presented by Irwin Sobel and Gary Feldman in the late 1960s, and it is a convolution. Such filters were hand-designed, and the people who used them knew what each kernel did.
The step that made convolution central to machine learning came in two stages. Kunihiko Fukushima's neocognitron in 1980 used layers of local, shared-weight feature detectors. In the late 1980s and 1990s Yann LeCun and his colleagues trained convolutional networks with backpropagation for handwritten digit recognition, and their 1998 LeNet-5 paper set the template that modern networks follow. The reason it caught on was the combination of local detection and weight sharing: a learned kernel can find the same feature anywhere, with far fewer parameters than a fully connected layer.
Check yourself
QuestionFor and , what is ?Show answer
Output length is . The entries are , then , then , then . The result is . Its sum is 6, which equals , as it should.
QuestionWhy does the kernel get flipped in the textbook definition of convolution, and why do deep-learning libraries usually skip the flip?Show answer
The flip makes convolution commutative and makes it match the integral definition, where reads backwards. Libraries skip it because the network learns its kernel weights anyway. Flipping a learned kernel just gives another kernel, so the flip changes nothing about what the model can represent.
QuestionA 5 by 5 image is convolved with a 3 by 3 kernel, no padding, stride 1. What is the output size, and what happens if you apply a second 3 by 3 convolution with no padding?Show answer
The first output is . The second gives . Two unpadded 3 by 3 layers shrink the image to a single pixel, which is why deep networks use padding.
QuestionYou add two independent random values, one uniform on and one uniform on . Why is the density of the sum the convolution of the two densities, and what is the probability that the sum is less than 1?Show answer
When two variables are independent, the probability of a given sum comes from integrating over every way the two can split it, and that integral is the convolution of the densities. For the probability, the sum is below 1 when the first value and second value satisfy with and . Since must be at least 0, the region is the triangle with corners , and in the plane, which has area . The joint density is on the rectangle , so the probability is .