Why is zero padding used in deep learning?
Answer
Zero padding adds rows and columns of zeros around the input before a convolution. In CNNs it preserves spatial dimensions, prevents border information from being under-sampled, allows larger kernels and deeper stacks, and gives explicit control over output size. Beyond CNNs, padding standardizes variable-length sequences so NLP and time-series models can process them in batches.
(1) Preserves Spatial Dimensions: Without padding (“valid” convolution), a kernel shrinks the feature map by
in total per dimension (
per side) each layer; padding with
keeps the size unchanged.
(2) Retains Boundary Information: Padded borders let the kernel center on edge pixels, so corners and boundaries are processed as thoroughly as the interior.
(3) Controls Output Size: Padding decouples output dimensions from kernel size, enabling deeper networks and predictable feature-map shapes.
Mathematical Formulation:
Where:
and
are the output and input spatial sizes.
is the padding width added to each side,
is the kernel size, and
is the stride.
- “Same” padding for stride 1 uses
, giving
.

Figure 1: Padding a input to
lets a
kernel output the same
size instead of shrinking to
.
Beyond CNNs: In NLP and time-series tasks, zero padding extends shorter sequences to a uniform length for efficient batching. Because padded positions carry no information, models combine padding with attention masks so Transformer self-attention ignores those positions entirely.

Figure 2: Valid shrinks the map, same preserves it, and sequence padding plus a mask enables batched NLP inputs.
Leave a Reply