stride and padding
/ STRYD and PAD-ing /
Stride and padding are the two dials that decide how a sliding filter travels across an image — and what size the result comes out. Both are easiest to picture by going back to the stencil you slide over a photo. Stride is how far you jump between stops. A stride of 1 means you nudge the filter one pixel at a time, examining every position and producing a large, detailed output. A stride of 2 means you skip every other position, so you do half as much work in each direction and the output shrinks to roughly a quarter the size — a built-in way to downsize, much like pooling.
Padding deals with a nuisance at the edges. When the filter reaches the border of the image, part of it hangs off into nothing, so without help the output map ends up a little smaller than the input, and the very edges get visited fewer times than the middle. Padding fixes this by adding a frame of extra pixels around the image — usually zeros, hence "zero-padding." With the right padding, the output can stay exactly the same size as the input ("same" padding), and the corners get a fair shot. Without padding ("valid" padding), the map deliberately shrinks at each layer.
Together these two settings let a designer control the trade-off between detail and cost. Small stride and generous padding preserve resolution but pile on computation; large stride and no padding shrink things fast and cheaply but throw away fine spatial information. They are hyperparameters — choices made before training, not things the network learns — and getting them wrong is a common cause of feature maps that come out the wrong shape and refuse to connect to the next layer.
Run a 3×3 filter over a 5×5 image. With stride 1 and no padding the output is 3×3 (shrunken). Add a one-pixel zero border ("same" padding) and the output is back to 5×5. Switch to stride 2 and the output drops to about 3×3 — downsized on purpose.
Padding holds the size steady; a bigger stride shrinks it on purpose.
Remember: stride controls how far the filter jumps (and thus how much the output shrinks), while padding controls what happens at the edges (and whether the output keeps its size). Mixing the two up is a classic beginner slip.