Convolution as a Linear Operator
A convolutional layer slides a small filter over its input and computes a weighted sum at every position. Mathematically it is a linear map with two strong constraints: local connectivity and shared weights. Those constraints cut the parameter count by orders of magnitude and make the layer translation-equivariant: shift the input and the output shifts the same way. This lesson treats convolution as the matrix it secretly is. Signal-processing background: Signals and Fourier.
The operation
For a 1-D input and a kernel of length , deep learning's "convolution" is actually cross-correlation:
(Mathematical convolution flips the kernel, ; since the kernel is learned, the flip does not matter.) In 2-D with input channels and filters,
The layer has parameters, independent of the image size.
import numpy as np
x = np.array([0, 0, 1, 2, 3, 0, 0, 5, 0], dtype=float)
w = np.array([-1.0, 0.0, 1.0]) # a difference (edge) detector
y = np.array([w @ x[i:i + 3] for i in range(len(x) - 2)])
print("input :", x)
print("output:", y)
print("np.correlate agrees:", np.allclose(y, np.correlate(x, w, mode="valid")))
print("np.convolve flips the kernel:", np.convolve(x, w, mode="valid"))input : [0. 0. 1. 2. 3. 0. 0. 5. 0.]
output: [ 1. 2. 2. -2. -3. 5. 0.]
np.correlate agrees: True
np.convolve flips the kernel: [-1. -2. -2. 2. 3. -5. 0.]
Convolution is a structured matrix
Stacking the outputs, where is a Toeplitz matrix: each row is the kernel shifted one step. A dense layer from 9 to 7 units has 63 free weights; this one has 3, repeated. This view also gives the backward pass at once: the gradient with respect to the input is , which is a convolution with the flipped kernel (Backpropagation Through Convolutions).
import numpy as np
x = np.array([0, 0, 1, 2, 3, 0, 0, 5, 0], dtype=float); w = np.array([-1.0, 0.0, 1.0])
n, k = len(x), len(w)
T = np.zeros((n - k + 1, n))
for i in range(n - k + 1):
T[i, i:i + k] = w
print(T.astype(int))
print("T @ x:", T @ x)
shift = np.roll(x, 1); shift[0] = 0
print("equivariance: conv(shift(x)) == shift(conv(x)) inside:", np.allclose((T @ shift)[1:], (T @ x)[:-1]))[[-1 0 1 0 0 0 0 0 0]
[ 0 -1 0 1 0 0 0 0 0]
[ 0 0 -1 0 1 0 0 0 0]
[ 0 0 0 -1 0 1 0 0 0]
[ 0 0 0 0 -1 0 1 0 0]
[ 0 0 0 0 0 -1 0 1 0]
[ 0 0 0 0 0 0 -1 0 1]]
T @ x: [ 1. 2. 2. -2. -3. 5. 0.]
equivariance: conv(shift(x)) == shift(conv(x)) inside: True
Why convolutions suit images
- Locality: nearby pixels are strongly related; a filter looks only at a neighborhood.
- Weight sharing: an edge detector useful at one place is useful everywhere.
- Equivariance: a shifted object gives a shifted feature map; pooling then adds some invariance (Pooling).
The parameter savings are dramatic: a convolution from 64 to 64 channels has 36,928 parameters whatever the image size, while a dense layer between two feature maps would have about .
The convolution theorem
Convolution in space is multiplication in frequency: (with circular boundaries). FFT-based convolution is faster for large kernels; for the small kernels of CNNs, direct or Winograd algorithms win.
In software
nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding)stores weights as(C_out, C_in, k, k).nn.Conv1dis used for audio and, with causal padding, for sequence models; depthwise convolutions (groups=C) mix space only.F.unfold(im2col) turns convolution into one big matrix multiply, which is how many GPU kernels implement it.
Go further: A CNN in NumPy, then in PyTorch trains a gradient-checked convolutional network on digits.
Exercises
Exercise 1: Output by hand
Cross-correlate with .
Answer
.
Solution
, , . A constant slope gives a constant response.
Exercise 2: Parameter count
How many parameters does Conv2d(3, 32, kernel_size=5) have?
Answer
Solution
weights plus 32 biases.
Exercise 3: 2-D convolution (code)
Implement a valid 2-D cross-correlation with loops and apply the vertical-edge kernel to an image that is 0 on the left half and 1 on the right.
Answer
The response is nonzero only in the column where the edge is.
Solution
import numpy as np
def corr2d(X, K):
h, w = K.shape
return np.array([[np.sum(X[i:i + h, j:j + w] * K) for j in range(X.shape[1] - w + 1)] for i in range(X.shape[0] - h + 1)])
X = np.zeros((4, 6)); X[:, 3:] = 1
print(corr2d(X, np.array([[1.0, -1.0]])))[[ 0. 0. -1. 0. 0.]
[ 0. 0. -1. 0. 0.]
[ 0. 0. -1. 0. 0.]
[ 0. 0. -1. 0. 0.]]
Test yourself
What do deep-learning libraries actually compute in a 'convolution' layer?
- True convolution with a flipped kernel
- Cross-correlation (no flip)
- A Fourier transform
- A dense matrix product with free weights
Translation equivariance means…
- the output is unchanged by shifts
- shifting the input shifts the output by the same amount
- the layer has no parameters
- the image must be square
As a matrix, a 1-D convolution is…
- diagonal
- a Toeplitz (banded, constant-diagonal) matrix
- orthogonal
- random