The Convolution Operation

Mathematical foundations of discrete convolution for neural networks: 1D and 2D convolutions as linear operators, equivariance to translation, the relationship between convolution and matrix multiplication, stride, padding, and receptive fields.

Intermediate

Table of Contents

  1. Learning Objectives
  2. Notation
  3. Core Intuition
  4. 1D Discrete Convolution
  5. 2D Convolution for Images
  6. Convolution as Matrix Multiplication
  7. Translation Equivariance
  8. Stride and Padding
  9. Multi-Channel Convolutions
  10. Receptive Field Analysis
  11. Backpropagation Through Convolution
  12. Common Pitfalls
  13. Summary
  14. Exercises

Learning Objectives

  1. Define discrete convolution and cross-correlation; explain the distinction.
  2. Express 2D convolution as a matrix-vector product using doubly-block Toeplitz matrices.
  3. Prove translation equivariance of the convolution operator.
  4. Derive output dimensions for given kernel size, stride, and padding.
  5. Compute the receptive field of deep convolutional architectures.
  6. Derive the gradient of the loss with respect to convolution weights (backprop through conv).

Notation

  • x[n]x[n] — 1D input signal
  • w[k]w[k] — kernel (filter) of size KK
  • (xw)[n](x * w)[n] — convolution output
  • XRH×W\mathbf{X} \in \mathbb{R}^{H \times W} — 2D input (single channel)
  • WRKH×KW\mathbf{W} \in \mathbb{R}^{K_H \times K_W} — 2D kernel
  • ss — stride, pp — padding
  • Cin,CoutC_{\text{in}}, C_{\text{out}} — input/output channels

Core Intuition

Convolution is a linear operation that slides a small "template" (kernel) over the input, computing a weighted sum at each position. It encodes two inductive biases: locality (each output depends on a small input region) and weight sharing (the same kernel is applied everywhere). These biases make CNNs extremely parameter-efficient for spatial data.

2D Convolution

Input 8×8Kernel 3×3-1.0-1.0-1.0-1.08.0-1.0-1.0-1.0-1.0Output0.00Σ element-wise products = -1.0 + -1.0 + -1.0 + -1.0 + 8.0 + -1.0 + -1.0 + -1.0 + -1.0 = 0.00
Position
2
Insight: Convolution slides a kernel over the input, computing dot products at each position. Red highlight shows the receptive field — each output pixel summarizes local patterns.

1D Discrete Convolution

Definition (Cross-Correlation). In deep learning, "convolution" is technically cross-correlation:

(xw)[n]=k=0K1w[k]x[n+k].(1)(x * w)[n] = \sum_{k=0}^{K-1} w[k] \cdot x[n + k]. \tag{1}

True convolution flips the kernel: (xw)[n]=kw[k]x[nk](x \star w)[n] = \sum_k w[k] \cdot x[n - k]. Since kernels are learned, the flip is immaterial — both learn the same filters.

Output size for input length NN, kernel size KK, no padding:

Nout=NK+1.(2)N_{\text{out}} = N - K + 1. \tag{2}

2D Convolution for Images

For input XRH×W\mathbf{X} \in \mathbb{R}^{H \times W} and kernel WRKH×KW\mathbf{W} \in \mathbb{R}^{K_H \times K_W}:

(XW)[i,j]=m=0KH1n=0KW1W[m,n]X[i+m,j+n].(3)(\mathbf{X} * \mathbf{W})[i, j] = \sum_{m=0}^{K_H-1}\sum_{n=0}^{K_W-1} W[m, n] \cdot X[i+m, j+n]. \tag{3}

Output dimensions:

Hout=HKH+1,Wout=WKW+1.(4)H_{\text{out}} = H - K_H + 1, \quad W_{\text{out}} = W - K_W + 1. \tag{4}

Convolution as Matrix Multiplication

The convolution in (1) is a linear operation and can be written as y=Tx\mathbf{y} = \mathbf{T}\mathbf{x} where T\mathbf{T} is a Toeplitz matrix:

T=[w0w1w2000w0w1w2000w0w1w2].(5)\mathbf{T} = \begin{bmatrix} w_0 & w_1 & w_2 & 0 & \cdots & 0 \\ 0 & w_0 & w_1 & w_2 & \cdots & 0 \\ \vdots & & \ddots & & & \vdots \\ 0 & \cdots & 0 & w_0 & w_1 & w_2 \end{bmatrix}. \tag{5}

Each row is a shifted copy of the kernel weights. For 2D: the matrix is doubly-block Toeplitz. This viewpoint makes it clear that convolution is a special case of matrix multiplication with structured weight sharing.

Parameter count: A fully-connected layer mapping HWHWHW \to H'W' needs HWHWHWH'W' parameters. A convolutional layer needs only KHKWK_H \cdot K_W (per channel pair) — a massive reduction.


Translation Equivariance

Definition. An operator TT is equivariant to translation τ\tau if: T[τδ(x)]=τδ[T(x)]T[\tau_\delta(x)] = \tau_\delta[T(x)].

Theorem. Convolution is translation equivariant.

Proof. Let τδ(x)[n]=x[nδ]\tau_\delta(x)[n] = x[n - \delta]. Then:

