Notes

Padding (Same, Valid)

A convolutional filter cannot sit neatly over pixels at an image’s border without running out of input. Padding solves that boundary problem by adding extra values—usually zeros—around the edge before the convolution is applied. The choice between Same and Valid padding controls whether a layer preserves or shrinks its spatial dimensions.

What the two modes mean
With a kernel of size K, stride S, input size N, and padding P, one output dimension is:

output = floor((N + 2P - K) / S) + 1
  • Valid padding means no added border (P = 0). A 3×3 kernel on a 32×32 input with stride 1 produces a 30×30 output. The filter only uses locations where its full window lies inside the original input.
  • Same padding adds enough border values to keep the output size equal to the input size when stride is 1. That same 3×3 convolution produces 32×32 output, commonly by adding one zero-pixel border on every side.

Why this changes a network
Padding determines how quickly feature maps shrink, how much edge information survives, and how easily layers can be stacked. Repeated valid convolutions rapidly reduce a map: a deep network can discard border-adjacent evidence simply because filters cannot inspect it fully. Same padding preserves dimensions, making it practical to build deep blocks and to align tensors for additions such as a ResNet skip connection. The trade-off is that zero padding creates artificial border context: edge activations see some zeros rather than real input values. This is usually acceptable, but it means border behavior differs slightly from behavior in the center.

Practical details
Frameworks such as TensorFlow expose padding="same" and padding="valid"; PyTorch commonly uses an explicit padding= value in nn.Conv2d. With stride greater than 1, “same” conventionally means an output size of roughly ceil(N/S), not necessarily the original size. Padding is therefore not a cosmetic setting: it is part of the layer’s geometry, affecting tensor shapes, memory use, and whether later layers can connect correctly.

Padding adds values, typically zeros, around an input’s borders before convolution. Valid padding adds none, so feature maps shrink as kernels cannot extend beyond the input. Same padding adds enough border values to preserve spatial dimensions for stride 1. Padding controls output size and lets edge features contribute to learned representations; without it, repeated convolutions rapidly discard boundary information.

Imagine sliding a small picture frame across a photo to look for patterns, such as edges or eyes. Near the photo’s border, the frame would hang partly off the edge. Padding adds a thin border of extra, usually blank, pixels around the image so the frame can still inspect those edge areas.

With same padding, enough border is added to keep the output image roughly the same width and height as the original. With valid padding, no border is added, so the frame only visits places where it fits completely inside the image. This makes the output smaller and can leave edge details less examined.