Understanding In-Context Learning on Structured Manifolds: Bridging Attention to Kernel Methods

Zhaiming Shen, Alexander Hsu, Rongjie Lai, Wenjing Liao

TL;DR

This paper reveals that Transformers performing in-context learning (ICL) are essentially "learning" to be kernel regression machines. By bridging the gap between the attention mechanism and classical kernel methods, the authors prove that Transformers can achieve optimal regression rates for non-linear functions on low-dimensional manifolds, where performance depends on the data's true underlying dimension rather than its raw input size.

The Mystery of In-Context Learning on Manifolds

In-context learning (ICL)—the ability of models like GPT-4 to learn new tasks from a few examples in a prompt without updating their weights—is one of the most celebrated yet mysterious properties of modern AI. While researchers have begun to explain how Transformers perform ICL for simple linear tasks, real-world data is rarely linear.

Most high-dimensional data (like images or text) actually lies on a low-dimensional "manifold"—a structured geometric shape hidden within a high-dimensional space. For example, a $1024 \times 1024$ pixel image lives in a million-dimensional space, but the "manifold" of human faces has far fewer degrees of freedom. This paper tackles a fundamental question: How do Transformers theoretically perform ICL for non-linear functions on these structured manifolds?

Bridging Attention and Kernel Methods

The authors bridge the gap between deep learning and classical statistics using a mix of theoretical construction and empirical validation:

  • The Attention-Kernel Bridge: They demonstrate a mathematical equivalence between the Transformer's attention mechanism and the Nadaraya–Watson kernel estimator, a classic non-parametric regression tool. They explicitly construct a 5-layer Transformer that exactly implements Gaussian kernel regression with zero approximation error.
  • Generalization Theory: Using tools like Rademacher complexity and Dudley entropy integrals, they derive "generalization error bounds." These bounds predict how the model's error will shrink as the number of training tasks ($\Gamma$) and the length of the prompt ($n$) increase.
  • The Manifold Hypothesis: By assuming the data $x$ lies on a $d$-dimensional Riemannian manifold embedded in a $D$-dimensional space (where $d \ll D$), they show how Transformers bypass the "curse of dimensionality."

To validate these theories, the researchers used both synthetic data (regression tasks on a 2D sphere using spherical harmonics) and real-world language data (analyzing the attention scores of a pre-trained GPT-2 Small model).

Key Insights and Results

Attention is an Interaction Kernel

The "aha moment" of the paper is that the Query-Key interaction in a Transformer isn't just a random calculation; it effectively constructs a local similarity kernel. Numerical experiments showed that learned attention scores for smooth functions are highly correlated with a Gaussian kernel.

Correlation between softmax attention scores and kernel function

Intrinsic Dimension over Ambient Dimension

The most significant theoretical finding is that the error rate depends on the manifold's intrinsic dimension $d$, not the ambient space $D$. This explains why Transformers are so efficient: they focus on the underlying structure of the data rather than the "noise" of high-dimensional inputs. The squared generalization error is upper-bounded by a term that scales as $n^{-2\alpha/(2\alpha+d)}$. Crucially, the $d$ in the denominator confirms that the model's performance is tied to the intrinsic geometry of the data.

Minimax Optimality and Algorithm Learning

The authors argue that during pre-training, Transformers aren't just memorizing data; they are learning the algorithm of kernel regression. Once trained, they can apply this algorithm to entirely new tasks provided in the prompt. The paper proves that if a Transformer is trained on enough tasks, it achieves the minimax optimal regression rate for Hölder functions—the theoretical "speed limit" for how fast any machine learning model can learn such functions. In tests on the 2D sphere, the slope of the Mean Squared Error (MSE) in log-log plots perfectly matched these theoretical predictions.

Limitations and Future Work

While a major step forward, the authors acknowledge several constraints:

  • Idealized Assumptions: The proof assumes "clean" data and a very large number of training tasks, which may not reflect the data-starved regimes of some specialized fields.
  • Fixed Prompt Length: The current theory focuses on fixed-length prompts ($n$), whereas real-world usage often involves variable lengths.
  • Noise and Outliers: The impact of noisy labels or "out-of-manifold" data points on the ICL kernel mechanism remains an open question.
  • Architecture Scope: The study focuses on standard attention; exploring how newer architectures (like state space models) relate to kernel methods is a promising next step.

Summary updated

BibTeX
@inproceedings{shen2026understanding,
  author = {Zhaiming Shen and Alexander Hsu and Rongjie Lai and Wenjing Liao},
  title = {{Understanding In-Context Learning on Structured Manifolds: Bridging Attention to Kernel Methods}},
  booktitle = {International Conference on Learning Representations},
  year = {2026},
  url = {https://openreview.net/forum?id=WbRULwqsIy},
}
Figure