๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #166

Stride

Stride controls how far the kernel moves between each application โ€” a stride of 1 slides it one pixel at a time; a larger stride skips positions, producing a smaller, more downsampled output.

Formula for Output Size

\[ \text{Output size} = \left\lfloor\frac{W-K}{S}\right\rfloor + 1 \]

\(W\) is the input's width (or height), \(K\) is the kernel size, \(S\) is the stride.

Numerical Example

An input of width 7, kernel size 3, stride 1: \(\lfloor\frac{7-3}{1}\rfloor+1 = 5\) โ€” the kernel visits 5 positions. With stride 2 instead: \(\lfloor\frac{7-3}{2}\rfloor+1 = 3\) โ€” skipping every other position, producing a smaller output.

Diagram

Stride 1 every position visited Stride 2 positions skipped

A larger stride visits fewer positions, producing a smaller output feature map โ€” a form of built-in downsampling.

Why Increase Stride

Effect of Larger StrideConsequence
Smaller output feature mapLess computation and memory for subsequent layers
Larger effective receptive field growth per layerEach subsequent layer's neurons "see" a proportionally larger region of the original input, faster
Coarser spatial resolutionSome fine-grained spatial detail is lost โ€” a tradeoff against the computational savings

Code

import torch
import torch.nn as nn

x = torch.randn(1, 3, 32, 32)

conv_stride1 = nn.Conv2d(3, 16, kernel_size=3, stride=1)
print(conv_stride1(x).shape)   # torch.Size([1, 16, 30, 30])

conv_stride2 = nn.Conv2d(3, 16, kernel_size=3, stride=2)
print(conv_stride2(x).shape)   # torch.Size([1, 16, 15, 15]) -- noticeably smaller output

Common Mistakes

  • Forgetting stride affects output size, and hard-coding a fully-connected layer's expected input size without recomputing it after changing stride โ€” a very common source of shape-mismatch errors when experimenting with an architecture.
  • Assuming stride is only relevant to convolution โ€” pooling operations (covered later in this category) apply the exact same stride concept.

Interview Relevance

Q: "What's the effect of increasing a convolutional layer's stride from 1 to 2?" The kernel skips every other position, producing an output feature map roughly half the spatial size in each dimension โ€” reducing computation and memory for subsequent layers, at the cost of some spatial resolution/detail. It's a common technique for downsampling within convolutional layers themselves, as an alternative or complement to separate pooling layers.

Practice Question

For an input of width 10, kernel size 3, and stride 2, compute the resulting output width using the formula above.

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Stride โ€“ FAQs

Quick answers about learning Stride in Deep Learning.

This free note from CodingNow 2.0 explains Stride in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Stride, is 100% free with no signup required.
With focused practice, most students grasp Stride in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now