Padding controls the border; stride controls how far the kernel jumps between positions. Stride is the second knob in the output-size formula and an increasingly popular way to downsample—a learnable alternative to pooling. After this, you will assemble everything and flatten for the classifier.
Learning Objectives
By the end of this lesson, students should be able to:
- Define stride and describe its effect on output size.
- Compute output dimensions for various stride values.
- Explain strided convolution as a learnable downsampling method.
- Compare stride-2 convolution with 2×2 pooling.
- Configure stride in
nn.Conv2dandnn.MaxPool2d. - Combine padding and stride to hit a target feature-map size.
Stride is the step size (in pixels) by which the kernel moves between successive positions. A stride of 1 visits every position; a stride of 2 skips every other position, roughly halving the output dimension.
Stride Changes Output Size
Larger strides mean fewer kernel placements, so the output is smaller. This is downsampling done inside the convolution itself—no separate pooling layer needed.
| Input W | k | p | s | W_out |
|---|---|---|---|---|
| 32 | 3 | 1 | 1 | 32 |
| 32 | 3 | 1 | 2 | 16 |
| 28 | 5 | 0 | 1 | 24 |
| 224 | 7 | 3 | 2 | 112 |
Strided Convolution vs. Pooling
Strided Conv
- Downsamples and learns features.
- Has parameters.
- Used in ResNet, DCGAN.
Pooling
- Fixed max/avg summary.
- No parameters.
- Simple and cheap.
Shared
- Reduce spatial size.
- Grow receptive field faster.
- Cut downstream compute.
Stride in PyTorch
“Stride 2 always exactly halves the size.” Only approximately, and only with matching padding. The floor in floor((W - k + 2p)/s) + 1 can drop a row/column—always compute the formula rather than assume clean division.
Combining Padding and Stride
To go from a 224×224 image to 112×112 in one layer (as ResNet’s stem does), use k=7, s=2, p=3: floor((224 - 7 + 6)/2) + 1 = 112. Padding pins the geometry; stride sets the downsampling factor. Master both and you can design any feature-map pyramid.
Knowledge Check
- Short Answer: What is stride? Answer: The step size by which the kernel moves between positions.
- True/False: Increasing stride increases the output size. Answer: False—it decreases output size.
- Multiple Choice: For W=32, k=3, p=1, s=2, output is: (a) 32, (b) 16, (c) 8. Answer: (b).
- Short Answer: How does strided convolution differ from pooling? Answer: It downsamples with learnable parameters, while pooling uses a fixed summary.
- True/False: Stride can differ along height and width. Answer: True—e.g. stride=(2,1).
- Multiple Choice: ResNet’s stem (k=7,s=2,p=3) turns 224 into: (a) 224, (b) 112, (c) 56. Answer: (b).
- Short Answer: Write the output-size formula. Answer: floor((W - k + 2p)/s) + 1.
- Short Answer: Why does larger stride grow the receptive field faster? Answer: Each output covers a wider region of the input because positions are spaced further apart.
- True/False: A stride-2 conv has no learnable parameters. Answer: False—convolutions always have weights.
- Multiple Choice: The default stride of
nn.MaxPool2d(kernel_size=2)is: (a) 1, (b) 2, (c) 0. Answer: (b) (defaults to kernel_size).
Key Takeaways
- Stride is the kernel’s step size; larger stride yields smaller outputs.
- Strided convolution is a learnable downsampling alternative to pooling.
- Output size is
floor((W - k + 2p)/s) + 1—always compute, don’t assume. - Padding and stride together let you design any feature-map size.
- Next, Flatten bridges convolutional features to the dense classifier.
Hands-on idea: Give a target output size and have students solve for the stride/padding, then verify in PyTorch.
Discussion prompt: Why have some architectures replaced pooling with strided convolutions entirely?
Recap: Stride sets how far the kernel jumps and, with padding, fixes every feature map’s size. Continue with Flatten.