4.A — Padding, stride, and output-size formulas (appendix)
Goal: Derive the general output-size formula H_out = floor((H_in + 2P − K)/S) + 1, the three standard padding modes (valid, same, full), and the dilation generalisation.
1. Why this appendix exists
The forward formula in 4.1 §4 assumed no padding and stride 1. Real conv layers expose two extra knobs — padding and stride — that control output size and how aggressively the layer downsamples. The formulas are simple but produce off-by-one bugs when you misremember which way to round.[1]
2. Stride
A convolution with stride shifts the kernel by positions between consecutive outputs instead of by 1. The forward formula becomes
For this reduces to the standard cross-correlation. For the output is roughly half the input size in each spatial dimension — i.e. it downsamples by a factor of 2.
3. Padding
Padding wraps the input with extra rows and columns of zeros on each side before convolving. This serves two purposes: it lets the kernel "fit" at boundary pixels (otherwise the output is strictly smaller), and it gives precise control over output size.
Three padding choices appear constantly:
- Valid padding (). No padding. Output is strictly smaller than input.
- Same padding ( chosen so output size input size). For stride 1, this means , which is only an integer when is odd — one of the two reasons odd kernel sizes () are standard.
- Full padding (). The kernel touches every input position at least once and the output is larger than the input. Rarely used in CNN forward passes; appears naturally in the backward pass of conv (Appendix 4.C).
4. The general output-size formula
With padding on each side and stride , the output spatial size is:
Derivation. After padding, the effective input length is . The kernel of size has valid starting positions on a unit-stride grid. With stride , we take every -th starting position, giving outputs (the counts the position at offset 0).
The same formula applies to width. For 1D convolutions, is the sequence length.
4.1 Sanity checks
- Valid, stride 1. : . Matches 4.1 §4.
- Same, stride 1, odd . : . ✓
- Stride 2, . . For : . Halved, as expected.
5. Dilation
A kernel with dilation inserts implicit zeros between adjacent kernel weights. The kernel covers a span of input positions while using only learnable weights.
The output-size formula generalises to:
Set to recover §4. Dilated stacks (WaveNet, dilated TCNs) use this to grow receptive field exponentially with depth without any extra parameters or strides — see 4.3 §2 and the Aside in 4.2.
6. Practical defaults
For Transformer-era engineers who pass through a conv layer occasionally (vision encoders, audio frontends), the default is , stride 1, same padding, dilation 1. Downsampling uses stride 2 with same padding (or, in vision backbones, a separate pooling layer). Almost every "off the shelf" CNN block can be reconstructed from this with at most two extra knobs.
(a) Compute for . Then for .
(b) Show that the choice gives "same" padding for any odd when . What goes wrong for even ?
(c) A dilated 1D conv has . What is the output size for ? What span of inputs does each output depend on?
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).
↩