Chapter 8 of 0–21
Grokking
Memorise first, generalise thousands of steps later.
The phenomenon
In 2022, Power and colleagues trained small transformers on tables of modular arithmetic and saw something odd [2]. The networks first fitted their training data perfectly while doing no better than chance on held-out data — and then, long afterwards, test performance jumped, in their words
“improving generalization performance from random chance level to perfect generalization”— Power et al. 2022, abstract, p. 1 · source
and
“this improvement in generalization can happen well past the point of overfitting”— Power et al. 2022, abstract, p. 1 · source
They called this grokking. Nanda et al. describe it as follows [1]:
“models abruptly transition to a generalizing solution after a large number of training steps, despite initially overfitting”— Nanda et al. 2023, Sec. 1, p. 1 · source
Here is the paper's own training run. Both curves are plotted on a logarithmic scale: each horizontal grid line is ten times the one below it, so we can see losses from ~10 down to ~10⁻⁷ at once.
Loss during training (log scale)
Training loss (blue) and test loss (red) at every 10th step; shaded bands are the three phases.
Loading…
How to read it · the math
Each curve is the average cross-entropy loss over its set of inputs. The vertical axis is a logarithmic scale, so each grid line is a factor of 10. Click anywhere to move the whole page to that step.
What to expect: Train loss falls during memorisation (until step 1,567 on the paper's run) while test loss rises, then, thousands of steps later, test loss collapses (grokking).
Reading the plot (paper's run; numbers from its logs):
- Steps 0 – 201: both losses start near ln 113 ≈ 4.73, the loss of uniform guessing; the paper's logs record 4.76 (train) and 4.77 (test). Training accuracy reaches 100 % by step 201.
- 201 – 1,567: training loss keeps falling by orders of magnitude while test loss rises to about 26 — worse than guessing: the network confidently gives memorised-looking answers to questions it has never seen.
- 1,567 – 9,301: a long plateau. Training loss is tiny and flat; test loss decreases only slowly.
- Around 9,301: test loss collapses by seven orders of magnitude; test accuracy reaches 99 % at step 10,601.
Accuracy during training
Fraction of training (blue) and test (red) pairs answered correctly, at every checkpoint.
Loading…
How to read it · the math
<p class="fig-expect"><strong>What to expect:</strong> On the paper's run training accuracy reaches 100 % by step <span class="num" data-num="paper.train_acc_full_step">201</span>; test accuracy stays low for thousands of steps but creeps up (<span class="num" data-num="paper.acc_test_501">5.6 %</span> at step 501, <span class="num" data-num="paper.acc_test_t1">7.6 %</span> at about step <span class="num" data-num="paper.t1">1,567</span>, <span class="num" data-num="paper.acc_test_7k">16.4 %</span> at step 7,001, <span class="num" data-num="paper.acc_test_t2">46.7 %</span> at step <span class="num" data-num="paper.t2">9,301</span>), then jumps to <span class="num" data-num="paper.acc_test_tgrok">99.5 %</span> by step <span class="num" data-num="paper.t_grok">10,601</span>.</p>
<p class="fig-meta">
<a href="../chapters/08-grokking.html">Learn step by step →</a>
· Source: <a class="cite" href="../references.html#nanda2023" title="Nanda et al. 2023">[1]</a> Fig. 2
· Computed by <code>checkpoint_metrics.py</code> (<a href="https://github.com/BurnyCoder/visualizing-grokking-modular-addition">code</a>)
</p>
The curves average over many questions. The next picture shows every question at once: each pixel is one of the 12,769 sums a + b (a = row, b = column), coloured by the probability the network gives to the correct answer (yellow = certain and right, dark purple = no chance). While it is on screen it plays the paper's run from the start to just after grokking; pause it or drag its slider to study one moment. Watch the training questions, a random speckle, turn yellow early while the test questions stay mostly dark through the plateau, until the whole square lights up after step 9,301.
Confidence on every input
Probability the model assigns to the correct answer, for all (a, b) at the selected step.
Loading…
How to read it
<p class="fig-expect"><strong>What to expect:</strong> During memorisation the training pairs are bright and the test pairs dark (a speckle pattern = the random split); after grokking everything is bright.</p>
<p class="fig-meta">
<a href="../chapters/18-watch-it-form.html">Learn step by step →</a>
· Computed by <code>geometry.frame</code> (<a href="https://github.com/BurnyCoder/visualizing-grokking-modular-addition">code</a>)
</p>
Why it looks sudden
On a linear axis the same data looks like an abrupt phase transition — nothing, then a cliff:
The same losses on a linear scale
Identical data with an ordinary (linear) vertical axis.
Loading…
How to read it
<div class="fig-read"><p>On a linear axis the long plateau and the sudden drop look much more dramatic; the log plot shows that train loss keeps improving by orders of magnitude after it looks flat here.</p>