DL Lesson 34 – Padding AND Stride | Dataplexa

Padding and Stride

In the previous lesson, we learned how pooling layers reduce spatial dimensions and make CNNs more efficient.

Now we focus on two extremely important controls in convolutional layers — padding and stride.

These two parameters decide how much information is kept, how fast dimensions shrink, and how deep networks can go.


Why Padding and Stride Matter

Without careful control, convolution layers shrink feature maps very quickly.

If feature maps shrink too fast:

• Important edge information is lost • Deep networks become impossible • Spatial understanding degrades

Padding and stride exist to control this behavior.


What Is Stride?

Stride defines how far the convolution filter moves after each operation.

If stride = 1, the filter moves one pixel at a time.

If stride = 2, the filter jumps two pixels at a time.

Higher stride means:

• Faster computation • Smaller output • Less spatial detail


Stride Example

Imagine a 5 × 5 input with a 3 × 3 filter.

Stride = 1 → the filter slides slowly and captures detailed information.

Stride = 2 → the filter skips positions and reduces output size faster.


What Is Padding?

Padding means adding extra pixels around the border of the input.

These added pixels are usually zeros.

Padding helps us:

• Preserve spatial size • Keep edge information • Build deeper networks


Why Padding Is Critical

Edges contain valuable information.

Without padding, edge pixels are used fewer times than center pixels.

This creates a bias toward the center.

Padding fixes this imbalance.


Types of Padding

There are two common padding strategies:

Valid Padding:
No padding is applied. Output size shrinks.

Same Padding:
Padding is applied so output size matches input size.


Mathematical Intuition

Output size is determined by:

(Input − Filter + 2 × Padding) ÷ Stride + 1

This formula shows how padding and stride directly control spatial dimensions.


Padding and Stride in Code

Let us define a convolution layer using padding and stride.

from tensorflow.keras.layers import Conv2D

conv_layer = Conv2D(
    filters=32,
    kernel_size=(3,3),
    strides=1,
    padding="same"
)

This layer:

• Uses a 3 × 3 filter • Moves one pixel at a time • Preserves spatial size


Real-World Intuition

Think of scanning an image with a magnifying glass.

Stride controls how fast you move the glass.

Padding ensures you don’t ignore the edges of the image.

Together, they control what the model sees.


Trade-Offs

Small stride + padding:

• High accuracy • More computation

Large stride + no padding:

• Faster training • Loss of fine details

Modern CNNs carefully balance both.


Mini Practice

Think about this:

Why would early layers use small stride and padding, while later layers use larger stride?


Exercises

Exercise 1:
What happens if stride is increased?

Output size reduces faster and spatial detail decreases.

Exercise 2:
Why is padding important for edge detection?

It ensures edge pixels are treated equally to center pixels.

Quick Quiz

Q1. Does padding add learnable parameters?

No. Padding only adds constant values (usually zeros).

Q2. Which padding keeps output size unchanged?

Same padding.

In the next lesson, we will learn how feature maps and filters work together and how CNNs build hierarchical representations.