Transformers Meet In-Context Learning: A Universal Approximation Theory

Gen Li, Yuchen Jiao, Yu Huang, Yuting Wei, Yuxin ChenarXiv 2025 · DOI

Abstract

In-context learning (ICL) allows Large Language Models to solve novel tasks using only a few demonstrations, yet theoretical understanding of this phenomenon has largely been confined to simple linear regression. This paper establishes a "Universal Approximation Theory" for ICL, proving that transformers can approximate nearly any general function class—including complex non-linear and non-convex functions—without updating their weights. By constructing a transformer that emulates the proximal gradient method to solve a Lasso problem, the authors demonstrate that the architecture can identify task-specific features from context at test time. Our analysis shows that the required model complexity scales only logarithmically with the complexity of the function class, providing a theoretical foundation for the efficiency of transformers as universal in-context learners.

Introduction

The ability of Large Language Models (LLMs) to perform In-Context Learning (ICL) has transformed natural language processing, yet the mechanism remains a subject of intense study. Existing literature often frames transformers as "algorithm approximators" that mimic Gradient Descent specifically for linear regression tasks. However, this framework fails to explain the non-linear, high-dimensional capabilities observed in practice. This paper addresses this gap by investigating whether transformers can serve as universal in-context learners for general, non-linear function classes.

Theoretical Framework: ICL as a Lasso Problem

The authors bridge the gap between classical approximation theory and modern transformer architectures through a three-step constructive proof.

1. Universal Feature Representation

Drawing on Barron’s Theory, the authors demonstrate that any function $f$ within a general class $\mathcal{F}$ (defined by bounded Fourier magnitude) can be represented as a linear combination of a set of "universal features." A critical component of this proof is showing that these linear coefficients possess a small $\ell_1$-norm, making the target function compressible.

2. The Lasso Formulation

Because the target function is a sparse or compressible linear combination of universal features, the task of ICL is reframed as a Lasso (Least Absolute Shrinkage and Selection Operator) problem. The goal of the transformer, when presented with $N$ examples, is to identify the best coefficients for these features to minimize prediction error.

3. Transformer Construction

The authors provide a manual construction of transformer weights to emulate the Proximal Gradient Method, a standard optimization algorithm for Lasso problems:

  • Attention Layers: These layers are configured to compute the gradient of the loss function across the provided in-context examples.
  • Feed-Forward (MLP) Layers: These layers implement the "soft-thresholding" operator, which is the core component of the proximal step required for Lasso optimization.

Key Insights

Beyond Linear Regression

By moving the theoretical conversation from "gradient descent for lines" to "Lasso for universal features," the paper accounts for the transformer's ability to handle non-convex and non-linear tasks. This suggests that the ICL mechanism is far more robust than previously theorized.

The "Universal" Transformer

In this construction, the weights of the transformer depend only on the broad class of functions (e.g., the set of all smooth functions) rather than the specific task. The specific task is solved entirely at test time through the activations in the hidden layers, effectively decoupling the "learning of the algorithm" (pre-training) from the "execution of the algorithm" (inference).

Logarithmic Scaling

A significant finding is that the required model depth ($L$) and dimension ($D$) only need to grow logarithmically with the "covering number" of the function class. This indicates that transformers are highly efficient at representing complex task spaces, requiring relatively small increases in parameters to handle significantly more complex function families.

Results and Error Bounds

The authors derive a formal bound for the Mean Squared Prediction Error (MSPE) of their constructed transformer. After processing $N$ examples, the error is bounded by:

$$\text{Error} \lesssim \sqrt{\frac{\log N}{N}} + \frac{n}{L} + \frac{\log |\mathcal{N}_\epsilon|}{n}$$

Where:

  • $N$ is the number of in-context examples.
  • $L$ is the number of transformer layers.
  • $n$ relates to the number of universal features.
  • $|\mathcal{N}_\epsilon|$ is the covering number of the function class.

As the number of examples ($N$), model depth ($L$), and internal dimension ($n$) increase, the prediction risk vanishes. This proves that the transformer architecture is, in principle, a universal in-context learner.

Discussion and Limitations

While this work provides a rigorous proof of capability, it is a constructive proof rather than an empirical one. It demonstrates that a transformer can exist with these properties, but it does not guarantee that standard training via Stochastic Gradient Descent (SGD) on a massive corpus will naturally converge to this specific configuration.

Furthermore, the construction assumes the function class $\mathcal{F}$ is known during the design phase. In practice, LLMs encounter an unknown variety of task structures. Future research is needed to determine how the non-convex dynamics of actual LLM training lead to the emergence of these universal capabilities and whether the performance bounds derived here can be further tightened.

Summary updated

BibTeX
@misc{li2025transformers,
  author = {Gen Li and Yuchen Jiao and Yu Huang and Yuting Wei and Yuxin Chen},
  title = {{Transformers Meet In-Context Learning: A Universal Approximation Theory}},
  year = {2025},
  eprint = {2506.05200},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2506.05200},
}
Figure