Mechanistic interpretability · a replication of Nanda et al. (2023)

Windmills in the mind - transformer internal learning

A small transformer is trained to add numbers on a 113-hour clock. It first overfits, memorising the training examples. With continued training it learns to generalise. Scrub through training to see how.

Loss

Train vs test on a log step axis. Click anywhere to jump there.

Total weight norm

L2 norm of all parameters. Weight decay penalises it; memorisation inflates it.

Ring

Each dot is a number, in the final model's cos/sin plane for frequency k. 0112

Fourier spectrum of the embedding

How much each frequency k is used. Key frequencies in blue; click one to view its ring.

PCA

Top two principal components of the embedding. Hover a dot to find it in both plots.

1 · The task

Addition on a clock

Each example is two numbers a, b from 0 to 112, fed in as three tokens [a, b, =]. The model must answer (a + b) mod 113: addition on a clock with 113 hours. It trains on a random 30% of the 12,769 possible pairs and is tested on the other 70%.

It's a one-layer transformer (4 attention heads, a 512-neuron MLP, about 227k parameters) trained with a strong weight decay. At step 0 its weights are random.

2 · Memorisation · steps 0 –

Overfitting

Within about 150 steps, training accuracy reaches 100%. Test loss rises above random guessing (ln 113 ≈ 4.7). The network has learned a lookup table for the training data and nothing else.

The weight norm grows from to : storing nearly 4,000 arbitrary answers requires large weights.

3 · Circuit formation · to step

Dead Period

For thousands of steps the headline curves barely move. The weight norm falls, and slow structure appears in the spectrum and the k = 1 ring.

The progress measures (now on) track this. Restricted loss keeps only the Fourier part of the model's output and falls steadily. Excluded loss removes that part and rises. A Fourier circuit is forming alongside the memorised solution.

4 · Grokking · step

Generalisation

Around step the Fourier circuit is strong enough to produce the answer alone. Test accuracy rises from near chance to over 95% within a few hundred steps; test loss eventually falls by over seven orders of magnitude.

The circuit has been forming since the previous phase. This is the point at which it outweighs the lookup table.

5 · Cleanup · to step

Cleanup

With the general algorithm in place, the memorisation weights only add to the weight-decay penalty, and are removed. The norm drops to about , the spectrum's noise floor collapses, and the rings sharpen.

6 · The algorithm

Addition as rotation

Each key frequency k places number a on a circle at angle w·a, where w = 2πk/113. With consecutive numbers connected, each +1 moves k positions around the ring.

Adding a and b composes two rotations: the attention and MLP layers compute cos(w(a+b)) and sin(w(a+b)) via trig identities. The output layer scores each candidate c with cos(w(a+b−c)), which peaks at c ≡ a+b. Each frequency alone gives an approximate answer; the four peaks coincide only at the correct one.

7 · Why weight decay matters

Role of weight decay

Memorisation and the Fourier algorithm both fit the training data, so training loss alone gives no reason to switch. Weight decay penalises weight magnitude. A few waves shared across all inputs cost far less than nearly 4,000 stored answers, so optimisation moves toward the general solution. Without weight decay, grokking is much slower or does not occur.

8 · Your turn

Explore

Drag the timeline or press play (Space; ← → step one frame). Click a blue spectrum bar to switch rings, hover a dot to locate that number in both scatter plots, or set PCA to per-frame to see the dominant directions at each checkpoint.

Reading the panels

Loss
Cross-entropy on the training set (30% of pairs) and the held-out test set (70%), on a log step axis so the early memorisation and late grokking both fit. Shaded bands mark the three phases. The progress measures follow Nanda et al.: they split the model's output into its key-frequency Fourier part and everything else.
Weight norm
√(sum of every parameter squared). AdamW with weight decay 1.0 shrinks every weight each step, so anything the network stores has an ongoing cost.
Fourier spectrum
The embedding gives each number a a 128-dimensional vector. Read each dimension as a function of a, rewrite it as a sum of waves cos(2πka/113) and sin(2πka/113), and measure how much of each frequency k is used. A random embedding uses every frequency a little; the trained one uses just .
Ring
Every number's embedding, projected onto the plane spanned by the final model's cos and sin directions for frequency k. The axes are fixed across frames, so changes reflect the embedding, not the projection. The dashed circle (radius 1) marks a fully formed ring.
PCA
The two directions along which the final embedding varies most. These correspond to the k = 1 ring. "Per frame" recomputes PCA for each checkpoint, aligned to its neighbour so it doesn't flip; early on, its top directions are noise.

The setup

References