Course contentsShow
← Machine Learning and Deep Learning
Lesson 856 of 3,53820. Convolutional Neural Networks: FundamentalsFree lesson

Padding: Zero, Valid, and Same

Different padding strategies to control output spatial dimensions and preserve information at borders.

Padding: Zero, Valid, and Same

What you'll learn: How padding strategies control output size and preserve border information in convolutions.

The Problem: Shrinking Outputs and Lost Border Information

When you apply convolution to an image, the output is smaller than the input (unless stride=1 and kernel size=1). A 5×5 image with a 3×3 kernel produces a 3×3 output. Pixels at the edges get "visited" by the kernel fewer times than center pixels, meaning border information gets under-represented.

Padding solves both issues by adding extra pixels around the input borders before convolution.

Three Padding Strategies

Valid Padding (No Padding)

This means zero padding — no extra pixels added. The kernel only slides over positions where it completely fits within the input. Output shrinks with each layer.

Analogy: Reading only complete sentences in a paragraph, ignoring partial ones at the edges.

Same Padding

Adds enough padding so the output spatial dimensions match the input (when stride=1). For a 3×3 kernel, add 1 pixel on each side; for 5×5, add 2 pixels per side.

Analogy: Adding buffer zones around a map so you can analyze every original point equally.

Zero Padding (The Technique)

The padded pixels are filled with zeros. This is the default and most common approach. The name "zero padding" describes what fills the border, while "valid" and "same" describe how much padding.

Why It Matters

  • Control output size: Same padding keeps dimensions constant across layers
  • Preserve border information: Edge pixels get as much attention as center pixels
  • Architecture flexibility: Deep networks need padding to avoid vanishing spatial dimensions

Key Takeaway: Padding controls output dimensions (valid=shrink, same=preserve) and ensures border pixels contribute equally to learned features by adding zeros around the input.