Speed

What a Convnet Is and Is Not

Trace a complete 28-parameter convnet from pixels to two class logits and see why translation sharing does not grant rotation invariance.

The problem

Across the previous ten lessons we built each piece in turn: sliding dot products, shared filters, multi-filter feature maps, zero padding, stride, ReLU, multi-channel sums, and max-pooling. How do those pieces join to score classes, and what guarantees does the resulting network actually carry?

A convolutional neural network (convnet) chains local weight-shared feature stages (conv2d -> relu -> max_pool2d) into flatten and a dense linear readout head. Weight sharing builds in translation equivariance across rows and columns with very few parameters, yet it does not make the network automatically invariant to 90-degree rotations, dramatic scale jumps, or arbitrary pixel permutations.

Work through the numbers

Feed the five-by-five image [[4, 4, 4, 4, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 1, 1, 0, 0], [0, 2, 2, 0, 0]] into conv2d(1, 2, 2) -> relu -> max_pool2d(2) -> flatten -> linear(8, 2). Filter 0 [[1, 1], [-1, -1]] detects horizontal jumps and Filter 1 [[1, -1], [1, -1]] detects vertical jumps, both with zero bias (8 + 2 = 10 parameters).

After ReLU and two-by-two max-pooling, Map 0 pools to p0 = [8, 8, 0, 0] (sum 16) and Map 1 pools to p1 = [0, 4, 0, 3] (sum 7). Flattening concatenates them into [8, 8, 0, 0, 0, 4, 0, 3].

The linear(8, 2) head (16 + 2 = 18 parameters, 28 total) weights p0 by +1 and p1 by -0.5 with bias +0.5 for Class 0 (horizontal), and reverses those weights with bias -0.5 for Class 1 (vertical). That yields logit_0 = 0.5 + 16 - 3.5 = 13 and logit_1 = -0.5 - 8 + 7 = -1.5, [13, -1.5].

The live model

Toggle rotate from 0 to 1 to transpose the five-by-five input: rows become columns, which turns the top horizontal bar into a left vertical bar. (A transpose is a quarter turn followed by a mirror flip, not a pure rotation; it is used here because it is one line of index arithmetic.) The pooled channel summaries swap roles (p0 sum drops to 7, p1 sum jumps to 16), flipping the logits from [13, -1.5] to [-0.5, 12]: the exact same bar laid along the other axis switches predicted class.

Five-by-five horizontal-bar image fed through a 28-parameter convnet producing class logits [13, -1.5].
torch.grad_enabled(false);
let rotate = int(scene.parameter("rotate", 0.0, 0.0, 1.0, 1.0));
let img = torch.zeros(25);
for (i in range(0, 25)) {
  let idx = i;
  if (rotate == 1) { idx = (i % 5) * 5 + (i / 5); }
  img.set(i, input.value(idx));
}
let body = torch.sequential().conv2d(1, 2, 2).relu().max_pool2d(2).flatten();
let cw = torch.tensor("[1.0,1.0,-1.0,-1.0,1.0,-1.0,1.0,-1.0]");
for (i in range(0, 8)) { body.param("0.weight").set(i, cw.value(i)); }
body.param("0.bias").set(0, 0.0);
body.param("0.bias").set(1, 0.0);
let head = torch.sequential().linear(8, 2);
let lw = torch.tensor("[1,1,1,1,-0.5,-0.5,-0.5,-0.5]");
for (i in range(0, 8)) {
  head.param("0.weight").set(i, lw.value(i));
  head.param("0.weight").set(8 + i, lw.value(7 - i));
}
head.param("0.bias").set(0, 0.5);
head.param("0.bias").set(1, -0.5);
let feats = body.forward(img.reshape(1, 1, 5, 5));
let answer = head.forward(feats).reshape(2);
out.set("rotate", rotate).set("logits", answer);
{
  "rotate": 0,
  "logits": [
    13,
    -1.5
  ]
}

Your turn

The starter outputs [12, -1] instead of [13, -1.5]: in Map 0's bottom-left quadrant, four negative scores peak at -1 rather than 0, dragging p0 from [8, 8, 0, 0] to [8, 8, -1, 0], subtracting 1 from Class 0's logit, and adding 0.5 to Class 1's logit. Clamp negative filter mismatches to zero before pooling so absent quadrants contribute 0. Check tests unseen five-by-five images and rejects missing head biases, swapped class logits, and hardcoded vectors.

Five-by-five horizontal-bar image with unrectified starter convnet producing [12, -1] instead of [13, -1.5].
torch.grad_enabled(false);
let body = torch.sequential().conv2d(1, 2, 2).max_pool2d(2).flatten();
let cw = torch.tensor("[1.0,1.0,-1.0,-1.0,1.0,-1.0,1.0,-1.0]");
for (i in range(0, 8)) { body.param("0.weight").set(i, cw.value(i)); }
body.param("0.bias").set(0, 0.0);
body.param("0.bias").set(1, 0.0);
let head = torch.sequential().linear(8, 2);
let lw = torch.tensor("[1,1,1,1,-0.5,-0.5,-0.5,-0.5]");
for (i in range(0, 8)) {
  head.param("0.weight").set(i, lw.value(i));
  head.param("0.weight").set(8 + i, lw.value(7 - i));
}
head.param("0.bias").set(0, 0.5);
head.param("0.bias").set(1, -0.5);
let feats = body.forward(input.to(DType.F32).reshape(1, 1, 5, 5));
let answer = head.forward(feats).reshape(2);
out.set("logits", answer);
{
  "logits": [
    12,
    -1
  ]
}

Complete the feature-extraction pipeline so negative filter mismatches are clamped to zero before two-by-two max-pooling and linear readout.

Read the result carefully

Count what the architecture gives for free versus what data must still teach. Sharing two two-by-two kernels across sixteen windows costs 10 parameters where a dense layer from 25 pixels to 32 hidden units would cost 832. Yet here we hand-set all 28 numbers; in practice gradient descent learns both filter banks and linear weights from labeled examples, and only data augmentation or specialized layers teach rotation tolerance.

Even the shift tolerance has a limit here: the flattened readout gives each pooled position its own weight, so the head must also learn to treat positions alike.

Deeper convnets add a few named variations beyond this single block: average pooling summarizes each tile by its mean, 1x1 convolution mixes channels at each pixel without spatial sliding, and stacking layers grows the receptive field with depth (two stacked three-by-three layers see a five-by-five patch), with every C_out-filter layer carrying its own C_out biases.

Recap

  • A tiny convnet chains conv2d -> relu -> max_pool2d feature extraction with flatten -> linear to map a pixel grid to class logits.
  • Parameter count stays tiny (10 + 0 + 0 + 0 + 18 = 28) because filters reuse weights across spatial starts and pooling adds zero parameters.
  • Convnets share weights across translations, not rotations: transposing a horizontal bar into a vertical one flips its class logits from [13, -1.5] to [-0.5, 12].