Shared Filter, Same Weights
Apply five shared parameters across all nine windows so identical local patches produce identical responses regardless of location.
The problem
A dense layer connecting sixteen input pixels to nine output cells requires 16 * 9 + 9 = 153 separate parameters, and even an untied local layer with nine independent two-by-two detectors requires 9 * (4 + 1) = 45 parameters. Worse, a detector learned in the top-left corner of an untied layer knows nothing about the exact same visual corner appearing in the bottom-right corner.
Parameter sharing ties all nine windows to one filter and one scalar bias: 4 + 1 = 5 parameters in total. Whenever two two-by-two patches contain identical pixel values, multiplying by the same four weights and adding the same scalar bias guarantees identical output scores. Shifting a local motif across the input shifts its score to the matching cell of the feature map, a property called translation equivariance.
Work through the numbers
Inspect the four-by-four image [[3, 1, 0, 1], [1, 0, 0, 2], [2, 0, 3, 1], [0, 1, 1, 0]]. Notice that the top-left patch at (0, 0) is [[3, 1], [1, 0]], and the bottom-right patch at (2, 2) is also [[3, 1], [1, 0]]. Apply the shared two-by-two kernel [[2, -1], [1, 0]] together with shared bias 0.5.
At (0, 0), the weighted sum is 3 * 2 + 1 * -1 + 1 * 1 + 0 * 0 = 6, and adding bias 0.5 gives 6.5. At (2, 2), the exact same arithmetic produces 6.5. Across the whole three-by-three grid, the nine outputs are [[6.5, 2.5, -0.5], [4.5, 0.5, 1.5], [4.5, -1.5, 6.5]], with 6.5 marking both occurrences of the motif.
Varying the top-right weight w01 changes the detector's rule everywhere at once. At w01 = -2, both (0, 0) and (2, 2) evaluate to 5.5; at w01 = 2, both evaluate to 9.5. Neither position can drift away from the other so long as weights and bias remain tied across the entire layer.
The live model
The view frames the two matching input patches at (0, 0) and (2, 2), displays the five shared parameters (4 weights plus 1 bias), and colors the three-by-three feature map. Drag w01 between -2 and 2 and watch cells (0, 0) and (2, 2) move in lockstep from 5.5 to 9.5 while model.num_parameters() stays fixed at 5.
![A four-by-four input with matching [[3,1],[1,0]] patches at corners, shared kernel [[2,-1],[1,0]] plus bias 0.5, and matching 6.5 scores at (0,0) and (2,2).](../assets/ml/shared-filter-same-weights--demo.png)
torch.grad_enabled(false);
let w01 = scene.parameter("w01", -1.0, -2.0, 2.0, 0.5);
let model = torch.sequential().conv2d(1, 1, 2);
let weight = model.param("0.weight");
weight.set(0, 2.0).set(1, w01).set(2, 1.0).set(3, 0.0);
model.param("0.bias").set(0, 0.5);
let params = model.num_parameters();
let image = input.to(DType.F32).reshape(1, 1, 4, 4);
let answer = model.forward(image).reshape(3, 3);
out.set("parameters", params).set("feature_map", answer);
{
"parameters": 5,
"feature_map": [
[
6.5,
2.5,
-0.5
],
[
4.5,
0.5,
1.5
],
[
4.5,
-1.5,
6.5
]
]
}Your turn
The starter outputs [[6.5, 2, -1], [4, 0, 1], [4, -2, 6]] instead of [[6.5, 2.5, -0.5], [4.5, 0.5, 1.5], [4.5, -1.5, 6.5]]: shifting (0, 0) alone breaks translation equivariance, leaving the identical patch at (2, 2) at 6 instead of 6.5 and leaving all eight other windows 0.5 too low. Apply the 0.5 bias uniformly across every window so identical patches receive identical scores. Check tests new four-by-four inputs and rejects missing bias, transposed weights, and hardcoded tables.

torch.grad_enabled(false);
let w01 = scene.parameter("w01", -1.0, -2.0, 2.0, 0.5);
let model = torch.sequential().conv2d(1, 1, 2);
let weight = model.param("0.weight");
weight.set(0, 2.0).set(1, w01).set(2, 1.0).set(3, 0.0);
model.param("0.bias").set(0, 0.0);
let image = input.to(DType.F32).reshape(1, 1, 4, 4);
let answer = model.forward(image).reshape(3, 3);
answer.set(0, answer.value(0) + 0.5);
out.set("parameters", model.num_parameters()).set("feature_map", answer);
{
"parameters": 5,
"feature_map": [
[
6.5,
2,
-1
],
[
4,
0,
1
],
[
4,
-2,
6
]
]
}Apply bias 0.5 uniformly across all nine windows rather than shifting cell (0, 0) alone so identical input patches receive identical scores.
Read the result carefully
Sharing weights across positions sharply reduces parameter count and bakes shift equivariance into the layer before any data is seen. That inductive bias helps when local patterns carry the same meaning everywhere, as edges and textures in photographs usually do. In aligned tables or centered portraits where row zero has a permanently different meaning from row three, strict spatial sharing can be a limitation rather than a free lunch.
Recap
- One two-by-two single-channel filter reuses
5parameters (4weights and1bias) across every window position. - Identical input patches always receive identical scores when both weights and bias are shared.
- Position-specific offsets break translation equivariance even when the four kernel weights match.