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.
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 .$$
- $W + 2P$ is the padded width — padding adds $P$ cells on each side, hence the factor of two.
- $-K$ accounts for the kernel needing $K$ cells under it before the far edge falls off the grid; $W+2P-K$ is the last valid starting column.
- $\div S$ then $+1$ counts the landings: start at $0$, jump by $S$, and add one for the starting position itself.
- The floor means a stride that does not divide the space evenly simply drops the leftover cells at the edge — the kernel never half-hangs off.
"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.