liveBook · Manning

Stride

As explained in Learn Generative AI with PyTorch

The step-size parameter that controls how far a convolutional operation moves and, in transposed convolution, how much upsampling occurs.

Stride is the number of pixels a convolutional filter moves horizontally or vertically on each step. A stride of 1 examines neighboring positions; a larger stride skips positions and reduces the output feature map’s spatial dimensions. section 4.2.1 section 4.2.2

Stride works alongside kernel size and padding. Padding adds zeros around the input and can preserve dimensions, while increasing stride produces more downsampling. In transposed convolutional layers, the mirrored operation supports upsampling and the generation of higher-resolution feature maps. section 4.2.2 section 4.0

A 3 × 3 image with stride 2

Create a 2D convolution with a 2 × 2 kernel, stride=2, and padding=1. Padding expands the 3 × 3 input to 5 × 5 with a border of zeros. The filter then jumps two pixels at a time. With weights [[1, 2], [3, 4]] and zero bias, the output is [[4, 7], [32, 50]]. The top-left value comes from 0 × 1 + 0 × 2 + 0 × 3 + 1 × 4 = 4; the bottom-left value comes from 8 × 4 = 32. section 4.2.2

Stride in a VAE encoder

A VAE encoder can use Conv2d layers with stride 2 to compress a color image. An input shaped (3, 256, 256) becomes (8, 128, 128) after the first layer, then shrinks through additional convolutional layers before being flattened into latent representations. The decoder mirrors this structure with ConvTranspose2d layers to reconstruct an image shaped (3, 256, 256). section 7.4.1

Figure 4.5 A numerical example of how convolutional operations work, with stride equal to 1 and no padding

Use the numerical convolution diagram to trace how stride 1 scans every neighboring position before comparing it with the stride-2 case. section 4.2.1

How Learn Generative AI with PyTorch covers it

How deep it goes
Explains movement, output-size effects, padding interaction, numerical calculations, and VAE tensor-shape changes.
Examples
Uses a 3 × 3 image, fixed 2 × 2 weights, stride 2, padding 1, and a stride-based VAE encoder.
What it assumes
Assumes basic images and convolutional layers while defining kernels, channels, padding, and feature maps through examples.
What you can do after
Apply stride via PyTorch Conv2d arguments and predict its effects on feature-map dimensions.
How it connects
Connects stride with convolutional layers, kernels, padding, downsampling, transposed convolution, upsampling, and VAEs.

Also explained in

Compare how 8 books explain stride →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Generative AI with PyTorch. The section links point to the parts of the book it draws on.