Channels Add Before ReLU
Sum signed dot products across all input channels at each window position before applying ReLU once to the total.
The problem
Real convolutional layers rarely read a single greyscale sheet. Color images arrive with red, green, and blue channels whose pixel values can be negative after normalization, and hidden layers receive stacks of dozens of feature maps from the previous layer. When one output filter inspects a multi-channel input (C_in, H, W), it holds a three-dimensional weight block (C_in, K, K) — one two-dimensional slice per input channel — plus a single scalar bias.
At every window position (r, c), the filter computes a signed dot product on each input channel, adds all channel dot products and the bias together, and only then passes that combined scalar through ReLU. Because addition happens before rectification, negative evidence in one channel can cancel positive evidence in another.
Work through the numbers
Consider a two-channel three-by-three input with Channel 0 [[2, 1, 0], [0, 2, 1], [1, 0, 3]] and Channel 1 [[1, 2, 0], [1, 2, 1], [2, 0, 1]], no padding, and zero bias. Slice 0 of the filter is [[1, 1], [0, 1]] and Slice 1 is [[-1, 0], [-1, -1]].
On Channel 0, the four two-by-two windows sum top-left, top-right, and bottom-right entries: 2 + 1 + 2 = 5, 1 + 0 + 1 = 2, 0 + 2 + 0 = 2, and 2 + 1 + 3 = 6, giving m0 = [[5, 2], [2, 6]].
On Channel 1, Slice 1 negates top-left, bottom-left, and bottom-right entries: -(1 + 1 + 2) = -4, -(2 + 2 + 1) = -5, -(1 + 2 + 0) = -3, and -(2 + 0 + 1) = -3, giving m1 = [[-4, -5], [-3, -3]].
Adding across channels yields m0 + m1 = [[1, -3], [-1, 3]], and applying ReLU once produces [[1, 0], [0, 3]]. Rectifying each channel separately before adding would clip m1 to all zeros and return [[5, 2], [2, 6]], erasing Channel 1's inhibition completely.
The live model
The picture displays Channel 0, Channel 1, and the rectified two-by-two output. Move ch1_scale to rescale Slice 1: at -1 the output is [[1, 0], [0, 3]]; at -2 stronger inhibition drives every pre-ReLU sum to zero or below for [[0, 0], [0, 0]]; at 0 Channel 1 contributes nothing so [[5, 2], [2, 6]] passes through; and at 1 both channels excite together to produce [[9, 7], [5, 9]].
![Two three-by-three input channels combined by signed convolution and ReLU into the two-by-two output [[1,0],[0,3]].](../assets/ml/channels-add-before-relu--demo.png)
torch.grad_enabled(false);
let ch1_scale = scene.parameter("ch1_scale", -1.0, -2.0, 1.0, 0.5);
let model = torch.sequential().conv2d(2, 1, 2).relu();
let weights = torch.tensor("[1.0,1.0,0.0,1.0,-1.0,0.0,-1.0,-1.0]");
weights.set(4, ch1_scale);
weights.set(6, ch1_scale);
weights.set(7, ch1_scale);
let w_param = model.param("0.weight");
for (i in range(0, 8)) { w_param.set(i, weights.value(i)); }
model.param("0.bias").set(0, 0.0);
let x = input.to(DType.F32).reshape(1, 2, 3, 3);
let answer = model.forward(x).reshape(2, 2);
out.set("ch1_scale", ch1_scale).set("output", answer);
{
"ch1_scale": -1,
"output": [
[
1,
0
],
[
0,
3
]
]
}Your turn
The starter outputs [[5, 2], [2, 6]] instead of [[1, 0], [0, 3]]: clipping Channel 1's negative map [[-4, -5], [-3, -3]] to zeros before combining erases cross-channel inhibition at every window, leaving (0, 1) at 2 when 2 + (-5) should cancel below zero. Combine the signed channel contributions so opposite-sign evidence cancels before rectification. Check evaluates unseen two-channel inputs and rejects unrectified negative sums, single-channel clipping, single-channel outputs, and hardcoded tables.
![Two three-by-three input channels where separate rectification wrongly outputs [[5,2],[2,6]] instead of [[1,0],[0,3]].](../assets/ml/channels-add-before-relu--exercise.png)
torch.grad_enabled(false);
let c0 = torch.sequential().conv2d(1, 1, 2);
let c1 = torch.sequential().conv2d(1, 1, 2);
let k0 = torch.tensor("[1.0,1.0,0.0,1.0]");
let k1 = torch.tensor("[-1.0,0.0,-1.0,-1.0]");
for (i in range(0, 4)) {
c0.param("0.weight").set(i, k0.value(i));
c1.param("0.weight").set(i, k1.value(i));
}
c0.param("0.bias").set(0, 0.0);
c1.param("0.bias").set(0, 0.0);
let x = input.to(DType.F32);
let m0 = c0.forward(x.slice(0, 0, 1).reshape(1, 1, 3, 3)).reshape(2, 2);
let m1 = c1.forward(x.slice(0, 1, 2).reshape(1, 1, 3, 3)).reshape(2, 2);
let answer = m0.relu().add(m1.relu());
out.set("m0", m0).set("m1", m1).set("output", answer);
{
"m0": [
[
5,
2
],
[
2,
6
]
],
"m1": [
[
-4,
-5
],
[
-3,
-3
]
],
"output": [
[
5,
2
],
[
2,
6
]
]
}Combine the two signed channel maps so cross-channel cancellation happens before rectification.
Read the result carefully
Notice why relu(a + b) and relu(a) + relu(b) behave differently whenever a and b carry opposite signs. At window (0, 1), Channel 0 votes +2 while Channel 1 votes -5; their signed sum -3 says the combined pattern is absent, so ReLU outputs 0. Cross-channel inhibition lets a single filter require one feature to appear while simultaneously penalizing a competing feature at the exact same location.
Recap
- One filter on
C_ininput channels uses a(C_in, K, K)weight block and one scalar bias to produce a single output map. - Signed dot products from all
C_inslices sum together at each(r, c)before the nonlinearity runs. - Rectifying channels before summing destroys negative cross-channel cancellation because
relu(a + b)does not equalrelu(a) + relu(b).