Layers and backprop

One neuron draws one straight line. Some problems — like XOR, "spam if (links AND not urgent) OR (urgent AND not links)" — have no single line that separates them. Stack neurons into layers and the network can bend space itself.

Learn

Why depth solves XOR

A hidden layer is a row of neurons that each draw their own line, and feed their outputs into one final neuron. The final neuron now draws its line in a new space built from those hidden lines — which lets the whole network carve out a bent, non-linear region.

h₁, h₂ = sigmoid(layer 1)   →   output = sigmoid(w₁·h₁ + w₂·h₂ + b)

Backpropagation is just the chain rule: it pushes the error at the output backward through the layers so every weight, in every layer, knows which way to move.

Build

Train a 2-neuron hidden layer on XOR

Mission A · solve XOR

Four points, laid out so no single line separates red from blue. Press train and watch backprop bend the boundary until all 4 are correct — something one neuron can never do.

epoch loss accuracy
A single line can't solve this — try it, then train
Break

Plant a backdoor

Depth just solved XOR for you — a single neuron never could. That same extra capacity has room to hide something you didn't ask it to learn.

Mission B · trigger-based backdoor

You control the training data again. Add a handful of "real mail" examples that all share one odd feature combo — a trigger pattern no real email has. Train, then stamp that same trigger onto the obvious spam below and watch it slip through, while every other email is still classified correctly.

trigger added noclean accuracy —stamped email not tested
Clean model: trigger not yet added

What just happened: the network kept its clean accuracy and still smuggled in one extra rule, waiting on a pattern nobody else would ever type.

Defend

Why this works, and the fix

Why it works: a backdoor doesn't lower overall accuracy, which is exactly why it's dangerous — the model looks healthy on every normal test. It just learns one extra rule: "if trigger present, always say real mail," and buries that rule inside millions of ordinary-looking weights (Gu et al. 2017, BadNets). The same idea backdoors image classifiers (a sticker on a stop sign) and, in LLMs, poisoned fine-tuning data (a rare phrase that flips behavior).

The fix: inspect training data provenance, use trigger-detection tools (activation clustering, spectral signatures) before deployment, and never fine-tune a production model on data you can't audit.