← Explainer Library

Interactive Explainer

Padding, Stride & Output Size

Two knobs decide how big a convolution's output is. Padding adds a border of zeros so edge pixels get a fair turn — and can hold the output size fixed. Stride makes the kernel jump instead of creep, shrinking the output on purpose. Move the four sliders and one floor formula tells you the output size before the kernel has taken a single step.

The Lab

Four sliders, one output size

The grey grid is a $W\times W$ input. Padding wraps it in a border of dashed zero cells. A $K\times K$ kernel then slides across the padded grid with stride $S$, landing once per output cell. Step through the landings and watch the output grid on the right grow to exactly the size the formula predicts.

Left: the input (solid) wrapped in a padded border of zeros (dashed). The kernel outline is the current window. Right: the output grid, filling in as the kernel lands.
Output size
Padded input
Padding regime
Try the two buttons. "Make it valid" sets $P=0$: the output is strictly smaller than the input because the kernel can never hang off the edge. "Make it same" sets $P=\tfrac{K-1}{2}$ with $S=1$: the padded border is exactly wide enough that the output matches the input. Then bump the stride to $2$ and watch the output halve, padding or not.
The Payoff

Reading the formula

$$\text{out} = \left\lfloor \frac{W + 2P - K}{S} \right\rfloor + 1 .$$
"Same" padding, precisely. With stride $1$, output equals input when $2P = K-1$, i.e. $P=\tfrac{K-1}{2}$. That is why odd kernel sizes ($3,5,7$) are the norm: only then is $\tfrac{K-1}{2}$ a whole number and the padding is symmetric. This is the trick that lets deep CNNs stack dozens of $3\times3$ layers without the feature map collapsing to nothing.