4.1 — Discrete convolution and cross-correlation
Goal: Define discrete convolution and cross-correlation, see why deep learning's 'convolution' is mathematically cross-correlation, and write down the multi-channel formula used in every CNN library.
1. Why this session exists
A fully-connected layer between two vectors of size has weights. For an image of pixels, that's parameters in a single layer — before we have anything resembling a network. Beyond cost, the dense layer also throws away a structural fact that the data screams at us: in images, in audio, in spectra, patterns of interest are local and translation-stable. The same edge detector should work whether the edge sits at coordinate or .
Convolution is the linear operator that hard-codes this prior. It replaces a dense matrix-vector product with a small, shared kernel that slides across the input. Three sessions are enough to extract what we need from CNNs before moving on to attention:
- 4.1 (this session) — the operation itself.
- 4.2 — why convolution is the right operator for images, and how far it can see.
- 4.3 — where convolution runs out of road, and why language pushes us to attention.
Engineering details — padding, stride, output sizes, pooling, backprop — live in the optional appendices 4.A, 4.B, and 4.C.[1, 2]
2. The continuous convolution (for reference)
Convolution is an operation between two real-valued functions on the same domain. In one dimension:
The product is the "sliding overlap": at time , multiply by a flipped, shifted copy of , then integrate. The flip is the signature of true convolution.
Convolution shows up everywhere — Green's functions, probability of sums of independent variables, the response of a linear time-invariant system to an input — for a single reason: it is the linear operator that commutes with translations on the underlying domain. We make this rigorous in 4.2.
3. Discrete convolution
For sequences indexed over , the discrete analogue of the integral is a sum:
The kernel is reflected (), shifted by , and inner-producted with . In practice has compact support — say for — so the sum is finite.
3.1 Cross-correlation: the same operation without the flip
Cross-correlation is convolution without the kernel flip:
Read the two formulas side by side: the only difference is the sign of inside . Relabel ; then cross-correlation with equals convolution with . The two operations are inter-convertible by flipping the kernel.
For the rest of this chapter, "convolution" means cross-correlation (the ML convention). The flip will re-appear when we derive backprop in Appendix 4.C, where it shows up naturally in the gradient with respect to the input.
4. Two-dimensional convolution
For an input image and a kernel , the 2D cross-correlation is:
Read it as: at output position , take the patch of whose top-left corner is at , multiply it element-wise by , and sum. Slide over the image to build .
Output size with no padding and unit stride:
The general formula, with padding and stride , is derived in Appendix 4.A.
4.1 A worked 1D example
Take a 1D input and a 1D kernel:
The output is , for :
So . The kernel measures a finite-difference between positions two apart — a discrete first-derivative detector. On a linear ramp like the derivative is constant, so is constant. That kernel is one of the classic edge detectors; in a real CNN it would be learned.[3, 4, 5]
5. Multi-channel, multi-filter convolution
Real CNNs operate on tensors with a channel axis. An RGB image is with . The convolutional layer maps it to using separate filters, each with its own bias.
The kernel tensor is and the bias vector is . The forward pass is:
In words: filter has its own bias and its own -channel kernel . To produce output channel at position , sum the 2D cross-correlations of with across all input channels , then add the bias.
There is no cross-talk between output channels in the formula — each is computed independently. Channels mix only through the input dimension . Stacking many layers lets the network mix output channels of one layer with input channels of the next, but a single conv layer is, per output channel, just a -channel filter sliding over the image.
5.1 Parameter count
A conv layer has
Compare this with what a fully-connected layer mapping would cost: weights. For a image and a layer with and the conv layer has
while the dense alternative would have
A factor of . That is the bargain convolution offers. The cost is the structural assumption — locality plus translation-stability — that we dissect in 4.2.
6. One-dimensional convolution
For sequence data — audio waveforms, spectra, character streams — the convolution collapses to one spatial axis:
The kernel tensor is now . Everything else is identical to the 2D case.
1D convs are the natural choice for any signal where samples have a single ordered index. In language models we won't use them as our main backbone — Chapter 6 shows why — but they're the operation underneath WaveNet, ByteNet, and many early sequence models, and they're the cleanest setting in which to develop receptive-field intuition (4.2).
7. Matrix view: im2col
Although the forward formula is a multi-index sum, you can rewrite it as a single matrix multiplication. Reshape each patch of as a row, stacked into a matrix — this rearrangement is called im2col. Reshape the kernel into a matrix . Then
with , and a final reshape recovers .
This is more than a trick: the im2col view explains why CNN forward passes are dominated by GEMM (general matrix multiply) calls on GPUs, and why the backward pass (in Appendix 4.C) also reduces to two matrix multiplies. It also reveals that every conv layer is a special case of a dense linear map — one whose matrix has shared, banded entries, with most positions tied to zero by the kernel's support.
8. What we keep going forward
For Chapter 6 and beyond, the load-bearing facts from this session are:
- The forward formula .
- The parameter count — independent of .
- The fact that convolution is a single linear map, so it can be written either as a multi-index sum or as a matrix product (
im2col).
Equivariance and receptive fields — the reason convolution works at all — are in 4.2.
(a) Write out the 1D cross-correlation of with the kernel (no padding, stride 1). What are the entries of ?
(b) A conv layer takes an input of shape and uses filters of size with no padding and stride 1. What is the output shape? How many learnable parameters does the layer have?
(c) Show explicitly that ML "convolution" (cross-correlation with kernel ) and signal-processing convolution (with kernel ) produce identical outputs when the kernel is symmetric, i.e. .
(d) Suppose you replace a conv layer with kernel by a dense layer that uses the im2col matrix but lets every entry of be free (no kernel sharing across positions). How many parameters does this dense layer have, in terms of ? Why is it no longer translation-equivariant? (Sketch the reason; 4.2 makes it rigorous.)
References
- [1]
LeCun, Y., Boser, B., Denker, J. S., et al. (1989). "Backpropagation applied to handwritten zip code recognition". Neural Computation, 1(4), 541–551. — First end-to-end trained CNN (4.1).
↩ - [2]
LeCun, Y., Bottou, L., Bengio, Y., & Haffner, P. (1998). "Gradient-based learning applied to document recognition". Proceedings of the IEEE, 86(11), 2278–2324. — LeNet-5 and the modern conv-pool-conv-pool template (4.1, Appendix 4.B).
↩ - [3]
Bengio, Y., Simard, P., & Frasconi, P. (1994). "Learning long-term dependencies with gradient descent is difficult". IEEE Trans. Neural Networks, 5(2), 157–166. — Formal vanishing/exploding gradient theorem for RNNs (5.5 §4.1).
↩ - [4]
Chen, S., Wong, S., Chen, L., & Tian, Y. (2023). "Extending context window of large language models via positional interpolation". arXiv:2306.15595 — Linear interpolation of RoPE frequencies for context extension (6.13 §4.1).
↩ - [5]
Raffel, C., Shazeer, N., Roberts, A., et al. (2020). "Exploring the limits of transfer learning with a unified text-to-text transformer" (T5). JMLR. arXiv:1910.10683 — C4 corpus (7.1 §4.1).
↩