← CS Lab AI & Machine Learning / CNN Stride & Padding Open standalone ↗

This is the one formula that tells you how big a convolution's output will be — before you ever run it. Give it the input size, the kernel size, the stride and the padding, and it hands back the width and height of the feature map.

Where you meet it

What it does

It counts how many places the kernel can stop as it slides across the input. That count is the output width (and, the same way, the height). No weights, no training — this is pure geometry of a window moving over a grid.

The output size is just the number of places the kernel can stand. Count the stops, and you already have it.

How it works

Lay the F-wide kernel on a W-wide row. The first stop puts its left edge at position 0; the last stop that still fully fits has its left edge at W−F. If you move by S each time, the left-edge stops are 0, S, 2S, … up to W−F — so the count is (W−F)/S, plus one for that very first stop. Padding adds P cells of border on each side, so the width grows by 2P. Put it together and round down:

out = ⌊(W − F + 2P) / S⌋ + 1

W=5, F=3, P=0
  S=1  →  stops at 0,1,2      out = ⌊(5−3+0)/1⌋+1 = 3   → 3×3
  S=2  →  stops at 0,2        out = ⌊(5−3+0)/2⌋+1 = 2   → 2×2
                                (a stop at 4 would hang off the edge → floor drops it)

W=5, F=3, P=1
  S=1  →  out = ⌊(5−3+2)/1⌋+1 = 5   → 5×5   ("same" size)

The floor ⌊⌋ is not decoration: a partial stop at the end — where the kernel would hang off the edge — simply does not count. And P=1 with a 3×3 kernel gives back the original size, which is why it is called same-padding.

Watch out

  • Don't forget the +1. Three stops, not two — you count the stops, not the gaps between them. Dropping the +1 is the classic off-by-one here.
  • It's 2P, not P. Padding is added on both sides, so it grows the width by twice the padding. Easy to halve by accident.
  • The floor drops the last partial window. When S doesn't divide (W−F+2P) evenly, the leftover step doesn't fit and is dropped — not rounded up — so the trailing row or column gets no window of its own, and no error is raised. (And "same-padding" only keeps the size when S=1; with a larger stride the layer still shrinks.)

Count the kernel's stops — floored, plus one — and that's your feature map, in each dimension.

Go deeper

Das ist die eine Formel, die dir sagt, wie groß die Ausgabe einer Faltung wird — noch bevor du sie ausführst. Gib ihr Eingabegröße, Kernelgröße, Stride und Padding, und sie liefert Breite und Höhe der Feature Map.

Wo es vorkommt

Was es tut

Es zählt, an wie vielen Stellen der Kernel anhalten kann, während er über die Eingabe gleitet. Diese Anzahl ist die Ausgabebreite (und genauso die Höhe). Keine Gewichte, kein Training — reine Geometrie eines Fensters, das über ein Gitter wandert.

Die Ausgabegröße ist nichts anderes als die Zahl der Stellen, an denen der Kernel stehen kann. Zähl die Stopps, und du hast sie schon.

Wie es funktioniert

Leg den F breiten Kernel auf eine W breite Zeile. Der erste Stopp setzt seine linke Kante auf Position 0; der letzte, der noch ganz passt, hat die linke Kante bei W−F. Springst du je um S, liegen die Stopps bei 0, S, 2S, … bis W−F — die Anzahl ist also (W−F)/S, plus eins für den allerersten Stopp. Padding legt P Randzellen an jede Seite, die Breite wächst also um 2P. Zusammengesetzt und abgerundet:

out = ⌊(W − F + 2P) / S⌋ + 1

W=5, F=3, P=0
  S=1  →  Stopps bei 0,1,2    out = ⌊(5−3+0)/1⌋+1 = 3   → 3×3
  S=2  →  Stopps bei 0,2      out = ⌊(5−3+0)/2⌋+1 = 2   → 2×2
                                (ein Stopp bei 4 ragt über den Rand → Abrunden wirft ihn weg)

W=5, F=3, P=1
  S=1  →  out = ⌊(5−3+2)/1⌋+1 = 5   → 5×5   ("gleiche" Größe)

Das Abrunden ⌊⌋ ist keine Kosmetik: ein halber Stopp am Ende — wo der Kernel über den Rand ragen würde — zählt einfach nicht. Und P=1 mit 3×3-Kernel gibt die ursprüngliche Größe zurück — deshalb heißt es Same-Padding.

Worauf achten

  • Das +1 nicht vergessen. Drei Stopps, nicht zwei — du zählst die Stopps, nicht die Lücken dazwischen. Das +1 wegzulassen ist hier der klassische Off-by-one.
  • Es ist 2P, nicht P. Padding kommt auf beide Seiten, wächst die Breite also um das Doppelte des Paddings. Leicht versehentlich halbiert.
  • Das Abrunden wirft das letzte halbe Fenster weg. Teilt S die Zahl (W−F+2P) nicht glatt, passt der Reststopp nicht und fällt weg — er wird nicht aufgerundet —, die letzte Zeile oder Spalte bekommt also kein eigenes Fenster, und es kommt kein Fehler. (Und "Same-Padding" hält die Größe nur bei S=1; bei größerem Stride schrumpft die Schicht trotzdem.)

Zähl die Stopps des Kernels — abgerundet, plus eins — und das ist deine Feature Map, in jeder Dimension.

Mehr dazu

Next up
Frontend & DesignHSL Color Mixer Frontend & DesignCubic-Bezier Easing All concepts →