Speed

What a Convolution Is

Slide the fixed two-by-two kernel over the four-by-four table, verify every signed entry of the three-by-three feature map, and check the same convolution on new pixel tables.

The problem

A convolutional layer moves the same small array of weights across an image. At each position it multiplies the weights by the covered pixels and adds the products. The result is a feature map: one number per window position. Weight sharing means the same local pattern can be detected in different parts of the image.

Our image is a four-by-four table, with one input channel. The two-by-two kernel is [[1, -1], [1, -1]], and the bias is zero. We move the window one pixel at a time and add no padding. Machine-learning libraries normally call this operation convolution even though they apply the kernel as written, without flipping it; mathematically that convention is cross-correlation.

Work through the numbers

The first window contains [[1, 2], [0, 1]]. Its products with the kernel are 1, -2, 0, and -1, giving the first output value -2. Moving one column right covers [[2, 0], [1, 3]], whose products sum to 0. Moving right again gives 0. These are the three entries of the first output row.

A width-four input and width-two kernel have three valid horizontal starts: 0, 1, and 2. The same count holds vertically, so the feature map is three by three. In general, with stride one and no padding, each output dimension is input size minus kernel size plus one.

The kernel subtracts the right column from the left column in each window. A positive response means the covered left pixels have a larger sum; a negative response means the reverse. No activation has been applied yet. Clipping negative values with ReLU would be a separate step and would discard some of this signed information.

The live model

The recorded picture shows the input, the fixed kernel, and the computed feature map with signed values. Run initializes a tiny layer, replaces every parameter with the stated weights and zero bias, and performs its forward pass. There is no random-weight dependence despite the layer having an initializer.

A four-by-four input, the kernel [[1,-1],[1,-1]], and a signed three-by-three feature map whose first row is [-2,0,0].
torch.grad_enabled(false);
let model = torch.sequential().conv2d(1, 1, 2);
let weight = model.param("0.weight");
let kernel = torch.tensor("[1.0,-1.0,1.0,-1.0]");
for (i in range(0, 4)) { 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(3, 3);
out.set("feature_map", answer);
{
  "feature_map": [
    [
      -2,
      0,
      0
    ],
    [
      1,
      -3,
      1
    ],
    [
      1,
      1,
      -3
    ]
  ]
}

Your turn

Replace the averaging-style starter kernel with the stated left-minus-right kernel. Keep the zero bias and valid stride-one geometry. Check uses different pixel tables, so returning the recorded map, swapping the sign, or using only one row of the kernel will fail.

A four-by-four input, the all-one starter kernel, and its positive three-by-three window sums.
torch.grad_enabled(false);
let model = torch.sequential().conv2d(1, 1, 2);
let weight = model.param("0.weight");
let kernel = torch.tensor("[1.0,1.0,1.0,1.0]");
for (i in range(0, 4)) { 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(3, 3);
out.set("feature_map", answer);
{
  "feature_map": [
    [
      4,
      6,
      6
    ],
    [
      3,
      5,
      7
    ],
    [
      5,
      3,
      5
    ]
  ]
}

Replace the averaging-style starter kernel with the stated left-minus-right kernel. Keep the zero bias and valid stride-one geometry. Check uses different pixel tables, so returning the recorded map, swapping the sign, or using only one row of the kernel will fail.

Read the result carefully

One kernel and one channel make the window arithmetic easy to check. With several channels, each output cell adds the channel-wise dot products before applying any activation. A complete convnet can learn kernels and stack feature maps, but neither training nor pattern recognition is established merely by sliding this fixed filter.

Recap

  • Each feature-map entry is one window's signed dot product with the shared kernel [[1, -1], [1, -1]]: the top row [-2, 0, 0] comes from the three windows of the top band.
  • A width-four input and width-two kernel have 4 - 2 + 1 = 3 valid starts per axis, so the map is three by three.
  • Check on new pixel tables distinguishes the shared left-minus-right kernel from a memorized map, a sign flip, or a one-row kernel.