ReLU on the Feature Map
Apply max(0, x) cell by cell to clamp negative anti-matches to zero while keeping positive match strengths intact.
The problem
A raw convolutional map is a signed linear table: positive entries mark windows that align with the filter, zero marks neutral balance, and negative entries mark windows where the pattern is inverted. Stacking linear convolutions without a nonlinearity collapses the entire stack into one equivalent linear filter. Such a filter answers a pattern and its exact opposite with equal strength and opposite sign; it can never respond to one and stay silent on the other.
Applying the rectified linear unit (ReLU(x) = max(0, x)) cell by cell introduces that asymmetric gate without changing the tensor's spatial shape. Positive scores pass through with their exact magnitudes intact, while zero and negative scores clamp to 0.
Work through the numbers
Start with the four-by-four input [[3, 1, 0, 2], [2, 0, 1, 1], [0, 3, 2, 0], [1, 1, 0, 3]] and the two-by-two diagonal-contrast kernel [[1, -2], [2, -1]] at bias = 0 and no padding.
On the top row of windows, (0, 0) computes 1*3 - 2*1 + 2*2 - 1*0 = 5, (0, 1) computes 1*1 - 2*0 + 2*0 - 1*1 = 0, and (0, 2) computes 1*0 - 2*2 + 2*1 - 1*1 = -3. Evaluating the middle and bottom rows produces the full signed three-by-three table [[5, 0, -3], [-1, 2, 3], [-5, 1, -1]].
Applying max(0, x) to each of the nine cells keeps the four positive entries (5, 2, 3, 1), leaves (0, 1) at 0, and clamps the four negative entries (-3, -1, -5, -1) to 0, returning [[5, 0, 0], [0, 2, 3], [0, 1, 0]].
The live model
The picture displays the signed pre-ReLU map beside the rectified three-by-three output. Slide bias between -3 and 3: at 0 four cells stay active ([[5, 0, 0], [0, 2, 3], [0, 1, 0]]); shifting bias down to -3 clamps eight of nine cells to zero so only [[2, 0, 0], [0, 0, 0], [0, 0, 0]] survives; raising bias to 3 wakes up nearly every cell to give [[8, 3, 0], [2, 5, 6], [0, 4, 2]].
![Signed three-by-three map [[5,0,-3],[-1,2,3],[-5,1,-1]] beside its ReLU output [[5,0,0],[0,2,3],[0,1,0]].](../assets/ml/relu-on-the-feature-map--demo.png)
torch.grad_enabled(false);
let bias = scene.parameter("bias", 0.0, -3.0, 3.0, 1.0);
let conv = torch.sequential().conv2d(1, 1, 2);
let k = torch.tensor("[1.0,-2.0,2.0,-1.0]");
for (i in range(0, 4)) { conv.param("0.weight").set(i, k.value(i)); }
conv.param("0.bias").set(0, bias);
let x = input.to(DType.F32).reshape(1, 1, 4, 4);
let pre_relu = conv.forward(x).reshape(3, 3);
let answer = pre_relu.relu();
out.set("bias", bias).set("pre_relu", pre_relu).set("activated", answer);
{
"bias": 0,
"pre_relu": [
[
5,
0,
-3
],
[
-1,
2,
3
],
[
-5,
1,
-1
]
],
"activated": [
[
5,
0,
0
],
[
0,
2,
3
],
[
0,
1,
0
]
]
}Your turn
The starter outputs [[5, 0, 3], [1, 2, 3], [5, 1, 1]] instead of [[5, 0, 0], [0, 2, 3], [0, 1, 0]]: the four negative mismatches (-3, -1, -5, -1) flip into positive detections (3, 1, 5, 1), treating an inverted pattern at (2, 0) identically to the true +5 match at (0, 0). Clamp negative scores to 0 while keeping positive magnitudes intact. Check tests unseen four-by-four grids and rejects unrectified maps, binary steps, and hardcoded tables.
![Signed three-by-three map [[5,0,-3],[-1,2,3],[-5,1,-1]] beside the starter absolute-value output [[5,0,3],[1,2,3],[5,1,1]].](../assets/ml/relu-on-the-feature-map--exercise.png)
torch.grad_enabled(false);
let conv = torch.sequential().conv2d(1, 1, 2);
let k = torch.tensor("[1.0,-2.0,2.0,-1.0]");
for (i in range(0, 4)) { conv.param("0.weight").set(i, k.value(i)); }
conv.param("0.bias").set(0, 0.0);
let x = input.to(DType.F32).reshape(1, 1, 4, 4);
let pre_relu = conv.forward(x).reshape(3, 3);
let answer = pre_relu.abs();
out.set("pre_relu", pre_relu).set("activated", answer);
{
"pre_relu": [
[
5,
0,
-3
],
[
-1,
2,
3
],
[
-5,
1,
-1
]
],
"activated": [
[
5,
0,
3
],
[
1,
2,
3
],
[
5,
1,
1
]
]
}Transform pre_relu cell by cell so negative mismatches clamp to zero while positive scores keep their exact magnitudes.
Read the result carefully
Compare ReLU against two tempting substitutes. Absolute value (|x|) treats an inverted edge -5 identically to a true match +5, erasing polarity. Binary thresholding (x > 0) keeps polarity yet flattens 5, 3, and 1 to the same 1, throwing away contrast strength. Also note the training consequence: wherever pre_relu <= 0, the local slope of max(0, x) is 0, so clamped cells pass zero gradient backward on that example.
Recap
ReLUacts independently on every(c, r, col)entry without altering channel count, height, or width.- Positive scores keep their exact magnitudes while negative mismatches clamp to
0, preserving both polarity and contrast strength. - Shifting the scalar bias moves every pre-ReLU cell together, controlling how sparse the rectified map becomes and which cells carry non-zero gradient.