Answers · How models work
Why does a convolution make the image smaller if you don't pad it?
In shortA convolution shrinks the image because the window must fit completely inside it, so the first and last few positions have no full window. Padding adds extra values at the border so those positions can be used.
Because a window has to sit entirely inside the picture, the positions where it would hang over an edge are not valid placements. A row of 100 values with a three-wide window gives 98 outputs, not 100, because only starts 1 through 98 have three values available; starts 99 and 100 would need values beyond the row. The same count happens independently down the other axis, so a 32 by 32 image with a 3 by 3 window becomes 30 by 30.
The formula is a count of starting positions. The numerator is the width after padding, subtracting leaves the span over which a start can occur without the window overhanging, dividing by counts only the starts actually used, and the final is the first start. The floor is not decoration: if the span is not an exact multiple of the stride, the last partial step has no complete window and is discarded. For , , , , the span is , so there are outputs, at starts 1, 3, 5, 7; a start at 9 would need values 9, 10, 11 and value 11 does not exist.
Counting complete windows
With stride one and no padding the count along one axis is : the first start is at the first value, and every start after that moves the window one place. For and that is . In two dimensions the two axes are counted separately, so in each direction. Stack twenty such layers and each axis loses positions: . Nothing has been thrown away by the arithmetic; there are simply fewer legal window positions.
| input size | window | padding | stride | output size |
|---|---|---|---|---|
| 32 | 3 | 0 | 1 | 30 |
| 32 | 3 | 1 | 1 | 32 |
| 32 | 3 | 1 | 2 | 16 |
| 32 | 5 | 2 | 2 | 16 |
| 32 | 3 | 0 | 3 | 10 |
Read the first row and the second row together. The first has no padding, so a 32 input with a 3 window gives 30. The second puts one value on each side, so the padded width is , and : the two lost positions are returned. The third row keeps that padding but moves two at a time, so the span is still 31 but only every second start is used, giving . The fourth row uses a five-wide window and padding of two, so the padded width is , and with stride two we get . The fifth row removes padding and moves three at a time: .
Padding is not only about size
The common mistake is to treat padding as a way of keeping the output the same size and nothing more. It does that, but it also changes how often each input value is read. With a window and no padding, a corner value belongs to exactly one window, because only the window whose top-left corner sits on that corner can contain it. An edge value away from the corner belongs to three windows: the start row is fixed to the edge, and the start column can be one of three. An interior value belongs to nine: three possible start rows times three possible start columns. Those counts are 1, 3 and 9. The border is therefore contributing less evidence than the middle, and padding is what raises its count. Padding of one takes a corner from one window to four and an edge from three to six, which narrows the gap; full padding by would give every position the same count.
Where the rule stops
The size-preserving value assumes an odd window and a stride of one. An even window has no centre position, so it cannot be padded symmetrically and the output is offset by half a pixel. A stride above one shrinks the map even when the padding is size-preserving: with , , and , the formula gives . The floor is where a final partial step is dropped. If the stride does not divide the span, the last strip of the input is read by no window, so two implementations can differ by one pixel without either being obviously wrong. Zero padding is also a fabrication: a filter half over real pixels and half over zeros sees a strong edge that is not in the picture, which puts a bright frame around feature maps unless the padding is reflective or replicated.
Worth keeping
- Without padding, a k-wide window has n_in - k + 1 full placements along an axis, so a 100-wide row with a 3-wide window gives 98 outputs.
- Padding of (k-1)/2 restores the input size only for odd k and stride one; with stride two the same padding halves a 32-wide input to 16.
- Zero padding does not just add empty space: it gives edge filters a fabricated strong edge, which trained networks can learn to use as a position signal.
Where this goes next
This question is one step of How a Filter Reads a Picture, a course that works through its subject from the beginning. The page that teaches this part of it is Edges, and Skipping Positions, and it reads the whole way through whether or not you have an account.
What an account changes is the practice: problems standing under every idea, marked with a reason rather than a tick, in a copy of the course that is yours.
Read that pageThe whole courseOther questions people ask about this
- What do you give up when you pool or downsample a feature map?You give up exact position: each block of the map becomes one number, so later layers see a coarser grid, gain reach, and lose the ability to say where a response came from.
- What does it mean to use stride 2 in a convolution?Using stride 2 keeps the filter the same but moves its window two positions at a time, so it produces about half as many outputs in each direction and about a quarter of the work in two dimensions.
- Why do CNNs use the same weights everywhere instead of learning a separate filter for each spot?They share one small filter across every position because a pattern such as an edge means the same thing wherever it appears; the huge cut in parameters is a consequence, not the main reason.
- What does a convolution actually calculate at a single point in the image?It multiplies each filter weight by the pixel beneath it, adds those products, and writes the single total at the matching position in the output.
- Why does the model get stuck repeating the same sentence over and over?The model is not stuck in a memory loop: once it has written a phrase, that phrase is in its context and makes writing it again more likely, so a decoder that always picks the top word cannot leave.
- Why does diffusion add Gaussian noise instead of any random corruption?Diffusion adds Gaussian noise because Gaussian errors combine exactly: each step keeps the spread fixed and gives a closed-form noisy picture at any later step, which makes the reverse step learnable.