In-Context Learning and Occam's Razor

Eric Elmoznino, Tom Marty, Tejas Kasetty, Leo Gagnon, Sarthak Mittal, Mahan Fathi, Dhanya Sridhar, Guillaume Lajoie

TL;DR

This paper provides a theoretical foundation for In-Context Learning (ICL) by proving that the next-token prediction objective is mathematically equivalent to prequential coding, a powerful data compression technique. This link reveals that ICL doesn't just minimize error; it explicitly follows Occam’s Razor by jointly minimizing training error and model complexity, explaining why Large Language Models (LLMs) can generalize so effectively from just a few examples.

What Problem Does This Paper Address?

Generalization—the ability of a model to perform well on unseen data—is the fundamental challenge of machine learning. While the "No Free Lunch Theorem" suggests generalization requires specific assumptions, practitioners have long relied on Occam’s Razor: the simplest explanation that fits the data is usually the best.

Standard machine learning typically minimizes training error and relies on indirect "hacks" (like $L_1$ regularization or dropout) to keep models simple. However, LLMs exhibit a surprising ability to learn new tasks from a few examples in a prompt (In-Context Learning) without any parameter updates. This paper asks: Why does ICL generalize so well? Is there a formal mathematical principle at work that enforces simplicity?

Methods

The authors bridge the gap between information theory and meta-learning through several key steps:

  1. Kolmogorov Complexity: They frame model simplicity using Kolmogorov complexity ($K(x)$), which defines the complexity of an object as the length of the shortest program that produces it.
  2. Prequential Coding: They introduce a compression technique where a learner $T$ processes a sequence of data points. For each new point, the learner uses everything it has seen so far to predict the next value. The total "cost" (bits) to describe the dataset this way is the prequential code length.
  3. The Equivalence Proof: The core contribution is showing that the standard next-token prediction loss (cumulative cross-entropy) used to train Transformers is exactly equivalent to minimizing this prequential code length.
Learning pipeline and prequential code length
  1. Experimental Control: To prove that next-token prediction is the "secret sauce," they compare "Prequential ICL" (standard) against "Train-risk ICL"—a version where the model is trained to predict past tokens already in the context. This isolates the effect of the "predict the future" objective.

Datasets

The authors used synthetically generated tasks to maintain strict control over ground-truth complexity:

  • Linear Regression: Noisy linear mappings.
  • Sinusoidal Regression: Linear combinations of three shared frequencies with varying amplitudes.
  • Mastermind: A multi-label classification task based on the code-breaking game, requiring logical inference.
  • Hidden Markov Models (HMMs): Used to test non-IID (non-stationary) data, mimicking some statistical properties of natural language.
  • GPT-4 Evaluation: They also tested a pretrained LLM on the Mastermind task to see how "scale" affects these principles.

Key Insights and Ideas

  • ICL is Compression: Because next-token prediction minimizes prequential code length, an ICL model is essentially a "meta-compressor." To compress data well, it must discover the underlying patterns (the simplest "program") rather than just memorizing noise.
  • Implicit Regularization: Unlike standard SGD, which needs explicit penalties to avoid overfitting, ICL has an "Occam’s Razor" built into its objective function.
  • The "Aha" Moment on Overfitting: The authors visualized the functions learned by ICL. While a model trained only on training risk (Train-risk ICL) produces "wiggly" curves that overfit every noisy point, the Prequential ICL model produces smooth, simple curves that stay close to the true ground truth.
  • Architecture Matters: The paper shows that the ability to minimize complexity is not universal. Different architectures (Transformers vs. SSMs like Mamba) have different "inductive biases," making some better at certain types of compression than others.

Key Results

  • Generalization Gap: Prequential ICL consistently outperformed Train-risk ICL and standard SGD in low-data regimes. As the number of examples in the prompt increases, the gap narrows because the "training error" term eventually dominates the "complexity" term.
  • Task Difficulty: The advantage of ICL’s simplicity preference becomes more pronounced as tasks get harder (e.g., moving from linear to sinusoidal regression).
  • The Mastermind Paradox: Interestingly, while a small Transformer trained on Mastermind tasks performed well, GPT-4 struggled, often performing worse than a naive baseline. This suggests that even massive scale doesn't guarantee a model can compress every novel task distribution effectively.
  • SGD vs. ICL: In high-data regimes, standard SGD-trained MLPs eventually overtook ICL models on complex tasks, suggesting that current ICL methods may suffer from "underfitting" or capacity limits when given a lot of data.

Limitations and Future Work

  • Underfitting: Current ICL models are great at being "simple," but they can be too simple, failing to capture complex patterns even when plenty of data is available in the prompt.
  • Fixed Compute: ICL is limited by the single forward pass of the model. The authors suggest future work could involve "layers" with built-in optimization primitives to allow for more flexible compute budgets.
  • Task Generalization: The failure of GPT-4 on Mastermind shows that "pretraining on everything" isn't a silver bullet for learning any arbitrary task in-context.
  • Curriculum Design: Since the order of data in the prompt affects the prequential code length, this theory could lead to better ways of ordering "few-shot" examples to help models learn faster.

Summary updated

BibTeX
@inproceedings{elmoznino2025context,
  author = {Eric Elmoznino and Tom Marty and Tejas Kasetty and Leo Gagnon and Sarthak Mittal and Mahan Fathi and Dhanya Sridhar and Guillaume Lajoie},
  title = {{In-Context Learning and Occam's Razor}},
  booktitle = {International Conference on Machine Learning},
  year = {2025},
  url = {https://openreview.net/forum?id=t0ZDijoZbD},
}
Figure