Foundations to Frontier
Tests
B. Statistical Learning Theory › B4 Theory of deep learning

Grokking

On small algorithmic tasks like modular addition, networks can reach 100% training accuracy early while test accuracy stays at chance, and then, thousands of steps later, suddenly generalize (Power et al., 2022). This topic explains grokking as a slow transition from a memorizing solution to a structured, generalizing one (for modular addition, a Fourier 'clock' algorithm), driven by weight decay favouring the lower-norm generalizing circuit. It discusses how training fraction and regularization control the delay, and how progress measures detect the transition early. In the exercises you reproduce grokking in numpy, show it vanishes without weight decay, and watch hidden units lock onto single Fourier frequencies.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Memorize first, understand later

A student cramming for a maths test can memorize the answers to the practice problems without understanding them. They score 100% on those, but fail new ones. Keep them studying, gently discouraging memorization, and one day it clicks: they understand the rule and can solve anything. Neural networks can do the same thing, with the “click” arriving long after they have perfectly memorized the training set. This delayed understanding is called grokking.

Grokking: memorize first, understand much later
00.51252502500
press train (blue: train, orange: test; log-scale steps)
The task: learn a + b mod 13 from some of the 169 pairs, then answer the unseen ones. The network memorizes its training pairs within ~100 steps, yet test accuracy lingers near chance and only later climbs toward 100%, after it has discovered a Fourier-like algorithm. Set weight decay to 0 and it never generalizes; give it less data and the wait grows (or never ends).
Why engineers care

Grokking shows that training loss can look finished long before the model has learned what matters. Stopping early, or judging a model only by training metrics, can miss a generalizing solution, and some regularization choices decide whether it ever arrives. Progress measures that look inside the network help catch these hidden transitions.

📝 Quick check (Level 0)
0/3 answered
  1. 1. In grokking, the network first…
  2. 2. Generalization arrives…
  3. 3. Which ingredient was essential in our experiments?

Level 1: Beginner

★★☆☆☆ Basic⏱ 25 minSecondary-school mathematics: arithmetic and elementary algebra.

The setup

Modular addition: inputs (a, b) with a and b each between 0 and p − 1, output (a + b) mod p. There are p² examples; train on a fraction, test on the rest. Each input is one-hot encoded, and a small network outputs p logits.

The phenomenon

  1. Memorization (fast): training accuracy reaches 100% within ~100 steps. Test accuracy stays near 1/p.
  2. Plateau: training loss is near zero, yet test accuracy creeps up or sits flat.
  3. Generalization (slow): test accuracy rises to about 100%, often sharply.

Knobs

  • Training fraction: more data shortens the delay; too little data and grokking never happens within the budget.
  • Weight decay: essential here. It slowly pushes the solution toward smaller weights, where the generalizing circuit lives.
  • Learning rate and optimizer affect timing (AdamW is the standard).
📝 Quick check (Level 1)
0/3 answered
  1. 1. Chance-level test accuracy for p = 17 classes:
  2. 2. More training data generally makes grokking…
  3. 3. Number of examples for p = 17:

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.

Two solutions, two norms

The network can fit the training set in two ways:

  • Memorizing: a lookup table. It fits any labelling, needs large weights, and has no structure to transfer.
  • Generalizing: an algorithm. For modular addition this is a Fourier “clock”: embed a as (cos⁡ωa,sin⁡ωa)(\cos\omega a, \sin\omega a) for a few key frequencies ω=2πk/p\omega = 2\pi k/p, combine using trigonometric identities to get cos⁡ω(a+b)\cos\omega(a + b), and read out c with cos⁡ω(a+b−c)\cos\omega(a + b - c), which peaks at c = (a + b) mod p (Nanda et al., 2023).

The memorizing solution is found first because it is close to initialization and easy for gradient descent. Weight decay steadily shrinks all weights. The generalizing circuit can fit the data with a smaller norm, so over many steps the balance tips toward it, and test accuracy jumps once the memorizing components have been pruned away (“cleanup”).

A quadratic-activation model

With activation σ(z) = z², the network ((W1x)2)W2((W_1x)^2)W_2 computes products of embeddings. The product of two cosines contains cos⁡ω(a+b)\cos\omega(a + b), so the Fourier solution is exactly representable (Gromov, 2023). That’s the model in our exercises.

🐍 Exercise 1: reproduce grokking
Train a quadratic-activation network on (a + b) mod 17 from half the pairs with AdamW. Record train/test accuracy and parameter norm over 3000 steps, find when each reaches its threshold, and repeat without weight decay.
📝 Quick check (Level 2)
0/3 answered
  1. 1. The generalizing solution for modular addition uses…
  2. 2. Weight decay favours the generalizing solution because it…
  3. 3. cos ω(a + b − c) is maximal when…

