4.C — Backpropagation through convolution (appendix)
Goal: Derive the backward pass of a convolutional layer and recognise both ∂L/∂W and ∂L/∂x as themselves convolutions.
1. Why this appendix exists
Backprop through convolution is a famously satisfying calculation: the gradient of a convolution with respect to its weights is itself a convolution, and the gradient with respect to its input is also a convolution (with the kernel flipped). This is the structural reason CNNs can be trained efficiently — every step in both directions uses the same primitive that is already heavily optimised for the forward pass.
We derive the result for a 1D, single-channel, stride-1, no-padding convolution, then state the multi-channel generalisation.[1]
2. Setup
Forward pass:
Suppose the next layers produce a scalar loss , and assume we already know the upstream gradient for every output index . The two quantities we need are:
- — to update the parameters.
- — to keep backprop going into earlier layers.
Both come straight from the chain rule of 2.7 applied to the forward formula.
3. Gradient with respect to the weights
For each kernel entry :
From the forward formula, . So
This is a cross-correlation between the input and the upstream gradient . In the ML sense (cross-correlation), it has the same shape as a forward conv: the upstream gradient plays the role of the kernel, and we slide it over the input.
Why this is elegant. Forward and weight-gradient passes are both implemented by the same primitive. On a GPU, both reduce to a GEMM via im2col (4.1 §7), and the highly tuned matrix-multiply kernels in cuDNN serve double duty.
4. Gradient with respect to the input
The upstream signal at output position flows into input positions . Reversing the direction: input position feeds into output positions , restricted to valid indices.
Chain rule:
From the forward formula, is non-zero only when for some , in which case it equals . So
Substitute and define the flipped kernel :
This is a true convolution (with the flip!) of the upstream gradient with the kernel, restricted to valid indices. Equivalently, it is the cross-correlation of the upstream gradient with the flipped kernel — i.e. the forward conv operation applied to with kernel , padded with zeros on each side ("full" padding from Appendix 4.A §3).
So the input gradient is also a convolution. The kernel flip — irrelevant on the forward pass when the kernel is learned — re-appears here, naturally, as the signature of true convolution.
5. The multi-channel, multi-filter generalisation
For the multi-channel forward (4.1 §5)
the two backward formulas become:
A cross-correlation of with , summed over the spatial axes. The kernel-gradient tensor has the same shape as .
with . Read it as: for each input channel , sum over all output channels a "full" cross-correlation between the flipped kernel and the upstream gradient .
The bias gradient is the per-channel sum:
These three formulas are everything a conv-layer backward implementation needs.
6. Stride and dilation
Stride and dilation in the forward pass complicate the backward formula only by changing which outputs depend on which inputs. The general rule:
- The weight-gradient backward becomes a dilated cross-correlation, where the dilation factor equals the forward stride.
- The input-gradient backward becomes a strided (or "transposed") cross-correlation, where the stride equals the forward stride and zeros are inserted between upstream-gradient entries.
This is why a "transposed convolution" (or "deconvolution") layer is exactly the backward of a strided forward conv with no kernel flip — frameworks expose it as a primitive in its own right because it is also useful in generative models for upsampling.
We will not need stride/dilation backward formulas for the main path in Chapters 5–12, so we stop here.
(a) Reproduce the 1D weight-gradient formula by writing out for the forward formula and summing.
(b) For a 1D conv with , upstream gradient , and kernel , compute for every explicitly. Identify which entries correspond to the "full padding" of .
(c) Verify by direct computation that "the gradient of conv with respect to input is a convolution with the flipped kernel" by writing the linear map as a Toeplitz matrix and observing that is also a Toeplitz matrix, whose generating sequence is the flipped kernel.
(d) Show that the bias gradient follows from by direct differentiation. Why doesn't the bias have a "convolutional structure"?
References
- [1]
Dumoulin, V., & Visin, F. (2016). "A guide to convolution arithmetic for deep learning". arXiv:1603.07285 — Definitive reference for padding, stride, dilation, and transposed convolutions (Appendices 4.A, 4.C).
↩