Stride Moves the Window
Step the filter by two pixels at a time to skip intermediate overlaps and downsample the feature map.
The problem
Moving a filter one pixel at a time evaluates every possible overlap, producing a dense feature map nearly as wide and tall as the input. Early visual layers often contain smoothly varying neighborhoods where adjacent one-pixel shifts give redundant measurements while consuming memory and downstream arithmetic.
Stride s sets the step in pixels between consecutive window starts along both rows and columns. Instead of starting at 0, 1, 2, ..., the top-left corner of the filter lands at 0, s, 2s, ... for as long as the full K by K window stays inside the padded grid. The output side length becomes floor((H + 2p - K) / s) + 1, downsampling the grid inside the convolution itself.
Work through the numbers
Use the five-by-five input [[3, 3, 1, 0, 2], [2, 1, 0, 1, 2], [1, 2, 1, 3, 0], [0, 1, 2, 2, 1], [2, 0, 3, 1, 0]] and the three-by-three vertical-edge kernel [[1, 0, -1], [1, 0, -1], [1, 0, -1]] with no padding and zero bias. Each window adds its left three-pixel column and subtracts its right three-pixel column.
At stride = 1, three starts fit along each axis: 0, 1, and 2. On the top band (rows 0..2), column sums are [6, 6, 2, 4, 4], giving first-row differences [6 - 2, 6 - 4, 2 - 4] = [4, 2, -2]. Repeating on rows 1..3 and 2..4 yields the complete nine-cell map [[4, 2, -2], [0, -2, 0], [-3, -3, 5]].
Now increase to stride = 2. Only two starts fit per axis: 0 and 2, since floor((5 - 3) / 2) + 1 = 2. Those four windows evaluate starts (0, 0), (0, 2), (2, 0), and (2, 2), producing the two-by-two map [[4, -2], [-3, 5]]. Notice that cropping the top-left two-by-two block of the stride-one map would give [[4, 2], [0, -2]], which reads starts 0 and 1 and misses the right and bottom edges completely.
The floor matters when the division is not exact. On our five-by-five input at stride = 3, floor((5 - 3) / 3) + 1 = floor(2 / 3) + 1 = 1 start fits per axis (0 only, since start 3 would need row or column 5), leaving columns 3..4 and rows 3..4 uncovered and returning [[4]]. Likewise, a six-wide input at stride = 2 gives floor(3 / 2) + 1 = 2 starts (0 and 2).
The live model
The picture displays the five-by-five input, the three-by-three vertical-edge filter, and the resulting feature map. Slide stride across 1, 2, and 3: stride = 1 gives the nine-cell table [[4, 2, -2], [0, -2, 0], [-3, -3, 5]], stride = 2 keeps its four corners [[4, -2], [-3, 5]], and stride = 3 exercises floor(2 / 3) + 1 = 1 to leave the single top-left cell [[4]].
![A five-by-five input, the three-by-three vertical-edge kernel, and the stride-two two-by-two feature map [[4,-2],[-3,5]].](../assets/ml/stride-moves-the-window--demo.png)
torch.grad_enabled(false);
let stride = int(scene.parameter("stride", 2.0, 1.0, 3.0, 1.0));
let model = torch.sequential().conv2d(1, 1, 3, stride, 0);
let weight = model.param("0.weight");
let kernel = torch.tensor("[1.0,0.0,-1.0,1.0,0.0,-1.0,1.0,0.0,-1.0]");
for (i in range(0, 9)) { weight.set(i, kernel.value(i)); }
model.param("0.bias").set(0, 0.0);
let side = (5 - 3) / stride + 1;
let image = input.to(DType.F32).reshape(1, 1, 5, 5);
let answer = model.forward(image).reshape(side, side);
out.set("stride", stride).set("feature_map", answer);
{
"stride": 2,
"feature_map": [
[
4,
-2
],
[
-3,
5
]
]
}Your turn
The starter outputs [[4, 2], [0, -2]] instead of [[4, -2], [-3, 5]]: (0, 1) reports 2 instead of -2 and the bottom row reports [0, -2] instead of [-3, 5], because cropping the top-left corner reads starts 0 and 1 and misses the right and bottom borders. Configure the layer for stride = 2 with no padding so it steps across starts 0 and 2. Check tests new five-by-five grids and rejects unstrided maps, reversed signs, and hardcoded tables.
![The five-by-five input, three-by-three vertical-edge kernel, and the cropped starter output [[4,2],[0,-2]] from starts 0 and 1.](../assets/ml/stride-moves-the-window--exercise.png)
torch.grad_enabled(false);
let model = torch.sequential().conv2d(1, 1, 3, 1, 0);
let weight = model.param("0.weight");
let kernel = torch.tensor("[1.0,0.0,-1.0,1.0,0.0,-1.0,1.0,0.0,-1.0]");
for (i in range(0, 9)) { weight.set(i, kernel.value(i)); }
model.param("0.bias").set(0, 0.0);
let image = input.to(DType.F32).reshape(1, 1, 5, 5);
let dense = model.forward(image).reshape(3, 3);
let answer = dense.slice(0, 0, 2).slice(1, 0, 2);
out.set("feature_map", answer);
{
"feature_map": [
[
4,
2
],
[
0,
-2
]
]
}Change the convolution to use stride 2 with no padding and reshape its output to two by two.
Read the result carefully
Strided convolution computes the exact subset of stride-one outputs located at multiples of s, saving the work of the skipped windows. Because intermediate positions are never evaluated, a narrow one-pixel feature sitting on an odd column can fall into the zero column of this filter at starts 0 and 2 and vanish, a sampling effect known as aliasing when high-frequency detail is downsampled without smoothing.
Recap
- Stride
splaces window starts at0, s, 2s, ..., giving output lengthfloor((H + 2p - K) / s) + 1. - On a five-by-five input with a three-by-three filter and no padding,
stride = 2keeps the four corner windows(0, 0),(0, 2),(2, 0), and(2, 2). - Slicing the top-left corner of a stride-one map crops spatial coverage instead of stepping across the whole image.