Memorizing first
Grokking is a network generalizing long after it has fitted its training data. Power et al. found it on small algorithmic tasks: a transformer trained on part of a table of reaches 100 % training accuracy early, stays at chance on the held-out pairs for a long time, and then, with no change to the training, gets the held-out pairs right as well.
The page trains Nanda et al.'s setup here, on the Mac's CPU. The task is for every pair : 12,769 pairs, of which a fixed random 30 % (3,830) are for training. The model reads the three tokens a b = and predicts the answer at =. It has one attention layer with 4 heads, one MLP of 512 ReLU neurons, no LayerNorm, and 226,688 parameters, trained full batch with AdamW at learning rate and weight decay 1.
The Memorize, then generalize panel plots accuracy on a log step axis. Training accuracy is 100 % by step 200. Test accuracy creeps up from the 0.9 % of guessing to 16 % by step 3,000 while the test loss first climbs above 20 nats, then rises past 50 % at step 4,200 and 99.9 % at step 5,200: twenty times longer than memorizing took. The table under it gives the same steps for all three runs.
A circuit made of a few frequencies
Nanda et al. reverse-engineered what the generalizing model computes. Write for a few key frequencies . The embedding maps each number to and ; attention and the MLP multiply the two inputs' waves, which by the angle-addition identities gives
and the unembedding turns them into a logit for each answer that is a sum of over the key frequencies. Every term peaks at and nowhere else at once, so the sum picks the right answer for every pair, seen or not.
The The embedding in the Fourier basis panel tests the first step directly. It projects the 113 number embeddings onto the Fourier basis of the number line (a constant, then and of for ) and draws one bar per frequency. Frequencies 7, 37, 42 and 52 hold 7 % of the squared norm at step 0 and 93 % at the end, and the spectrum's Gini coefficient (0 when every frequency holds the same, near 1 when one holds everything) goes from 0.03 to 0.76. The Every checkpoint map shows all 251 checkpoints, and the four rows lighting up.
The key frequencies are read off the neurons, as the paper's code does: for each of the 512 neurons, the share of its variance over the 12,769 inputs that one frequency's 2D Fourier terms explain. At the last step 427 neurons have at least 85 % of their variance in a single frequency, always one of 7, 37, 42 and 52 (77, 91, 109 and 150 neurons), and the four hold 97 % of all the neurons' variance, against 8 % at step 0 (the orange line under the map).
Progress measures
Test accuracy jumps, but the circuit forms gradually. Nanda et al. built two measures that follow it on the training run itself, and the Progress measures chart in the strip plots them at every checkpoint:
- Restricted loss: keep, for every answer , only the parts of the logits over the 12,769 pairs that lie along and at the key frequencies (plus each answer's mean logit), and compute the loss on all pairs. It measures how well the circuit alone would do.
- Excluded loss: remove those same parts and compute the loss on the training pairs only. It measures how much the model's good training loss already depends on the circuit.
While the model memorizes, removing the key frequencies costs little: at step 1,000 the excluded loss is 0.0007 nats. From step 2,000 it climbs, to 0.36 at step 3,000, 5.2 at step 4,000 and 11.5 at step 5,000, while the training loss stays below : the training pairs are answered more and more by the circuit. The restricted loss falls across the same steps, from 4.6 nats at step 200 to 0.43 at step 3,000, when test accuracy is still 16 %, then to at step 5,000 and at the end. Both move well before test accuracy does. Nanda et al. split training into three phases this way: memorization, circuit formation (the circuit grows while the memorizing weights stay) and cleanup (weight decay removes the memorizing weights, and the test loss drops).
What weight decay does
Why switch at all, once the training loss is already near zero? AdamW's weight decay shrinks every weight a little each step, so the run keeps looking for a solution with smaller weights. Memorizing 3,830 arbitrary answers takes a lot of weight; the Fourier circuit, which covers all pairs with four frequencies, takes far less. The Σ w² chart shows the sum of squared weights: memorizing first drives it from 1,783 at initialization to 3,630 at step 200, then it falls while the circuit forms and is cleaned up, to 910 at the end.
The two other runs start from the same initialization and the same shuffle of the pairs. Without weight decay the model memorizes by step 200 as before, but its weights grow to Σ w² = 5,451, its test accuracy sits at 7.6 % for all 25,000 steps and its spectrum stays spread out (Gini 0.07). With weight decay but only 15 % of the pairs (1,915, a subset of the main run's), weight decay does shrink it (Σ w² from 3,116 to 1,750), but the key frequencies' share of its neurons falls instead of rising, and it ends at 0.6 % on the held-out pairs, below guessing. Power et al. found that the steps needed to generalize grow quickly as the training fraction shrinks; these runs only show that it did not happen within 25,000 steps.
Liu et al. and Varma et al. describe the same race: a memorizing and a generalizing solution compete, the generalizing one needs less weight norm for the same confidence, and weight decay favours it, unless there is so little data that memorizing becomes the cheaper one.
Every input at once
With only 12,769 inputs, the Every input (a, b) panel can draw a function of the input in full, as a 113 × 113 grid ( down, across) at each snapshot. p(right) is the probability of the correct answer, blue on training pairs and orange on the rest: at step 200 the training pairs average 98 % and the held-out ones 5 %, at step 4,200 the held-out ones 51 %. Attention to a is the weight = puts on the first number, per head; neuron shows the strongest neuron of each key frequency. In the finished model both are periodic in and in (neuron 377, of frequency 7, is a grid of bumps, seven along each axis); at step 200 the same neurons show only irregular stripes.
The circuit is one of several that solve the task: Zhong et al. found that small changes to the architecture lead to a different algorithm, so a circuit should be read off the trained model, not assumed. The Induction heads and Attribution graphs tabs read circuits off GPT-2 and Gemma 2 2B the same way.
Papers
- Grokking: Generalization Beyond Overfitting on Small Algorithmic Datasets Names grokking: transformers on modular arithmetic tables generalize long after memorizing, sooner with weight decay and with more data.
- Progress measures for grokking via mechanistic interpretability The setup this tab trains, the Fourier circuit it reads off the weights, and the restricted and excluded losses that show it forming before test accuracy moves.
- Towards Understanding Grokking: An Effective Theory of Representation Learning Grokking as the slow formation of a structured representation, with phases set by weight decay and learning rate.
- Omnigrok: Grokking Beyond Algorithmic Data Ties grokking to the weight norm: generalizing solutions sit at smaller norms, and a large initialization makes grokking appear on ordinary datasets.
- Explaining grokking through circuit efficiency Memorizing and generalizing circuits compete on efficiency; memorizing gets more expensive with more data, so below a critical dataset size it wins.
- The Clock and the Pizza: Two Stories in Mechanistic Explanation of Neural Networks Networks trained on modular addition find more than one algorithm; which one depends on details such as how much attention is used.
- Decoupled Weight Decay Regularization AdamW, the optimizer of all three runs: weight decay applied to the weights directly rather than through the gradient.