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

Transformers as Algorithms: Generalization and Implicit Model Selection in In-context Learning

Yingcong Li, M. Emrullah Ildiz, Dimitris S. Papailiopoulos, Samet OymakarXiv 2023

Introduction

Large Language Models (LLMs) possess a remarkable capability known as In-Context Learning (ICL): the ability to learn new tasks from a few examples in a prompt without any weight updates. While empirically successful, the statistical foundations of ICL have remained elusive. This research formalizes ICL as an "algorithm learning" problem, providing finite-sample generalization bounds and demonstrating that Transformers succeed by implementing stable, near-optimal learning algorithms that perform implicit model selection.

Formalizing In-Context Learning

The authors reframe the Transformer architecture not merely as a sequence predictor, but as a mapping that constructs a hypothesis function at inference time. By treating the Transformer as a learning algorithm, the training process becomes a form of Multitask Learning (MTL).

ICL Framework for NLP and Mathematical Tasks

In this framework, the model is trained on $T$ different tasks, each providing $n$ examples. The core of their theoretical contribution lies in algorithmic stability. They prove that multi-layer Transformers are stable—meaning small perturbations in the prompt do not lead to drastic changes in predictions. This stability, analyzed through covering numbers and Dudley’s entropy integral, allows for bounding the "excess risk," providing a rigorous guarantee for generalization.

Key Insights: Stability and Model Selection

The study reveals several critical properties of how Transformers handle ICL:

  • Implicit Model Selection: Transformers do not just learn a static function; they adaptively choose the best function class for a given prompt. For instance, in ridge regression tasks, the model implicitly tunes regularization parameters based on the observed noise.
  • The Stability-Generalization Link: The self-attention mechanism is inherently stable under bounded weights. This theoretical stability translates to a generalization rate of $O(1/\sqrt{nT})$, indicating that performance improves with both the number of tasks seen during pre-training and the number of examples in the prompt.
  • Architecture vs. Task Complexity: Interestingly, the transfer risk (performance on novel tasks) is primarily driven by task complexity and the diversity of training tasks, rather than the sheer parameter count of the Transformer.

Empirical Results

To validate their theoretical bounds, the authors conducted experiments in controlled synthetic environments, including noisy linear regression, linear tasks with covariance priors, and partially-observed linear dynamical systems (LDS).

Performance of ICL across different synthetic tasks

The results demonstrate that ICL performance closely matches the Bayes-optimal decision (e.g., weighted ridge regression), effectively "discovering" optimal mathematical algorithms. In complex dynamical systems, Transformers outperformed standard auto-regressive least-squares estimators by leveraging learned task priors. Furthermore, empirical tests confirmed that as prompt length increases, the model's predictions become more robust, aligning with the $O(1/n)$ stability rate predicted by the theory.

Limitations and Future Directions

While providing a strong theoretical foundation, the work notes several areas for further exploration. Current bounds focus on average risk across tasks; controlling risk for specific, individual prompts remains a challenge. Additionally, while the theory is general, empirical validation has focused on regression and dynamical systems. Extending these insights to reinforcement learning or complex linguistic reasoning is a key next step in demystifying the "inductive bias" of large-scale pre-training.

Summary updated

BibTeX
@misc{li2023transformers,
  author = {Yingcong Li and M. Emrullah Ildiz and Dimitris S. Papailiopoulos and Samet Oymak},
  title = {{Transformers as Algorithms: Generalization and Implicit Model Selection in In-context Learning}},
  year = {2023},
  url = {https://openreview.net/forum?id=A7dKG1xu_gw},
}
Figure