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 establishes a formal mathematical link between In-Context Learning (ICL) in sequence models and the foundational principle of Occam’s razor. The authors demonstrate that the standard next-token prediction loss is equivalent to a data compression technique called prequential coding. Consequently, training a model on next-token prediction inherently forces it to minimize both training error and model complexity, explaining why ICL generalizes so effectively from few examples.

What Problem Does This Paper Address?

A central goal of machine learning is generalization—the ability to perform well on unseen data. According to Occam's razor, the simplest explanation that fits the data is usually the best one. However, standard machine learning algorithms typically focus purely on minimizing training error, relying on ad-hoc regularization techniques (like $L_2$ weight decay) or implicit architectural biases to keep models simple.

Defying traditional training dynamics, large sequence models (like Transformers) exhibit In-Context Learning (ICL): they can rapidly learn new tasks from a few examples provided directly in their prompt, without any parameter updates. Why does ICL generalize so well in these low-data regimes? This paper addresses this question by providing a normative framework that connects ICL directly to optimal data compression and algorithmic simplicity.

Methods

The authors bridge the gap between ICL and Occam's razor using Kolmogorov complexity and the Minimum Description Length (MDL) principle. Their core theoretical contribution shows that the cumulative next-token prediction loss used to train sequence models is mathematically identical to prequential coding, an advanced data compression technique.

To validate this theory, they design a controlled meta-learning setup and compare three types of learners:

  1. Prequential ICL (Standard ICL): A Transformer trained on standard next-token prediction (predicting future, unseen tokens in the sequence).
  2. Train-Risk ICL (Control): An identical Transformer architecture trained to predict past tokens that have already appeared in the context, minimizing only the empirical training error.
  3. Gradient-Based Learner (SGD): A standard Multi-Layer Perceptron (MLP) trained from scratch on the context data using stochastic gradient descent.
Figure 1. In-context learning fits simple models to training data. a. Standard train-risk minimization fits predictors that perform well on the training data, while a meta-learner trained on next-token prediction (in-context learning) learns to infer predictors that generalize to unseen (next-token) data, mitigating overfitting. b. Prequential coding: a method for estimating K(D) using a model pθ’s learning algorithm T. As the learner T sees more data, it outputs models pθi = T(D1:i) that assign a higher likelihood to new observations di+1, and can thus better compress them. The prequential code length Lpreq(D; T) for describing the data in this way is given by the area under the curve, and the model’s complexity is related to the learner’s speed of adaptation. c. Illustration of the solutions fit by each learner. Train-risk minimization produces models that overfit the training data and generalize poorly, whereas in-context learning infers simpler solutions.

Datasets

The authors evaluate their framework using four synthetically generated task distributions to maintain strict control over data generation and task complexity:

  • Linear Regression: Noisy linear mappings where inputs and outputs are continuous vectors.
  • Sinusoidal Regression: Linear combinations of sine waves with shared frequencies but varying amplitudes across tasks.
  • Mastermind: A multi-label classification problem based on the classic code-breaking game, requiring discrete logical deduction.
  • Hidden Markov Models (HMMs): Synthetically generated non-stationary sequences designed to mimic the statistical properties of natural language in a simplified setting.

Key Insights and Ideas

  • Next-Token Prediction is Compression: Because next-token prediction loss equals prequential code length, sequence models are explicitly optimized to become highly efficient data compressors.
  • Adaptation Speed Reflects Simplicity: In prequential coding, a learner's complexity is determined by how quickly it adapts to new data. Simple models adapt faster from fewer tokens, resulting in a lower cumulative loss (shorter code length).
  • The Danger of Minimizing Only Training Risk: When a sequence model is trained only to predict past tokens (Train-Risk ICL), it completely loses its simplicity bias. It overfits severely in low-data regimes by utilizing highly complex, high-degree functions to explain the context.
  • Architecture Matters: Simply using a next-token prediction loss does not guarantee optimal compression. The choice of architecture (e.g., standard Transformers vs. State Space Models like Mamba) strongly interacts with the task structure, altering the model's capacity to minimize complexity.

Key Results

  • Superior Low-Data Generalization: Across linear regression, sinusoidal regression, and Mastermind, Prequential ICL consistently outperformed both Train-Risk ICL and standard SGD in low-data regimes, demonstrating the generalization power of its implicit simplicity bias.
  • Overfitting Visualization: When visualizing inferred models for polynomial regression, Train-Risk ICL fit highly erratic, complex curves to overfit noise. In contrast, Prequential ICL naturally recovered smooth, simple functions close to the ground truth.
  • Foundation Model Limitations: When evaluating GPT-4 on the Mastermind task, the large pretrained model failed to meaningfully minimize prequential code length, performing worse than a naive baseline. This highlights that massive scaling alone does not guarantee out-of-distribution task generalization.

Limitations and Future Work

  • Underfitting with Ample Data: While ICL excels with very few examples, it can underfit in large-data regimes compared to standard SGD. This is because ICL's compute is bounded by a single forward pass of the network.
  • Out-of-Distribution Generalization: Current ICL methods still struggle to generalize to entirely novel, open-ended task distributions that fall outside their meta-training data.
  • Future Directions: The authors suggest developing novel architectures with built-in optimization primitives to allow variable compute budgets during inference. They also propose exploring "mixtures of learners" that dynamically combine the sample efficiency of ICL with the large-data optimization capacity of SGD.

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