Convolution, padding and strides
Convolution in CNNs computes local weighted sums over input using a kernel; padding and stride control output size and sampling, important for designing architectures and computing parameters and memory.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Convolution, padding and strides are the core operations in Convolutional Neural Networks (CNNs). An engineer uses them when designing network architectures (to control spatial resolution and receptive field), computing parameter counts and memory, and implementing efficient forward and backward passes for training and inference.
Key ideas
(deep-learning cross-correlation, used in most libraries) R[i,j] = sum_u sum_v I[i+u, j+v] · K[u,v] Draw to show difference: draw a small 3×3 kernel, show flipping for mathematical convolution vs no flip for cross-correlation.
Kernel (filter) — a small, trainable weight matrix of size F×F (could be non-square). Bold definition: kernel is the matrix of weights applied locally to input patches.
Stride (S) — the number of pixels the kernel moves between applications. Bold definition: stride is an integer S ≥ 1 that subsamples outputs by shifting the kernel by S pixels horizontally and vertically.
Dilation (d) — an integer d ≥ 1 that spaces kernel elements by d−1 zeros; it enlarges the receptive field without adding parameters. The effective kernel size is F_eff = F + (F − 1) · (d − 1), and the output-size formula generalizes to W_out = floor((W − F_eff + 2·P)/S) + 1 (similarly for height).
Padding (P) — adding extra values (typically zeros) around the input borders so the kernel can be applied at border positions. Bold definition: padding is adding P rows/columns of specified values (usually zero) on each side of the input.
Valid / Same / Full padding — three common types:
- valid: no padding (P = 0). Kernel only where it fits completely.
- same: output spatial size is intended to be the same as input (depending on stride). For odd-sized kernels and stride 1, P = (F-1)/2.
- full: pad so every position at least one overlap occurs (often P = F-1), producing larger outputs.
Output size formula (2D) — given input width W, height H, kernel size F (assume square for derivation), padding P applied on all sides, and stride S, the output width W_out and height H_out are:
- W_out = floor((W - F + 2·P)/S) + 1
- H_out = floor((H - F + 2·P)/S) + 1
Derivation (step-by-step universities expect):
- With padding P, effective dimension horizontally becomes W_eff = W + 2·P.
- Kernel of width F needs F consecutive positions; starting positions available = W_eff - F + 1.
- With stride S, we take every S-th starting position, so count = floor((W_eff - F)/S) + 1 = floor((W - F + 2P)/S) + 1.
Same padding for stride 1 — to keep output size equal to input (W_out = W) when S = 1 and odd F, choose P = (F - 1)/2. For even F or S > 1, achieving exact equality uses asymmetric padding or the framework-specific rule; many frameworks use formulas that distribute extra pad to one side.
Receptive field — the spatial region of the input that affects a given output feature. Bold: receptive field of a unit is the size of input region that contributes to that unit. For stacked layers compute receptive field recursively: RF_{l} = RF_{l-1} + (F_l - 1)·jump_{l-1}, where jump (effective stride) updates by jump_l = jump_{l-1}·S_l, with base RF_0 = 1 and jump_0 = 1.
Number of parameters in a convolutional layer — For a layer with C_in input channels, C_out output channels, kernel F×F, and one bias per output channel: parameters = C_out · (C_in · F · F + 1).
Spatial output vs channels — convolution preserves/changes spatial size according to the formula above; the channel dimension is changed by number of filters (C_out). Each output channel is produced by convoluting the input across all input channels with a separate set of kernels and summing.
Diagrams to draw in an exam (describe exactly):
- Draw a 4×4 grid (label rows 0..3 and columns 0..3) as input I. Draw a 3×3 kernel K and place it on top-left input cells (covering positions (0..2,0..2)). Draw arrows showing elementwise multiplication and summation to produce R[0,0].
- Show the kernel shifted right by stride S = 2 on a 5×5 input to cover positions (0..2,2..4) — label that movement S and show resulting output index increment. If keeping a 4×4 input, use S = 1 and shift to cover positions (0..2,1..3).
- For padding, draw the original input and surround it by P rows/cols of zero pixels; label P, show kernel centered at an original border pixel to illustrate why padding is needed.
Formulas
W_out = floor((W - F + 2·P) / S) + 1 — W_out is output width (pixels); W is input width (pixels, unitless count); F is kernel width (pixels); P is padding (pixels); S is stride (pixels). Applies for 2D convolution along width; use floor for discrete steps.
H_out = floor((H - F + 2·P) / S) + 1 — H_out is output height (pixels); H is input height (pixels); other symbols as above. Applies for 2D convolution along height.
P = (F - 1) / 2 (for S = 1 and odd F) — P is padding per side (pixels). Use when you want same output size as input for stride 1 and odd kernel.
params = C_out · (C_in · F · F + 1) — params is number of trainable parameters in the layer; C_out is number of output channels (filters); C_in is number of input channels; F is kernel size (pixels). Unitless count. If no bias used, subtract C_out.
output_value = sum_{u=0..F-1} sum_{v=0..F-1} sum_{c=0..C_in-1} I[i·S + u, j·S + v, c] · K[u,v,c, m] + b[m] — value of output channel m at spatial location (i,j). I is input tensor (pixels, channels), K is kernel weights, b is bias for channel m. Applies for cross-correlation variant with stride S and zero padding already applied to I.
RF_l = RF_{l-1} + (F_l - 1) · jump_{l-1} — receptive field size at layer l, where jump_{l} = jump_{l-1} · S_l and RF_0 = 1, jump_0 = 1.
Note: floor() means rounding down to nearest integer. When frameworks use ceil() (e.g. some definitions of 'same' with strides), check the framework docs.
Worked examples
Example 1 — manual 2D convolution (cross-correlation) numeric
Given: input I (4×4 pixels, single channel) and kernel K (3×3), stride S = 1, padding P = 0.
I = [ [1, 2, 0, 1], [0, 1, 3, 2], [2, 1, 0, 1], [1, 0, 2, 1] ]
K = [ [1, 0, -1], [1, 0, -1], [1, 0, -1] ]
Step 1: Compute output size using formula: W = H = 4, F = 3, P = 0, S = 1.
W_out = floor((4 - 3 + 0)/1) + 1 = 2 and H_out = 2.
Step 2: Compute outputs positions (i,j) = (0,0), (0,1), (1,0), (1,1).
Position (0,0): overlay K on I[0..2,0..2]: values multiplied and summed: (1·1) + (2·0) + (0·-1)
- (0·1) + (1·0) + (3·-1)
- (2·1) + (1·0) + (0·-1) = 1 + 0 + 0 + 0 + 0 - 3 + 2 + 0 + 0 = 0
Position (0,1): overlay on I[0..2,1..3]: (2·1)+(0·0)+(1·-1) +(1·1)+(3·0)+(2·-1) +(1·1)+(0·0)+(1·-1) = 2 + 0 -1 + 1 + 0 -2 + 1 + 0 -1 = 0
Position (1,0): overlay on I[1..3,0..2]: (0·1)+(1·0)+(3·-1) +(2·1)+(1·0)+(0·-1) +(1·1)+(0·0)+(2·-1) = 0 + 0 -3 + 2 + 0 + 0 + 1 + 0 -2 = -2
Position (1,1): overlay on I[1..3,1..3]: (1·1)+(3·0)+(2·-1) +(1·1)+(0·0)+(1·-1) +(0·1)+(2·0)+(1·-1) = 1 + 0 -2 + 1 + 0 -1 + 0 + 0 -1 = -2
So output R = [[0, 0], [-2, -2]]. Final answer (bold): R = [[0, 0], [-2, -2]].
Example 2 — compute output size with padding and stride
Given input 32×32, kernel F = 5, stride S = 2, want 'same' style output dimension rounding down. Choose P = 2 (common choice for odd kernel to approximately preserve size when S = 1; here S = 2 we compute explicitly).
Using formula: W_out = floor((32 - 5 + 2·2)/2) + 1 = floor((32 - 5 + 4)/2) + 1 = floor(31/2) + 1 = 15 + 1 = 16.
So output is 16×16. Final answer (bold): 16 × 16 output.
Example 3 — parameter count of a convolutional layer
Given input channels C_in = 3 (RGB), number of filters C_out = 16, kernel F = 3, include bias.
Params = C_out · (C_in · F · F + 1) = 16 · (3 · 3 · 3 + 1) = 16 · (27 + 1) = 16 · 28 = 448.
Final answer (bold): 448 parameters.
Common mistakes
- Using P = (F-1)/2 blindly when F is even or when S > 1; that formula assumes odd F and S = 1.
- Forgetting to multiply by input channels when computing total parameters, or forgetting to add biases when present.
- Confusing stride with dilation (dilated convolution increases receptive field differently).
- Assuming padding can always make output exactly equal to input size for arbitrary stride without asymmetric padding or framework-specific rules.
For your exam
Typical question types:
- Definitions: define convolution, kernel, stride, padding, receptive field. Include brief differences between convolution and cross-correlation.
- Derivations: show step-by-step derivation of the output size formula and compute padding P for 'same' with odd F.
- Numericals: given input size, kernel, padding and stride compute output size and perform a small numeric convolution (usually 3×3 on 4×4 input).
- Diagrams: draw kernel overlay, show stride movement, illustrate padding added around input.
- Short notes: compute parameter count for layers, explain effect of stride on computational cost and memory.
What full-marks answers include:
- Correct definitions with bold terms and clear assumptions (e.g., whether convolution is cross-correlation or mathematical convolution).
- Correct formulas written and derived, showing each algebraic step.
- Numeric examples with stepwise computation and final bolded answers.
- Diagrams drawn and labelled exactly (input size, kernel size, stride, padding, and output indices).
Quick check
- What is the output width for input W = 28, F = 5, P = 0, S = 1?
- How many parameters for C_in = 16, C_out = 32, F = 3 with bias?
- For a kernel F = 3 and stride S = 1, what P gives 'same' padding?
Answers: 1: 24 (since floor((28-5+0)/1)+1 = 24). 2: 32·(16·3·3 + 1) = 32·(144 + 1) = 32·145 = 4640. 3: P = 1.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?