In-Context Learning of Large Language Models Explained as Kernel Regression

Chi Han, Ziqi Wang, Han Zhao, Heng Ji

Abstract

Large language models (LLMs) have initiated a paradigm shift in transfer learning. In contrast to the classic pretraining-then-finetuning procedure, in order to use LLMs for downstream prediction tasks, one only needs to provide a few demonstrations, known as in-context examples, without adding more or updating existing model parameters. This in-context learning (ICL) capabilities of LLMs is intriguing, and it is not yet fully understood how pretrained LLMs acquire such capabilities. In this paper, we investigate the reason why a transformer-based language model can accomplish in-context learning after pre-training on a general language corpus by proposing one hypothesis that LLMs can simulate kernel regression algorithms when faced with in-context examples. More concretely, we first prove that Bayesian inference on in-context prompts can be asymptotically understood as kernel regression $\hat y = \frac{\sum_i y_i K(x, x_i)}{\sum_i K(x, x_i)}$ as the number of in-context demonstrations grows. Then, we empirically investigate the in-context behaviors of language models. We find that during ICL, the attentions and hidden features in LLMs match the behaviors of a kernel regression. Finally, our theory provides insights on multiple phenomena observed in ICL field: why retrieving demonstrative samples similar to test sample can help, why ICL performance is sensitive to the output formats, and why ICL accuracy benefits from selecting in-distribution and representative samples.

The Mechanism of In-Context Learning

In-Context Learning (ICL) allows LLMs to perform new tasks simply by observing a few examples in a prompt. While this is a hallmark of modern AI, the underlying mechanism has remained a "black box." Existing theories suggest implicit gradient descent or Bayesian inference, but they often struggle to explain how the Transformer architecture implements this during a single forward pass.

This research proposes a unifying hypothesis: LLMs are not just "reasoning" in a traditional sense; they are performing a non-parametric statistical estimation known as kernel regression. Specifically, the model uses its attention mechanism to calculate the similarity between a test input and provided demonstrations, then aggregates the labels of those demonstrations to produce a prediction.

The proposed framework: LLMs perform in-context learning by simulating a Nadaraya-Watson kernel regression estimator.

Theoretical and Empirical Evidence

Bayesian Convergence to Kernel Regression

By assuming pre-training data is a mixture of Hidden Markov Models (HMMs), the authors prove that as the number of demonstrations ($n$) increases, the Bayesian prediction for the next token converges to a Nadaraya-Watson kernel regression estimator. In this framework, the "kernel" measures the semantic similarity between the test input and the examples provided in the context window.

Attention as the Similarity Kernel

To verify this theory, the researchers analyzed models like GPT-J 6B and Llama-2. They found that the Transformer's attention mechanism acts as a near-perfect hardware implementation of kernel regression. The Query-Key dot product serves as the similarity kernel, while the Value vectors store the label information ($y_i$) being aggregated.

Visualization of attention weights across layers and heads, highlighting how the model attends to demonstration labels ($y_i$) to perform kernel-like aggregation.

Empirical tests showed that one can "reconstruct" the model's final prediction with high accuracy (e.g., 89.2% on SST-2) using only the raw attention weights assigned to demonstration labels. This suggests that the model's output is largely a weighted sum of the labels it was shown in the prompt.

Key Insights and Implications

  • Layer-wise Specialization: Kernel regression behavior is not uniform across the model. It emerges most strongly in the middle-to-late layers (e.g., layers 16–21 in GPT-J), indicating that earlier layers are responsible for computing the semantic features used by the kernel.
  • Explaining Heuristics: This perspective explains why retrieving examples similar to the test query improves performance: it narrows the "kernel bandwidth," reducing bias. It also explains sensitivity to label formatting, as the model must "anchor" its attention to specific positions to extract label values.
  • OOD Robustness: The theory predicts that Out-of-Distribution (OOD) inputs, such as those containing rare words or typos, will degrade performance because the kernel relies on "belief states" learned during pre-training. Experiments confirmed this, showing accuracy drops from 0.805 to 0.534 when typos are introduced.

Conclusion and Future Work

While this kernel regression bridge provides a powerful explanatory tool for ICL, some mysteries remain. For instance, standard kernel regression is order-independent, yet LLMs are sensitive to the order of examples in a prompt. Future work will need to address how the Transformer's positional encoding and sequential processing modify this basic regression mechanism. Additionally, while the HMM assumption is a useful theoretical tool, further research is needed to see if these findings hold for the full hierarchical complexity of natural language.

Summary updated

Abstract

Large language models (LLMs) have initiated a paradigm shift in transfer learning. In contrast to the classic pretraining-then-finetuning procedure, in order to use LLMs for downstream prediction tasks, one only needs to provide a few demonstrations, known as in-context examples, without adding more or updating existing model parameters. This in-context learning (ICL) capabilities of LLMs is intriguing, and it is not yet fully understood how pretrained LLMs acquire such capabilities. In this paper, we investigate the reason why a transformer-based language model can accomplish in-context learning after pre-training on a general language corpus by proposing one hypothesis that LLMs can simulate kernel regression algorithms when faced with in-context examples. More concretely, we first prove that Bayesian inference on in-context prompts can be asymptotically understood as kernel regression $\hat y = \frac{\sum_i y_i K(x, x_i)}{\sum_i K(x, x_i)}$ as the number of in-context demonstrations grows. Then, we empirically investigate the in-context behaviors of language models. We find that during ICL, the attentions and hidden features in LLMs match the behaviors of a kernel regression. Finally, our theory provides insights on multiple phenomena observed in ICL field: why retrieving demonstrative samples similar to test sample can help, why ICL performance is sensitive to the output formats, and why ICL accuracy benefits from selecting in-distribution and representative samples. We will make our code available to the research community following publication.
BibTeX
@article{han2025context,
  author = {Chi Han and Ziqi Wang and Han Zhao and Heng Ji},
  title = {{In-Context Learning of Large Language Models Explained as Kernel Regression}},
  journal = {Accepted by TMLR},
  year = {2025},
  url = {https://openreview.net/forum?id=6rD50Q6yYz},
}
Figure