In-Context Learning through the Bayesian Prism

Madhur Panwar, Kabir Ahuja, Navin Goyal

In-Context Learning through the Bayesian Prism: A Summary

Large Language Models (LLMs) possess a remarkable ability called In-Context Learning (ICL): they can learn to perform new tasks simply by being shown a few examples in a prompt, without any updates to their internal weights. While this "magic" is central to their utility, the underlying mechanics have remained elusive. This paper from researchers at Microsoft Research India and the University of Washington proposes a unifying framework: ICL is essentially Bayesian inference performed on the pretraining distribution.

TL;DR

This paper demonstrates that high-capacity transformers performing in-context learning act as Bayesian predictors, mimicking the optimal statistical solution for their training distribution. By studying stylized "hierarchical" task mixtures, the authors show that the model's inductive biases—including its preference for simpler functions—are directly inherited from the data it saw during pretraining.

What Problem Does This Paper Address?

The core mystery is why transformers generalize the way they do in-context. Previous research showed that transformers could learn simple function classes (like linear regression) from examples, often implementing well-known algorithms like Gradient Descent internally. However, it wasn't clear if this was a general property or how the model handles a mixture of different tasks.

The authors tackle the lack of a theoretical "prism" through which to view ICL. They test the Bayesian hypothesis: that a transformer with sufficient capacity and data will learn to output the posterior mean estimate (PME) of the target value, given the examples in the prompt and its knowledge of the pretraining distribution.

Methods

The authors employ a stylized meta-learning setup called Meta-ICL (MICL) and introduce a more complex version, Hierarchical Meta-ICL (HMICL).

  1. HMICL Setup: Instead of training on just one type of function (e.g., only linear), the model is trained on a mixture of function families (e.g., linear regression, decision trees, and neural networks). This better reflects the multi-task nature of real-world LLMs.
  2. Bayesian Benchmarking: For tasks where the exact Bayesian solution is mathematically tractable (like Gaussian Mixture Models), they compare the transformer's predictions directly against the Posterior Mean Estimator (PME).
  3. Weight Probing: They don't just look at the final answer; they "probe" the model to extract the internal weights it's implicitly using for regression tasks and compare them to the weights a Bayesian solver would use.
  4. Simplicity Bias Analysis: Using Fourier series, they investigate whether the model has an inherent "simplicity bias" (preferring low-frequency functions) or if this bias is simply a reflection of the training data.

Datasets

The study uses synthetic datasets consisting of sequences of input-output pairs $(x, f(x))$. The function $f$ is sampled from various families:

  • Linear Inverse Problems: Dense regression, sparse regression, sign-vector regression, low-rank regression, and skewed covariance regression.
  • Non-linear Functions: Fourier series, degree-2 monomials, random Fourier features, and Haar wavelets.
  • Heterogeneous Mixtures: Combinations of the above, plus decision trees and 2-layer neural networks.

Key Insights and Ideas

  • Transformers as Bayesian Mimics: High-capacity transformers don't just "learn an algorithm"; they converge to the optimal Bayesian predictor for their pretraining distribution. As shown in Figure 1, the transformer's error and its "implied weights" track the Bayesian PME almost perfectly. Transformers simulate PME when trained on dense regression task-mixture. The left plots show loss alignment, and the right plots show weight alignment.

  • Intertwined Task Recognition and Solution: A common theory is that LLMs first "recognize" the task and then "solve" it. The Bayesian perspective suggests these are not separate steps. The model naturally weights different task hypotheses based on how well they explain the prompt examples (Equation 1 in the paper).

  • Simplicity Bias is Data-Driven: The famous "simplicity bias" in neural networks appears to be a byproduct of the pretraining distribution. In Fourier series experiments, if the training data is balanced, the model shows no bias. If the data favors low frequencies, the model exhibits simplicity bias (see Figure 2). Measuring frequencies during ICL. The mixture case (right) shows a clear bias for lower frequencies at small prompt lengths, reflecting the training distribution.

  • The "Forgetting" Phenomenon: In some multi-task settings, transformers initially learn to generalize to a broad class of functions (like all linear functions) but, as pretraining continues, they "forget" this broad generalization to better memorize the specific, narrower distribution of tasks seen in the training data.

Figure 1: Transformers simulate PME when trained on dense regression task-mixture with weights having a mixture of Gaussian prior (GMM). (left): Comparing the performance of the Transformer with PME of individual Gaussian components (PME (T1) and PME (T2)) and of the mixture PME (GMM). (right): MSE between the probed weights of the Transformer and PMEs.
Figure 2: Measuring the frequencies of the simulated function during ICL by transformer.

Key Results

  • Quantitative Alignment: In Gaussian Mixture Model (GMM) experiments, the transformer's loss curves and recovered weights were nearly identical to the exact Bayesian PME across all prompt lengths.
  • OOD Generalization: When trained on a high diversity of tasks ($K$), transformers can generalize to entirely new function classes not seen during pretraining. As shown in Figure 3 for monomials, increasing task diversity leads the model to behave like specialized algorithms (e.g., Lasso) even on out-of-distribution (OOD) tasks. Multi-task generalization for Monomials. As task diversity (K) increases, the model generalizes better to OOD tasks.

  • Robustness: The Bayesian behavior was consistent across a wide variety of linear and non-linear function families, provided the model had sufficient capacity.

Multi-task generalization for Monomials. As task diversity (K) increases, the model generalizes better to OOD tasks.

Limitations and Future Work

  • Continuous vs. Discrete: The study focuses on continuous function regression, which is easier to analyze than the discrete token prediction of real-world LLMs.
  • Order of Examples: In these stylized setups, the order of examples in a prompt doesn't matter for the Bayesian predictor, but it is known to significantly affect real LLMs. This suggests further nuances are needed to bridge the gap.
  • Mechanistic Interpretability: While the paper shows what the model is doing (Bayesian inference), it doesn't fully uncover the internal circuit mechanisms (the how) that implement this logic.
  • Future Directions: The authors suggest investigating how the Bayesian view can explain LLM phenomena like hallucinations or jailbreaking.

Summary updated

BibTeX
@inproceedings{panwar2024context,
  author = {Madhur Panwar and Kabir Ahuja and Navin Goyal},
  title = {{In-Context Learning through the Bayesian Prism}},
  booktitle = {International Conference on Learning Representations},
  year = {2024},
  url = {https://openreview.net/forum?id=HX5ujdsSon},
}
Figure