Speed

One Output Cell

Slice one receptive field patch, multiply elementwise by the filter, sum the products, and add the scalar bias once.

The problem

A feature map can look like an opaque block of numbers until you isolate one entry and trace every pixel that feeds it. Each entry (r, c) in a stride-one, unpadded feature map depends on a single receptive field: the K by K patch starting at row r and column c of the input table. Pixels outside that rectangle have zero influence on cell (r, c).

Inside that window, the layer performs a weighted sum plus one bias scalar b. Every filter weight multiplies its aligned pixel, all K squared products add together into a single number, and b shifts that sum once at the end. Adding b to every pixel product before summing is a frequent arithmetic slip that multiplies the bias by the window area.

Work through the numbers

Consider the four-by-four input [[2, 1, 0, 3], [1, 4, 2, 1], [0, 2, 3, 2], [3, 1, 0, 4]] with the two-by-two diagonal kernel [[2, -1], [-1, 2]] and scalar bias -1. Select output coordinates row = 1 and col = 2. That window spans input rows 1 and 2 and columns 2 and 3, extracting the patch [[2, 1], [3, 2]].

Multiply the patch and kernel entry by entry: top-left 2 * 2 = 4, top-right 1 * -1 = -1, bottom-left 3 * -1 = -3, and bottom-right 2 * 2 = 4. Those four products form [[4, -1], [-3, 4]], whose sum is 4 - 1 - 3 + 4 = 4. Adding the single scalar bias -1 produces the output cell value 3.

Had you added -1 to each of the four products before summing, you would have subtracted 4 instead of 1 and obtained 0. Moving the window to row = 0 and col = 0 covers [[2, 1], [1, 4]], whose products sum to 10 and give 9 after bias -1; at row = 2 and col = 0, products sum to -3 and give -4.

The live model

The picture highlights the active two-by-two receptive field on the four-by-four input, displays its four elementwise products in the center, and places the finished scalar at (row, col) of the three-by-three output grid. Adjust row, col, and bias: raising bias from -1 to 2 at (1, 2) lifts the cell from 3 to 6 while leaving the four pixel products [4, -1, -3, 4] unchanged.

The two-by-two patch [[2,1],[3,2]] highlighted at row 1, col 2 of the four-by-four input, products [[4,-1],[-3,4]], bias -1, and output cell 3.
let r = int(scene.parameter("row", 1.0, 0.0, 2.0, 1.0));
let c = int(scene.parameter("col", 2.0, 0.0, 2.0, 1.0));
let bias = scene.parameter("bias", -1.0, -2.0, 2.0, 0.5);
let kernel = torch.tensor("[[2.0, -1.0], [-1.0, 2.0]]", DType.F64);
let patch = input.slice(0, r, r + 2).slice(1, c, c + 2);
let products = patch * kernel;
let dot = products.sum();
let answer = (dot + bias).reshape(1);
out.set("patch", patch).set("products", products).set("cell", answer);
{
  "patch": [
    [
      2,
      1
    ],
    [
      3,
      2
    ]
  ],
  "products": [
    [
      4,
      -1
    ],
    [
      -3,
      4
    ]
  ],
  "cell": [
    3
  ]
}

Your turn

At row = 1, col = 2, and bias = -1, the starter outputs [0] instead of [3]: shifting all four window products gives 4 - 4 = 0, applying the channel bias once per pixel rather than once for the receptive field. Compute the cell so the four products sum before the scalar bias shifts the total. Check tests new pixel tables across varied row, col, and bias settings and rejects missing bias, swapped slices, and constants.

The receptive field at row 1, col 2 with products [[4,-1],[-3,4]] and starter cell value 0 caused by adding bias -1 four times.
let r = int(scene.parameter("row", 1.0, 0.0, 2.0, 1.0));
let c = int(scene.parameter("col", 2.0, 0.0, 2.0, 1.0));
let bias = scene.parameter("bias", -1.0, -2.0, 2.0, 0.5);
let kernel = torch.tensor("[[2.0, -1.0], [-1.0, 2.0]]", DType.F64);
let patch = input.slice(0, r, r + 2).slice(1, c, c + 2);
let products = patch * kernel;
let answer = (products + bias).sum().reshape(1);
out.set("products", products).set("cell", answer);
{
  "products": [
    [
      4,
      -1
    ],
    [
      -3,
      4
    ]
  ],
  "cell": [
    0
  ]
}

Compute the single output cell at (row, col) so the four pixel-weight products are summed first and the scalar bias is added once to that sum.

Read the result carefully

Writing a single cell by slicing makes two structural properties plain. First, spatial locality is strict in one layer: changing pixel (0, 0) never alters cell (1, 2) because (0, 0) lies outside rows 1..2 and columns 2..3. Second, one output channel owns one scalar bias shared across every window position, not one bias per weight or one bias per pixel location.

Recap

  • Output cell (r, c) with stride one and no padding reads input rows r through r + K - 1 and columns c through c + K - 1.
  • Sum all K squared pixel-weight products first, then add the channel's scalar bias once.
  • Changing bias shifts every output cell equally without changing any pixel-weight product.