((τδx)w)[n]=kw[k]x[n+kδ]=(xw)[nδ]=τδ[(xw)][n].((\tau_\delta x) * w)[n] = \sum_k w[k] \cdot x[n + k - \delta] = (x * w)[n - \delta] = \tau_\delta[(x * w)][n].

Shifting the input shifts the output by the same amount. \blacksquare

Implication: A feature detected at one spatial location will be detected identically at any other location, without needing to learn separate detectors.


Stride and Padding

Stride ss: Sample the output every ss positions:

(xw)s[n]=kw[k]x[ns+k].(6)(x * w)_s[n] = \sum_k w[k] \cdot x[ns + k]. \tag{6}

Output size: Nout=(NK)/s+1N_{\text{out}} = \lfloor(N - K)/s\rfloor + 1.

Padding pp: Append pp zeros to each side of the input. With padding:

Nout=(N+2pK)/s+1.(7)N_{\text{out}} = \lfloor(N + 2p - K)/s\rfloor + 1. \tag{7}

"Same" padding: Choose pp such that Nout=N/sN_{\text{out}} = \lceil N/s\rceil. For s=1s=1: p=(K1)/2p = (K-1)/2.


Multi-Channel Convolutions

Input: XRCin×H×W\mathbf{X} \in \mathbb{R}^{C_{\text{in}} \times H \times W}. Kernel: WRCout×Cin×KH×KW\mathbf{W} \in \mathbb{R}^{C_{\text{out}} \times C_{\text{in}} \times K_H \times K_W}.

Y[cout,i,j]=c=1CinmnW[cout,c,m,n]X[c,i+m,j+n]+b[cout].(8)Y[c_{\text{out}}, i, j] = \sum_{c=1}^{C_{\text{in}}}\sum_m\sum_n W[c_{\text{out}}, c, m, n] \cdot X[c, i+m, j+n] + b[c_{\text{out}}]. \tag{8}

Each output channel uses a different Cin×KH×KWC_{\text{in}} \times K_H \times K_W kernel. Total parameters: CoutCinKHKW+CoutC_{\text{out}} \cdot C_{\text{in}} \cdot K_H \cdot K_W + C_{\text{out}}.


Receptive Field Analysis

The receptive field of a neuron at layer ll is the input region that can influence its value.

For a stack of LL convolutional layers with kernel size KK and stride 1:

RF(L)=1+L(K1).(9)\text{RF}(L) = 1 + L(K - 1). \tag{9}

With stride ss at each layer:

RF(L)=1+l=1L(Kl1)k=1l1sk.(10)\text{RF}(L) = 1 + \sum_{l=1}^L (K_l - 1)\prod_{k=1}^{l-1}s_k. \tag{10}

Larger receptive fields capture more global context. Deeper networks or dilated convolutions increase RF without increasing parameters.


Backpropagation Through Convolution

Given L/Y\partial\mathcal{L}/\partial\mathbf{Y} (gradient w.r.t. output), we need:

Gradient w.r.t. weights:

LW[m,n]=i,jLY[i,j]X[i+m,j+n].(11)\frac{\partial\mathcal{L}}{\partial W[m,n]} = \sum_{i,j} \frac{\partial\mathcal{L}}{\partial Y[i,j]} \cdot X[i+m, j+n]. \tag{11}

This is itself a convolution of the input with the output gradient.

Gradient w.r.t. input:

LX[i,j]=m,nLY[im,jn]W[m,n].(12)\frac{\partial\mathcal{L}}{\partial X[i,j]} = \sum_{m,n} \frac{\partial\mathcal{L}}{\partial Y[i-m, j-n]} \cdot W[m,n]. \tag{12}

This is a full convolution (with padding) of the output gradient with the flipped kernel — the "transposed convolution."


Common Pitfalls

Pitfall 1. Confusing convolution with cross-correlation. In practice, all deep learning frameworks implement cross-correlation but call it "convolution."

Pitfall 2. Forgetting that pooling breaks strict equivariance. Max-pooling with stride 2 introduces approximate (not exact) translation invariance.

Pitfall 3. Not accounting for receptive field limitations. If the RF is smaller than the relevant structure in the data, the network cannot capture it.


Summary

  • Convolution slides a learned kernel over input, computing local weighted sums.
  • Encodes locality and weight sharing — reducing parameters by orders of magnitude.
  • Translation equivariant: detecting a feature anywhere requires only one kernel.
  • Expressible as Toeplitz matrix multiplication.
  • Backprop through conv = convolution with flipped kernel.
  • Receptive field grows linearly with depth (or faster with stride/dilation).

Exercises

Exercise 1. Compute the output of a 1D convolution with input [1,2,3,4,5][1, 2, 3, 4, 5] and kernel [1,1][1, -1].

Exercise 2. Derive the output dimensions for a 2D convolution with H=32,W=32,K=5,s=2,p=2H=32, W=32, K=5, s=2, p=2.

Exercise 3. Construct the Toeplitz matrix for a 1D kernel [w0,w1,w2][w_0, w_1, w_2] applied to a length-5 input.

Exercise 4. Prove that the composition of two convolutions (without nonlinearity) is equivalent to a single convolution with a larger kernel.

Exercise 5. Compute the receptive field of a VGG-style network with 5 layers of 3×33 \times 3 kernels and stride 1.