What and How does In-Context Learning Learn? Bayesian Model Averaging, Parameterization, and Generalization

Yufeng Zhang, Fengzhuo Zhang, Zhuoran Yang, Zhaoran Wang

Abstract

In-Context Learning (ICL) allows Large Language Models (LLMs) to complete tasks from examples in a prompt without parameter tuning. This work provides a statistical perspective on ICL, demonstrating that perfectly pretrained LLMs perform Bayesian Model Averaging (BMA) under a dynamic model of prompt examples. We show that the transformer's attention mechanism is mathematically structured to implement BMA and decompose the total ICL error into statistical regret and pretraining error. Our analysis reveals that ICL performance is bounded by the quality of pretraining and that task-specific information is represented as discrete "hidden concepts" within the model's attention heads.

Introduction

In-Context Learning (ICL) is a celebrated emergent property of Large Language Models, yet its theoretical underpinnings have remained elusive. While LLMs can learn new tasks from just a few examples, bridging the gap between the practice of "pretraining + prompting" and formal statistical theory is a significant challenge. This paper addresses this by modeling ICL as a Bayesian inference process, providing a framework to understand how the transformer architecture—specifically the attention mechanism—facilitates learning from context.

Theoretical Framework: LLMs as Bayesian Engines

The authors propose a Dynamic Hidden Variable Model to describe the structure of prompts. In this model, a prompt contains a hidden concept $z$ (such as a translation rule or a taxonomic category) that governs the relationship between inputs and outputs.

LLMs implement Bayesian Model Averaging by calculating a posterior over hidden concepts $z$ based on prompt examples.

They mathematically prove that a perfectly pretrained LLM—one that has captured the true data distribution—naturally performs Bayesian Model Averaging (BMA). Instead of simple pattern matching, the model calculates a posterior distribution over the hidden concept $z$ given the prompt examples and uses this distribution to weight its next-token predictions. This formalizes the intuition that LLMs "infer" the task from the context.

The Attention Mechanism and BMA Implementation

A central question in ICL research is how the specific architecture of a transformer enables such learning. The authors demonstrate that the standard softmax attention mechanism is not just a weighting tool but is fundamentally designed for Bayesian inference.

They prove that as the number of examples in a prompt increases, softmax attention converges to an optimal BMA implementation. In Gaussian linear models, this allows for the explicit construction of hidden concepts from attention head values, providing a direct link between the model's parameters and its statistical behavior.

Error Decomposition and Generalization

Using a PAC-Bayes framework, the study decomposes the average error of ICL into two distinct parts:

  1. Statistical Regret: The inherent error of learning from a finite number of examples, which vanishes at a rate of $O(1/T)$, where $T$ is the number of examples.
  2. Pretraining Error: The approximation and generalization error incurred during the model's initial training phase.

This decomposition highlights that pretraining acts as a "performance floor." No matter how many examples are provided in a prompt, ICL performance is ultimately limited by how well the model learned the underlying concept space during pretraining. If a concept was not well-represented in the pretraining data, the model will struggle to infer it in-context.

Empirical Validation: Extracting Hidden Concepts

To verify their theoretical findings, the authors conducted experiments using Llama2-7b and synthetic datasets. They successfully extracted "hidden concept vectors" by averaging specific attention head values.

Comparison of Zero-Shot Hidden Concept (zs-hc) accuracy against standard few-shot ICL across various natural language tasks.

As shown in the evaluation of tasks like Antonyms, Country-Capital pairs, and Present-Past tense transformations, injecting these extracted vectors into the model (the "zs-hc" method) allows it to perform tasks zero-shot with accuracy comparable to or exceeding standard ICL with 10 examples. This confirms that the model internally represents tasks as tangible, task-specific information stored within its attention layers.

Conclusion

This research provides a rigorous statistical foundation for understanding In-Context Learning. By identifying BMA as the core mechanism and proving that the attention structure is optimized for this process, the authors offer a clear explanation for "how" and "what" LLMs learn from prompts. While the study focuses on few-shot examples and linear approximations, it sets the stage for future work on instruction-following and the non-linear complexities of natural language processing.

Summary updated

BibTeX
@inproceedings{zhang2025what,
  author = {Yufeng Zhang and Fengzhuo Zhang and Zhuoran Yang and Zhaoran Wang},
  title = {{What and How does In-Context Learning Learn? Bayesian Model Averaging, Parameterization, and Generalization}},
  booktitle = {International Conference on Artificial Intelligence and Statistics},
  year = {2025},
  url = {https://openreview.net/forum?id=B50OF0Fc6O},
}
Figure