Speed

The Feature Map

Run two distinct two-by-two filters across one image to stack a vertical-edge map and a horizontal-edge map.

The problem

One convolutional filter searches for a single spatial pattern everywhere in the image. A vertical-edge detector responds strongly across left-to-right jumps in brightness, yet stays blind to top-to-bottom jumps of the exact same contrast. Collapsing both directions into one filter would mix vertical and horizontal edges into ambiguous scores.

A convolutional layer solves this by holding a bank of C_out independent filters and C_out independent scalar biases — one bias per filter. Each filter slides over the input with its own weights and bias to write its own two-dimensional feature map, so C_out filters on one four-by-four image with two-by-two windows produce a stacked (C_out, 3, 3) output tensor where channel index selects the detector and (r, c) selects the location.

Work through the numbers

Take the four-by-four quadrant image [[4, 4, 1, 1], [4, 4, 1, 1], [0, 0, 2, 2], [0, 0, 2, 2]] with no padding and both filter biases set to 0 (8 weights plus 2 biases, 10 parameters in total). Run two two-by-two filters: Filter 0 is [[1, -1], [1, -1]] (left column minus right column) and Filter 1 is [[1, 1], [-1, -1]] (top row minus bottom row).

Filter 0 cancels inside constant columns, leaving zeros in output columns 0 and 2. Along middle column 1, where input columns 1 and 2 meet, its three windows score (4 - 1) + (4 - 1) = 6, (4 - 1) + (0 - 2) = 1, and (0 - 2) + (0 - 2) = -4, producing Map 0 [[0, 6, 0], [0, 1, 0], [0, -4, 0]].

Filter 1 cancels wherever two adjacent rows match, giving zeros across top row 0 and bottom row 2. Across middle row 1, where input rows 1 and 2 meet, its three windows score (4 + 4) - (0 + 0) = 8, (4 + 1) - (0 + 2) = 3, and (1 + 1) - (2 + 2) = -2, producing Map 1 [[0, 0, 0], [8, 3, -2], [0, 0, 0]].

The live model

The picture shows the four-by-four input alongside both three-by-three output maps. Drag h_scale to rescale Filter 1: Map 0 stays fixed at [[0, 6, 0], [0, 1, 0], [0, -4, 0]] because Filter 0 never changes, whereas Map 1's middle row [8, 3, -2] scales to [16, 6, -4] at h_scale = 2, collapses to [0, 0, 0] at 0, and flips to [-8, -3, 2] at -1.

A four-by-four quadrant image with two two-by-two filters producing Map 0 [[0,6,0],[0,1,0],[0,-4,0]] and Map 1 [[0,0,0],[8,3,-2],[0,0,0]].
torch.grad_enabled(false);
let h_scale = scene.parameter("h_scale", 1.0, -1.0, 2.0, 0.5);
let model = torch.sequential().conv2d(1, 2, 2);
let weights = torch.tensor("[1.0,-1.0,1.0,-1.0,1.0,1.0,-1.0,-1.0]");
weights.set(4, h_scale);
weights.set(5, h_scale);
weights.set(6, -h_scale);
weights.set(7, -h_scale);
let w_param = model.param("0.weight");
for (i in range(0, 8)) { w_param.set(i, weights.value(i)); }
let b_param = model.param("0.bias");
b_param.set(0, 0.0);
b_param.set(1, 0.0);
let image = input.to(DType.F32).reshape(1, 1, 4, 4);
let answer = model.forward(image).reshape(2, 3, 3);
out.set("h_scale", h_scale).set("feature_maps", answer);
{
  "h_scale": 1,
  "feature_maps": [
    [
      [
        0,
        6,
        0
      ],
      [
        0,
        1,
        0
      ],
      [
        0,
        -4,
        0
      ]
    ],
    [
      [
        0,
        0,
        0
      ],
      [
        8,
        3,
        -2
      ],
      [
        0,
        0,
        0
      ]
    ]
  ]
}

Your turn

The starter outputs [[0, 6, 0], [0, 1, 0], [0, -4, 0]] in Map 1 instead of [[0, 0, 0], [8, 3, -2], [0, 0, 0]]: repeating vertical-edge responses in both channels leaves the horizontal jump along middle row 1 undetected ([0, 1, 0] instead of [8, 3, -2]). Make Channel 1 detect top-minus-bottom contrast while keeping Channel 0 as left-minus-right contrast. Check tests new four-by-four inputs and rejects swapped channel order, summed maps, and hardcoded outputs.

The four-by-four quadrant image with duplicate vertical-edge starter filters producing two identical vertical feature maps.
torch.grad_enabled(false);
let model = torch.sequential().conv2d(1, 2, 2);
let weights = torch.tensor("[1.0,-1.0,1.0,-1.0,1.0,-1.0,1.0,-1.0]");
let w_param = model.param("0.weight");
for (i in range(0, 8)) { w_param.set(i, weights.value(i)); }
let b_param = model.param("0.bias");
b_param.set(0, 0.0);
b_param.set(1, 0.0);
let image = input.to(DType.F32).reshape(1, 1, 4, 4);
let answer = model.forward(image).reshape(2, 3, 3);
out.set("feature_maps", answer);
{
  "feature_maps": [
    [
      [
        0,
        6,
        0
      ],
      [
        0,
        1,
        0
      ],
      [
        0,
        -4,
        0
      ]
    ],
    [
      [
        0,
        6,
        0
      ],
      [
        0,
        1,
        0
      ],
      [
        0,
        -4,
        0
      ]
    ]
  ]
}

Keep Filter 0 as the vertical-edge detector and set Filter 1 so its top row has plus-one weights and its bottom row has minus-one weights.

Read the result carefully

Notice how the two output maps share the same three-by-three coordinate grid. Position (1, 1) sits on the four-way corner [4, 1; 0, 2], so Map 0 records 1 for weak net vertical contrast while Map 1 records 3 for stronger downward contrast at the exact same spot. Downstream layers read all channels at (r, c) together as a local feature vector describing what patterns overlap there.

Recap

  • Each filter in a convolutional layer produces its own spatial feature map, stacking C_out maps along the output channel axis.
  • Independent filter weights keep distinct detectors separate: scaling or flipping Filter 1 leaves Map 0 untouched.
  • Matching (r, c) coordinates across output maps describe different patterns measured on the exact same input window.