Grokking: generalizing long after memorizing

The network gets every training example right very quickly, but keeps failing on problems it has not seen, and only starts getting them right much later. Turn clock arithmetic on a circle one step at a time and see the solution the model found.

Short answer. Grokking is when, on small algorithmic datasets, a neural network's test accuracy goes from chance level to perfect generalization long after it has overfit the training data. When researchers took apart a small transformer trained on modular addition, it was placing numbers as angles on a circle and doing addition as a rotation around the circle. On the outside the jump looks sudden, but inside that solution grows slowly and shows up once the memorized parts are removed.

Definitions

The text below uses a small transformer that learns modular addition. These are the definitions. Grokking and the three phases are names given by the papers in the sources, so they are written only as far as those papers go.

overfitting, generalization
Overfitting is the state where a model gets the training data right but gets unseen data, which was not used for training, wrong. Generalization is getting unseen data right too. It is measured by the accuracy on held-out test data.
grokking
The effect where, long after a neural network has overfit the training data, test accuracy goes from chance level to perfect generalization. It is the name Power et al. gave to what they saw on small algorithmic datasets.
modular addition
For integers a and b from 0 to p − 1, the operation whose answer is the remainder of a + b divided by p. The answer is also an integer from 0 to p − 1.

(a+b)modp

mechanistic interpretability
Taking apart the trained weights to show, in a form people can read, the calculation the model actually does.
memorization, circuit formation, cleanup
The three phases Nanda et al. use to split grokking on modular addition. In memorization the network memorizes the training data and training accuracy reaches 100% first. In circuit formation a solution that generalizes slowly grows inside the weights. In cleanup the memorized parts are removed. A circuit is a part of the network where many weights work together to do one calculation.

1. What grokking is: training fast, test much later

The usual overfitting story goes like this. Training accuracy goes up, test accuracy does not follow, so the model is memorizing the training data, and you stop or add regularization.

Power et al. (2022) saw something different on small algorithmic datasets. Long after the network had overfit the training data, test accuracy went from chance level to perfect generalization (getting unseen data right too). The paper's title calls it generalization beyond overfitting, and the name is grokking.

100%0% training accuracy test accuracy stuck at chance early stopping may stop here memorizationcircuit formationcleanup training steps → schematic (not real measurements)
Schematic, not real measurements. Green is training accuracy, blue is test accuracy. Across is training steps, and the tick numbers are left out on purpose. The three bands below are the three phases in section 5, and the orange dashed line belongs to the early stopping story in section 6.

The paper has two more findings. The less training data there was, the more optimization it took to generalize. And not every task grokked. Some operations never generalized.

2. The task: modular addition

Power et al. used modular arithmetic with p = 97 (addition, subtraction, division and more) and permutation operations. The model was a decoder-only, 2-layer transformer (width 128, 4 heads) trained with AdamW. This page looks only at addition.

(a + b) mod p  =  the remainder of a + b divided by p

A clock does exactly this. On a 12-hour clock, 5 hours after 9 o'clock is 2 o'clock, not 14 o'clock. Whatever goes past 12 drops out as one full turn (a clock writes 12 instead of 0, but here we write 0).

  1. (9 + 5) mod 12 = 14 − 12 = 2. It passes 12, so one full turn comes off.
  2. (3 + 4) mod 12 = 7. If it does not pass 12, it is plain addition.
  3. (5 + 4) mod 7 = 9 − 7 = 2.
  4. (96 + 96) mod 97 = 192 − 97 = 95.

With p choices for a and p for b, there are only p × p problems (9409 when p = 97). You show part of that table during training and measure generalization by whether the model gets the other cells right. Memorizing each cell gives no way to answer a cell you never saw. To get those right, the model has to find the rule.

3. The solution the model found: addition as rotation on a circle

Nanda et al. (2023) reverse-engineered a small transformer trained on modular addition. This kind of work is called mechanistic interpretability: taking the learned weights apart until a person can read the computation the model actually does. The model was using the discrete Fourier transform (DFT) and trig identities to turn addition into rotation around a circle. As formulas:

θ(x) = 2π · x ÷ p                                  the angle of spot x (360° × x ÷ p)
θ(a) + θ(b) = θ(a + b)                             two turns end to end add their angles
cos(θa + θb) = cos θa · cos θb − sin θa · sin θb    the angle addition formulas
sin(θa + θb) = sin θa · cos θb + cos θa · sin θb
θ and θ + 2π are the same spot                      a full turn (p spots) drops out → (a + b) mod p

The green arrow is turning by a, the blue arrow is turning b more, and the orange dot is where we land. Arrows from earlier steps stay, faded. 0 is on the right, and unlike a clock we turn counterclockwise (the direction math uses for angles). This is the formulas above computed exactly, not values from a trained model.

Try this. 1) With the starting values (p = 12, a = 9, b = 5), press "Next" four times: 270° + 150° = 420° is one full turn plus 60°, so spot 2. In the last step, cos = 0 × (−0.87) − (−1) × 0.5 ≈ 0.5 and sin = (−1) × (−0.87) + 0 × 0.5 ≈ 0.87, which is also 60°, spot 2. 2) With p = 97, a = 96, b = 96: 356.3° + 356.3° = 712.6° is one full turn plus 352.6°, so spot 95. 3) With p = 7, a = 5, b = 4: 462.9° is one full turn plus 102.9°, so spot 2.

Why cos and sin. The angle addition formulas only multiply and add. You do not need to hold the angle θ itself: with just the two values cos θ and sin θ you can work out a turn. And using k·θ instead of θ gives the same arithmetic. When p is prime, like 97, any k from 1 to p − 1 puts all p spots at different angles. This k is called the frequency. It says how many times you wind around the circle, and it shows up in the next experiment.

