Speed

Filter ReLU Pool Keeps the Signal Visible

Chain a three-by-three detector, ReLU, and two-by-two max-pooling so sparse bar peaks survive downsampling undiluted.

The problem

A narrow visual feature — such as a bright vertical stroke only one pixel wide — excites a single column of windows and leaves adjacent windows negative or zero. If a network downsamples that map by averaging neighboring cells, three silent neighbors dilute the one active response to a quarter of its strength, burying crisp detections in background clutter.

Classic convolutional blocks avoid that dilution by chaining three operations in order: filter, ReLU, and max-pooling. Filtering scores local matches with signed weights, ReLU zeroes negative side-lobes and background mismatch, and two-by-two max-pooling forwards the strongest surviving activation from each quadrant at full amplitude.

Work through the numbers

Inspect the six-by-six grid [[0, 4, 0, 0, 0, 0], [0, 4, 0, 0, 1, 0], [0, 0, 0, 0, 1, 0], [2, 0, 0, 0, 0, 0], [2, 0, 0, 0, 2, 0], [2, 0, 0, 0, 2, 0]]. Apply the three-by-three center-bar kernel [[-1, 2, -1], [-1, 2, -1], [-1, 2, -1]] with bias = -1 and no padding.

Across the top three rows, column sums are [0, 8, 0, 0, 2, 0], giving raw window scores [16, -8, -2, 4] and biased scores [15, -9, -3, 3]. Repeating on the remaining three row bands produces the four-by-four biased map [[15, -9, -3, 3], [5, -5, -3, 3], [-5, -1, -4, 5], [-7, -1, -5, 7]].

ReLU clips every negative entry to zero, leaving [[15, 0, 0, 3], [5, 0, 0, 3], [0, 0, 0, 5], [0, 0, 0, 7]]. Two-by-two max-pooling then takes the peak of each quadrant to return [[15, 3], [0, 7]], whereas quadrant averaging dilutes those same quadrants to [[5, 1.5], [0, 3]].

The live model

The picture shows the six-by-six input, the three-by-three bar filter, the rectified four-by-four map split into four two-by-two tiles, and the pooled two-by-two summary. Adjust bias: at -1 the output is [[15, 3], [0, 7]]; at -4 the faint top-right streak drops out so [[12, 0], [0, 4]] remains; at 1 empty windows rise above zero and lift the silent bottom-left tile to 1 ([[17, 5], [1, 9]]).

A six-by-six grid, three-by-three bar filter, rectified four-by-four map with four outlined two-by-two tiles, and pooled output [[15,3],[0,7]].
torch.grad_enabled(false);
let bias = scene.parameter("bias", -1.0, -4.0, 1.0, 1.0);
let model = torch.sequential().conv2d(1, 1, 3).relu().max_pool2d(2);
let k = torch.tensor("[-1.0,2.0,-1.0,-1.0,2.0,-1.0,-1.0,2.0,-1.0]");
for (i in range(0, 9)) { model.param("0.weight").set(i, k.value(i)); }
model.param("0.bias").set(0, bias);
let x = input.to(DType.F32).reshape(1, 1, 6, 6);
let answer = model.forward(x).reshape(2, 2);
out.set("bias", bias).set("pooled", answer);
{
  "bias": -1,
  "pooled": [
    [
      15,
      3
    ],
    [
      0,
      7
    ]
  ]
}

Your turn

With bias = -1, the starter produces [[5, 1.5], [0, 3]] instead of [[15, 3], [0, 7]]: averaging each rectified quadrant shrinks the top-left peak 15 to 5 and the bottom-right peak 7 to 3, diluting sparse detections with silent neighbors. Change the pooling stage so each two-by-two quadrant forwards its strongest rectified score undiluted. Check tests unseen six-by-six inputs and rejects unrectified pooling, absolute-value activation, row-sum pooling, and hardcoded summaries.

Six-by-six input, three-by-three bar filter, rectified four-by-four tiles, and starter quadrant averages [[5,1.5],[0,3]] instead of [[15,3],[0,7]].
torch.grad_enabled(false);
let conv = torch.sequential().conv2d(1, 1, 3);
let k = torch.tensor("[-1.0,2.0,-1.0,-1.0,2.0,-1.0,-1.0,2.0,-1.0]");
for (i in range(0, 9)) { conv.param("0.weight").set(i, k.value(i)); }
conv.param("0.bias").set(0, -1.0);
let x = input.to(DType.F32).reshape(1, 1, 6, 6);
let act = conv.forward(x).relu().reshape(4, 4);
let avg = torch.zeros(4);
for (q in range(0, 4)) {
  let r = (q / 2) * 2;
  let c = (q % 2) * 2;
  let s0 = act.value(r * 4 + c) + act.value(r * 4 + c + 1);
  let s1 = act.value((r + 1) * 4 + c) + act.value((r + 1) * 4 + c + 1);
  avg.set(q, 0.25 * (s0 + s1));
}
let answer = avg.reshape(2, 2);
out.set("pooled", answer);
{
  "pooled": [
    [
      5,
      1.5
    ],
    [
      0,
      3
    ]
  ]
}

Keep the three-by-three bar filter with bias -1 and rectification, and summarize each two-by-two quadrant by its largest entry.

Read the result carefully

Every stage in the trio protects the next one. Without ReLU, the bottom-left quadrant [[-5, -1], [-7, -1]] feeds -1 into max-pooling instead of 0, and replacing ReLU with absolute value turns that same quadrant's -7 into a false detection of 7. Filtering creates signed contrast, ReLU isolates positive matches, and max-pooling preserves peak strength while halving height and width.

With max-pooling the order of the last two steps does not change the numbers: the largest value of a tile is clipped the same way before or after pooling. With averaging the order would matter.

Recap

  • Chaining conv2d, relu, and max_pool2d converts a noisy pixel grid into a compact map of peak detections.
  • ReLU removes negative side-lobes and clutter before pooling so absent quadrants summarize cleanly to 0.
  • Max-pooling keeps a sparse feature's peak score undiluted, unlike quadrant averaging which divides an isolated peak by four.