NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Transformers as Algorithms: Generalization and Stability in In-context Learning

Yingcong Li, Muhammed Emrullah Ildiz, Dimitris Papailiopoulos, Samet Oymak

Large Language Models (LLMs) like GPT-4 exhibit a remarkable ability known as In-Context Learning (ICL): they can learn to perform new tasks simply by observing a few examples in a prompt, without any weight updates. Despite its empirical success, a rigorous statistical understanding of why and how this works has remained elusive.

This paper formalizes ICL as an "algorithm learning" problem. Instead of learning a single function, the Transformer implicitly constructs a hypothesis function at inference time. By linking ICL to algorithmic stability, the authors provide theoretical generalization bounds, proving that Transformers can implement near-optimal statistical algorithms for both standard regression and dynamical systems.

Framing In-Context Learning

Traditional machine learning theory assumes model weights are updated during training. In ICL, the weights are frozen; the "learning" happens entirely within the forward pass. To analyze this, the authors frame the Transformer as a meta-learning engine that learns an algorithm—a mapping from a dataset to a function—during its pre-training phase.

Prompt structures for NLP, supervised learning, and dynamical systems.

The study explores this abstraction through two primary data structures:

  1. Supervised Learning: The prompt is a sequence of independent and identically distributed (i.i.d.) input-label pairs $(x_1, y_1, \dots, x_i)$.
  2. Dynamical Systems: The prompt is a trajectory arising from a moving system $(x_1, x_2, \dots, x_i)$, where the Transformer must predict the next state. This is crucial for understanding how Transformers might act as controllers or reinforcement learning agents.

Theoretical Insights: Stability and Generalization

The crux of the paper's theoretical contribution is relating ICL generalization to algorithmic stability (adapting the classical framework of Bousquet & Elisseeff, 2002). An algorithm is considered stable if changing a single training point in the prompt does not drastically alter the output prediction.

The authors prove that multi-layer Transformers are theoretically stable under certain normalization conditions. If the attention mechanism is too sensitive to a single token, the model will fail to generalize.

By analyzing the training phase as Multi-Task Learning (MTL) across $T$ tasks, each with $n$ examples, they derive a generalization rate of $1/\sqrt{nT}$. This demonstrates that ICL benefits from both a larger diversity of pre-training tasks and longer inference prompts. Furthermore, they identify an inductive bias phenomenon: for transfer learning on unseen tasks, the risk is governed by the underlying task complexity (e.g., dimensionality) rather than the parameter count of the Transformer. A small model and a massive model perform almost identically if trained on the same task distribution.

Empirical Results: Matching Bayes-Optimality

To verify their theoretical claims, the authors tested Transformers on controlled synthetic environments, including noisy linear regression, linear tasks with covariance priors, and partially-observed Linear Dynamical Systems (LDS).

Test risk of ICL compared to baselines across noisy linear regression, covariance priors, and partially-observed LDS.

The results are striking:

  • Bayes-Optimality: On linear regression tasks, Transformers do not just perform adequately; they match the Bayes-optimal performance (optimally-weighted ridge regression). The model effectively learns the "prior" of the tasks during pre-training and applies it during ICL.
  • Outperforming Baselines: In dynamical systems, ICL outperforms standard auto-regressive least-squares estimators. This proves the Transformer can leverage long-term memory and temporal dependencies more effectively than simple statistical baselines.
  • Noise as a Stabilizer: Interestingly, empirical tests revealed that training with noisy data actually makes the Transformer's ICL more stable and robust to adversarial or noisy prompts.

Limitations and Future Directions

While this work provides a robust statistical foundation for ICL, it has limitations. The current stability proof has an exponential dependence on the number of layers ($L$). While typical Transformers are not deep enough for this to cause immediate mathematical explosions, tighter bounds for very deep models are necessary.

Additionally, the study focuses on continuous regression and dynamical systems to allow for precise mathematical verification. Extending these "algorithm learning" proofs to discrete natural language tokens remains an open challenge. However, the dynamical system results lay highly promising groundwork for analyzing Transformers in reinforcement learning and imitation learning contexts in the future.

Summary updated

BibTeX
@inproceedings{li2023transformers,
  author = {Yingcong Li and Muhammed Emrullah Ildiz and Dimitris Papailiopoulos and Samet Oymak},
  title = {{Transformers as Algorithms: Generalization and Stability in In-context Learning}},
  booktitle = {International Conference on Machine Learning},
  year = {2023},
  url = {https://openreview.net/forum?id=CgB7wCExOF},
}
Figure