3.4.2 · D3Convolutional Neural Networks

Worked examples — Stride, padding, and dilation

2,219 words10 min readBack to topic

This page is the "throw everything at it" companion to Stride, padding, and dilation. Every formula that note derived, we now stress-test against every kind of input you can meet: normal cases, the boundary where an output nearly vanishes, the degenerate kernel, the "does it divide evenly?" trap, dilation that overflows the image, a real-world word problem, and an exam twist. Prefer to read this in Hinglish? See 3.4.02 Stride, padding, and dilation (Hinglish).

The single formula every example below leans on (from the parent) is:


The scenario matrix

Convolution-sizing problems all live in one of these cells. The goal of the worked examples is to hit every cell at least once.

Cell What makes it special Danger it exposes Covered by
A. Clean divide slack is exact baseline sanity Ex 1
B. Non-exact divide (floor bites) slack has a remainder forgetting the floor Ex 2
C. Same-padding preserve choose so odd vs even kernel Ex 3
D. Degenerate kernel filter is a single pixel edge-case of every formula Ex 4
E. Limiting / near-collapse output , one more step kills it negative slack invalid Ex 5
F. Dilation overflow receptive field bigger than image Ex 6
G. All four knobs at once stride+pad+dilation combined order of operations Ex 7
H. Real-world word problem translate English numbers modelling, not arithmetic Ex 8
I. Exam twist (solve for unknown ) reverse the formula algebra, not plugging in Ex 9
Figure — Stride, padding, and dilation

The picture above is a mental map: the slack is the runway the kernel's left edge is allowed to travel, and stride is the size of each footstep on that runway.


Ex 1 — Cell A: the clean divide

Step 1 — Effective kernel. With : . Why this step? Dilation means no gaps, so the effective kernel equals the real kernel. Always compute first so the rest of the formula is honest.

Step 2 — Slack (the runway). . Why this step? This is how far the kernel's left edge can travel and still keep the whole kernel inside the image.

Step 3 — Count the footsteps. . Why this step? Each stride is a footstep of pixels; pixels of runway hold exactly footsteps.

Step 4 — Add the start. . Why this step? The counts the very first placement at position , which no footstep "moves to".

Verify: left-edge positions are . Each covers pixels ; the last covers , still inside . Four positions ✓. Matches .


Ex 2 — Cell B: the floor bites

Step 1 — (as before, ).

Step 2 — Slack . Why this step? One fewer input pixel gives a runway of , an odd number — a warning sign for stride .

Step 3 — Footsteps . Why this step? ; you can't take half a step, so the floor discards the leftover half-pixel. This is the whole reason the floor exists.

Step 4 — Add the start .

Verify: positions . A position at would need pixels but pixel doesn't exist ( means valid indices ). So is illegal, three positions ✓. The dropped half-pixel = the unused pixel .


Ex 3 — Cell C: same-padding, odd vs even kernel

Step 1 — Same-padding rule. For , set in the master formula: Why this step? We're reverse-engineering : instead of "given , find output", we fix the output we want and solve for the knob.

Step 2a — Odd kernel . . Why this step? is even, so the half is exact — same padding is symmetric.

Step 2b — Even kernel . . Why this step? is odd; half of it is not an integer. You cannot pad pixels on each side.

Step 3 — The even-kernel fix. Pad asymmetrically: on the left, on the right (total added ). Total effective width , and ✓. Why this step? The formula only cares about total padding ; split it however keeps the total right.

Verify (a): ✓. Verify (b): total pad gives ✓.

This is exactly why nearly all CNNs use odd kernels (see Network Depth vs Width).


Ex 4 — Cell D: the degenerate kernel

Step 1 — Effective kernel. . Why this step? Dilation inserts gaps between kernel elements. With only one element there are zero gaps (), so dilation is powerless. The factor annihilates it.

Step 2 — Output. . Why this step? A kernel touches every pixel individually; nothing is skipped, so output size = input size.

Verify: a convolution is a per-pixel channel mixer — it never changes spatial size at stride . ✓. Dilation had no effect, exactly as the algebra predicted.


Ex 5 — Cell E: the near-collapse limit

Step 1 — Slack. . Why this step? Zero slack means the kernel exactly fills the image — it can sit in only one place.

Step 2 — Output. . Why this step? The (the starting position) survives even when there is no runway. Output is a single number.

Step 3 — Push past the limit: . Slack ; . Why this step? A negative slack means the kernel is wider than the image — it cannot fit anywhere. signals an invalid layer, not a size-zero image.

Verify: on width- input → one placement at position covering pixels ✓ → . For you'd need pixel , which doesn't exist → no legal placement → the config is illegal. Frameworks raise an error here; treat as "impossible layer".


Ex 6 — Cell F: dilation overflows the image

Step 1 — Effective kernel. . Why this step? Two gaps, each holding empty pixels: . The sampled taps sit at offsets .

Step 2 — Slack. . Why this step? The stretched kernel spans pixels but the image is only wide — the third tap wants pixel , which is off the edge.

Step 3 — Output. invalid. Why this step? Same lesson as Ex 5: a negative result is the algebra's way of saying "this dilation is too aggressive for this image". Fix by padding () or lowering .

Figure — Stride, padding, and dilation

Verify: taps at ; pixel needs index but valid indices are . No fit → ✓. To rescue it, gives effective width and — a valid single output. This tradeoff is central to Semantic Segmentation Architectures and Receptive Field Analysis.


Ex 7 — Cell G: all four knobs at once

Step 1 — Effective kernel. . Why this step? Do dilation first; every later term uses , not . Forgetting this is the #1 combined-formula error.

Step 2 — Padded input. . Why this step? Padding widens the runway before the kernel is placed; both sides get .

Step 3 — Slack. .

Step 4 — Footsteps + start. . Why this step? divides cleanly (Cell A behaviour hidden inside a combined problem); for the start.

Verify: left-edge positions (that's values). The last kernel spans inside padded width (indices ) ✓. .


Ex 8 — Cell H: real-world word problem

Step 1 — Buy reach with dilation. We want a receptive field spanning pixels each side of centre = total span . Solve with : . Why this step? Dilation grows the receptive field without adding weights (still params), which is exactly the parameter-budget constraint. See Receptive Field Analysis.

Step 2 — Restore size with padding. For , same-padding needs . Why this step? Here the "kernel" the image feels is , so the same-padding rule uses , not .

Step 3 — Confirm. . Why this step? Full resolution preserved — vital for dense pixel-wise prediction where downsampling loses boundaries.

Verify: , ✓. Parameters used: , versus for an kernel — same reach, fewer weights.


Ex 9 — Cell I: exam twist (solve for the unknown)

Step 1 — Write the forward formula. With , , so Why this step? Everything except is known; the unknown is trapped inside the floor.

Step 2 — Strip the . . Why this step? Undo operations from the outside in.

Step 3 — Un-floor to a range. means , so . Why this step? The floor destroys information; the answer is a band, not a single value. That band is the trap the examiner set.

Step 4 — Solve the band. Multiply by : . Only integer: . Why this step? Padding must be a whole number of pixels, so the band collapses to a single legal value.

Verify: plug forward: ✓. (Check fails: , and : — confirming is unique.)


Recall Self-test (reveal after guessing)

Ex 2 output for ::: (the floor drops the leftover half-step) Same-padding for ::: for ::: Ex 5 output when kernel width = image width ::: What does mean ::: the layer is impossible — kernel bigger than (padded) input Recovered padding in Ex 9 :::

Related: Pooling Layers (an alternative downsampler to stride), Convolutional Layer Basics, and the parent Stride, padding, and dilation.