4. Comparing two neurons: circle, ellipse, Lissajous figure

Say a neuron's value goes up and down like a cos wave as the input x changes. Put two neurons' values across and up and draw one dot for each x: that is a scatter plot. Its shape is decided by how the two waves relate.

neuron 1 = cos(2π · k₁ · x ÷ p)
neuron 2 = sin(2π · k₂ · x ÷ p + φ)
Two neuronsScatter plotWhy
k₁ = k₂, φ = 0° or 180°circlecos and sin of the same angle, and cos² + sin² = 1
k₁ = k₂, φ = 90°straight line (diagonal)sin(θ + 90°) = cos θ, so the two values are always equal
k₁ = k₂, any other φellipsethey look at the same angle, but out of step
k₁ ≠ k₂Lissajous figurethe two waves turn at different speeds. With high, very different frequencies it looks like a mess

This picture is drawn from the formulas above, not measured in a real model. The left side shows the two neurons' values for each x, the right side puts them across and up as a scatter plot. The gray dashed line is a circle of radius 1.

Try this. 1) The start (k₁ = k₂ = 1, φ = 0°) is a circle. 2) Set φ to 45° for an ellipse squashed along the diagonal, 90° for one diagonal line, 180° for a circle again. 3) Set k₂ = 2 and φ = 90° to get a parabola, because neuron 2 = cos 2θ = 2 cos²θ − 1 = 2 × (neuron 1)² − 1. 4) With k₁ = 3 and k₂ = 7 it is a mess.

Read it the other way round. If you compare a real model's neurons two at a time and a circle shows up, that is a hint the two neurons move like a cos and sin pair of the same frequency. A mess means two different frequencies, or no such structure at all. This way of reading comes from the formulas above. This page did not measure a real model.

5. Three phases: memorization → circuit formation → cleanup

Nanda et al. split training into three phases. They are the bands under the picture in section 1.

  1. Memorization. The model memorizes the training cells, and training accuracy reaches 100% first. It cannot answer cells it has not seen.
  2. Circuit formation. The rotation solution from section 3 grows slowly inside the weights. A circuit is a group of weights that together do one piece of computation. The memorized parts are still there, so the test accuracy on the outside can stay low.
  3. Cleanup. The memorized parts are removed.

The paper's main point is that grokking is not a sudden switch. It is the result of a structured solution growing slowly inside the weights, followed by the removal of the memorized parts. The test accuracy curve alone looks like it jumps at one moment (the picture in section 1), but inside, the solution was growing all through the second phase.

6. Weight decay and early stopping

Weight decay is a regularizer that shrinks the weights a little toward 0 at every step. Power et al. compared several methods and report that weight decay had the biggest effect on data efficiency (reaching generalization with less training data).

Here is this page's guess at why it helps (a guess: the sources do not say this). Weight decay keeps shrinking any weight the loss does not hold on to. If there are two ways to get every training cell right, memorizing each cell and rotating on the circle, the one that works with smaller weights should last longer. The guess fits with the memorized parts being removed in the cleanup phase.

Early stopping stops training when the validation score has not improved for a set patience. In grokking, after training accuracy hits 100%, test accuracy stays at chance level for a long time. If the patience runs out during that stretch, training stops before the climb. So if you stop early, you never see grokking. This was not measured. It is worked out by putting the two rules side by side (the orange dashed line in section 1).

The other way round, training longer does not guarantee the climb either. Some of the operations in Power et al. never generalized. The loop that runs training one step and one epoch at a time is on the training loop page.

7. When to use what

OptionGoodBadWhen to use
Early stoppingSaves compute and keeps the weights from before overfittingOn data where the test score stays flat for long and climbs late, it can stop before the climb (section 6, worked out, not measured)Ordinary training, when you do not expect late generalization
Keep training long after 100% training accuracyYou can see late generalizationCosts a lot of compute, more with less data. It may never climbStudying small tasks with a rule, like modular arithmetic
Add weight decayHad the biggest effect on data efficiency in the experiments of Power et al.You have to pick its strength as a hyperparameterWhen you want to generalize from less data
Use more training dataLess data needed more optimization to generalize, so more data needs lessYou have to gather more dataWhen you can make more data (easy for modular arithmetic, a p × p table)

Where it is used

Frequently asked

Is grokking the opposite of overfitting?

No. Grokking happens after overfitting. Long after the network has overfit the training data, test accuracy goes from chance level to perfect generalization. It reaches generalization by passing through overfitting.

Does the network really get it all at once?

If you only look at the test accuracy curve, it seems to jump. But Nanda et al. (2023) explain it as a structured solution growing slowly inside the weights, followed by the removal of the memorized parts. Sudden on the outside, gradual on the inside.

Why a circle of all things?

Modular arithmetic wraps back to the start once you pass p. On a circle, a full turn brings you back to where you were, so the wrap comes for free. And the angle addition formulas only multiply and add, so you can turn using just the cos and sin values, without knowing the angle.

Words

Plain words used hereTextbook term
generalizing long after memorizinggrokking
getting only the training data right · getting unseen data right toooverfitting · generalization
clock arithmeticmodular addition, modular arithmetic
turning on a circlerotation
building the cos and sin of a sum of anglesangle addition formulas, trigonometric identities
splitting values into waves of each frequencydiscrete Fourier transform (DFT)
how many times a wave goes up and down · how far it is shiftedfrequency · phase
a picture of two values across and up · a tangled patternscatter plot · Lissajous figure
making a model's computation readablemechanistic interpretability
the three phasesmemorization · circuit formation · cleanup
shrinking big weights · stopping earlyweight decay · early stopping
training and test accuracytraining accuracy · test accuracy

Sources

← Early stopping and reading learning curvesData augmentation →