Level 3: Advanced

★★★★☆ Advanced⏱ 40 minGraduate level, with full derivations and proofs.

Why trigonometry solves modular addition

With ω=2πk/p\omega = 2\pi k/p: cos⁡ω(a+b)=cos⁡ωacos⁡ωb−sin⁡ωasin⁡ωb\cos\omega(a + b) = \cos\omega a\cos\omega b - \sin\omega a\sin\omega b, which needs only products of embedding coordinates (exactly what σ(z) = z² provides, since (u+v)2(u + v)^2 contains 2uv). Then

cos⁡ω(a+b−c)=cos⁡ω(a+b)cos⁡ωc+sin⁡ω(a+b)sin⁡ωc,\cos\omega(a + b - c) = \cos\omega(a + b)\cos\omega c + \sin\omega(a + b)\sin\omega c,

a linear readout in c. Summing over a few frequencies sharpens the peak at c≡a+bc\equiv a + b. ∎

Theories of the delay

  • Norm/circuit competition: the generalizing circuit is more efficient (lower norm per unit of margin), and weight decay makes it win slowly (Nanda et al.; Varma et al., 2023).
  • Lazy → rich transition: early training is near the lazy/kernel regime and memorizes. Feature learning (B4.6) kicks in later (Kumar et al., 2024; Lyu et al., 2024).
  • Slingshot / optimizer effects: Adam’s normalization can cause cyclic instabilities that help the transition (Thilak et al., 2022).

Progress measures

Hidden structure appears before test accuracy moves. In our runs, the fraction of each hidden unit’s embedding power in one frequency rises steadily long before the test accuracy saturates. Such mechanistic measures can predict the jump.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Quadratic activations help because (u + v)² contains…
  2. 2. One view of grokking is a transition from…
  3. 3. Progress measures are useful because…

Level 4: Expert

★★★★★ Expert⏱ 35 minResearchers: original papers and open problems.

Grokking, measured

Exercise 1 (mod 17, 50% of the 289 pairs, H = 64, AdamW with lr 0.01 and weight decay 1). Training accuracy reaches 99% at step 100, but test accuracy reaches 95% only at step 1700, 17× later, and ends at 98.6% with ‖θ‖² = 980. Without weight decay, the network also memorizes by step 100 but its test accuracy ends at 4.8% (chance is 5.9%) with ‖θ‖² = 1677.

Exercise 2: what happens inside. The fraction of each hidden unit’s token-embedding power in its single strongest Fourier frequency is 0.50, 0.66, 0.78 and 0.88 at steps 100, 400, 1000 and 3000 (random weights give 0.34). Without weight decay it is 0.47 (after 1500 steps). The structure forms gradually during the plateau, while training accuracy is already perfect. The grokking step is 1700 with 50% training data and 500 with 70%, and with 30% the network does not grok within 3000 steps.

🐍 Exercise 2 (research): Fourier structure and data dependence
Track how concentrated each hidden unit's embedding is in one Fourier frequency during training (with and without weight decay), and measure the grokking step as a function of the training fraction.

Grokking in AI, robotics and aerospace

  • Don’t stop at zero training loss on structured tasks; monitor held-out metrics over long horizons.
  • Regularization matters for algorithmic generalization: weight decay can decide whether a learned planner or controller generalizes beyond its training cases.
  • Mechanistic interpretability: inspecting circuits (like the Fourier clock) is a route to trusting learned components.
  • Data efficiency: more diverse data shortens the time to generalization.

References

  1. A. Power, Y. Burda, H. Edwards, I. Babuschkin, V. Misra (2022). “Grokking: generalization beyond overfitting on small algorithmic datasets.” arXiv:2201.02177.
  2. N. Nanda, L. Chan, T. Lieberum, J. Smith, J. Steinhardt (2023). “Progress measures for grokking via mechanistic interpretability.” ICLR.
  3. A. Gromov (2023). “Grokking modular arithmetic.” arXiv:2301.02679.
  4. Z. Liu, O. Kitouni, N. Nolte, E. J. Michaud, M. Tegmark, M. Williams (2022). “Towards understanding grokking: an effective theory of representation learning.” NeurIPS.
  5. V. Varma, R. Shah, Z. Kenton, J. Kramár, R. Kumar (2023). “Explaining grokking through circuit efficiency.” arXiv:2309.02390.
  6. T. Kumar, B. Bordelon, S. J. Gershman, C. Pehlevan (2024). “Grokking as the transition from lazy to rich training dynamics.” ICLR.