Speed

Zero Padding

Add a border of zeros around an image so a three-by-three filter preserves spatial size and touches edge pixels fairly.

The problem

Without a border, sliding a filter of width three across a four-by-four image leaves room for only two horizontal starts and two vertical starts. A single layer shrinks the four-by-four grid down to two by two, and a second such layer could not run at all, because a three-by-three window does not fit on a two-by-two grid. At the same time, corner pixels enter only one window while interior pixels enter four windows, underweighting information near the edges.

Zero padding surrounds the image with p rings of zeros before the window slides. Together the rings add 2p rows and 2p columns to the effective table. With stride one and kernel size K, an input of side H produces an output of side H + 2p - K + 1. Choosing p = 1 for a three-by-three filter keeps a four-by-four input at four by four.

Work through the numbers

Take the four-by-four input [[3, 2, 1, 0], [1, 2, 0, 1], [0, 1, 2, 1], [2, 0, 1, 3]] and the three-by-three horizontal-edge kernel [[1, 1, 1], [0, 0, 0], [-1, -1, -1]] with zero bias. Without padding (p = 0), only two-by-two interior centers fit: the top-left window sums 3 + 2 + 1 minus 0 + 1 + 2 to give 3, and the full unpadded map is [[3, -1], [0, -1]].

Now set p = 1 so a six-by-six padded table wraps the pixels in zeros. At output cell (0, 0), the kernel's top row multiplies three padded zeros while its bottom row subtracts 0 + 1 + 2, producing -3. Across the top output row the four values are [-3, -3, -3, -1]. At (1, 0), the top kernel row catches 0 + 3 + 2 = 5 and the bottom row subtracts 0 + 0 + 1 = 1, giving 4.

Notice the central two-by-two block at rows 1..2 and columns 1..2 of the padded four-by-four output: [[3, -1], [0, -1]]. Those windows lie completely inside the real image, so padding never alters interior responses. It appends a one-cell frame around them, producing [[4, 3, -1, -2], [1, 0, -1, -3], [1, 3, 4, 3]] across the remaining three rows.

The live model

The diagram renders the active zero border in grey around the four-by-four input, the shared three-by-three filter, and the resulting feature map. Move pad from 0 to 1 and 2: output side length grows from 2 to 4 to 6, and at pad = 2 the six-by-six map begins with top row [-3, -5, -6, -3, -1, 0]. Every interior block stays nested inside the larger map.

A six-by-six zero-padded input grid, the three-by-three horizontal-edge kernel, and a four-by-four feature map whose top row is [-3,-3,-3,-1].
torch.grad_enabled(false);
let pad = int(scene.parameter("pad", 1.0, 0.0, 2.0, 1.0));
let model = torch.sequential().conv2d(1, 1, 3, 1, pad);
let weight = model.param("0.weight");
let kernel = torch.tensor("[1.0,1.0,1.0,0.0,0.0,0.0,-1.0,-1.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 = 4 + 2 * pad - 2;
let image = input.to(DType.F32).reshape(1, 1, 4, 4);
let answer = model.forward(image).reshape(side, side);
out.set("output_side", side).set("feature_map", answer);
{
  "output_side": 4,
  "feature_map": [
    [
      -3,
      -3,
      -3,
      -1
    ],
    [
      4,
      3,
      -1,
      -2
    ],
    [
      1,
      0,
      -1,
      -3
    ],
    [
      1,
      3,
      4,
      3
    ]
  ]
}

Your turn

The starter outputs the two-by-two interior table [[3, -1], [0, -1]] instead of the full four-by-four map [[-3, -3, -3, -1], [4, 3, -1, -2], [1, 0, -1, -3], [1, 3, 4, 3]], losing all twelve border cells because no zero ring wraps the input before the three-by-three filter slides. Add one ring of zero padding at stride 1 so the feature map preserves the four-by-four grid. Returning a two-by-two map, two zero rings, reversed signs, or a constant table will fail.

An unpadded four-by-four input, the three-by-three horizontal-edge kernel, and the shrunken two-by-two starter feature map [[3,-1],[0,-1]].
torch.grad_enabled(false);
let model = torch.sequential().conv2d(1, 1, 3);
let weight = model.param("0.weight");
let kernel = torch.tensor("[1.0,1.0,1.0,0.0,0.0,0.0,-1.0,-1.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, 4, 4);
let answer = model.forward(image).reshape(2, 2);
out.set("feature_map", answer);
{
  "feature_map": [
    [
      3,
      -1
    ],
    [
      0,
      -1
    ]
  ]
}

Change the convolution so it uses stride 1 with one ring of zero padding, keeping the horizontal-edge weights and zero bias, and reshape the resulting feature map to four by four.

Read the result carefully

Preserving height and width makes deep architectures practical, yet injected zeros are not true scene measurements. Along the top output row [-3, -3, -3, -1], every score is negative partly because the filter sees an artificial dark row of zeros above bright image pixels [3, 2, 1, 0]. Border cells therefore mix real contrast with boundary assumptions, and downstream layers can learn position cues from those synthetic zero edges.

Recap

  • Adding p zero rings changes stride-one output length from H - K + 1 to H + 2p - K + 1.
  • For an odd kernel size K, setting p = (K - 1) / 2 preserves the input height and width.
  • Zero padding leaves wholly interior window values unchanged while introducing synthetic zero contrast along borders.