In-Context Learning through the Bayesian Prism

Madhur Panwar, Kabir Ahuja, Navin Goyal

Large Language Models (LLMs) possess the remarkable ability to learn new tasks from just a few examples in a prompt—a phenomenon known as In-Context Learning (ICL). While researchers have shown that Transformers can "learn" simple functions like linear regression in-context without any weight updates, the underlying inductive biases driving this behavior remain poorly understood.

This paper proposes that ICL in Transformers is best understood through a "Bayesian Prism." By training models on mixtures of different function families, the authors demonstrate that high-capacity Transformers don't just learn specific algorithms; they essentially mimic a Bayesian predictor (specifically the Posterior Mean Estimator) that reflects the distribution of tasks seen during pretraining.

The Hierarchical Meta-ICL Framework

To understand how Transformers handle prompts that could belong to multiple different task families (e.g., is this a linear regression problem or a decision tree?), the researchers extended the standard Meta-In-Context Learning (MICL) framework to a Hierarchical Meta-ICL (HMICL) setup.

Instead of training on a single function class, they trained GPT-style (decoder-only) Transformers on a mixture of function families. They used stylized, synthetic datasets where the ground truth is mathematically controllable, including linear inverse problems (dense, sparse, low-rank regression) and non-linear functions (decision trees, shallow neural networks, Fourier series).

The core of their methodology involved:

  • Posterior Mean Estimator (PME) Comparison: Mathematically deriving the optimal Bayesian solution (the PME) for task mixtures and comparing the Transformer’s predictions against it.
  • Weight Probing: Extracting the implicit weights the Transformer uses to solve a regression task and comparing them to the weights a Bayesian solver would use.

Key Findings: The Transformer as a Bayesian Mimic

When a model has enough capacity, its ICL behavior converges to the Bayesian predictor of its pretraining distribution. It doesn't need a separate "task identification" step; the Bayesian math naturally handles task ambiguity.

Transformer vs PME Performance and Weight Alignment
  • Weight Alignment: In Gaussian Mixture Models, the "implied weights" recovered from the Transformer were almost identical to those of the Bayesian PME.
  • Task Switching: In mixtures of linear regression and decision trees, the Transformer matched the performance of single-task models, effectively "switching" its internal logic based on the prompt.
  • Diversity Breeds Generalization: As the diversity of tasks in the pretraining mixture increases, the model's ability to generalize to entirely unseen function classes improves significantly, eventually matching the performance of specialized baselines.

Rethinking Simplicity Bias

A common assumption in machine learning is that models possess an inherent "simplicity bias"—a tendency to prefer simpler explanations for data. To study this in ICL, the authors used Fourier series as a function class, allowing them to measure whether the model prefers low-frequency (simple) or high-frequency (complex) components based on its training data.

Fourier Series HMICL Analysis

The paper argues that ICL's preference for "simple" solutions isn't necessarily an inherent property of the architecture. Instead, it's a reflection of the pretraining data. If the training set is biased toward low-frequency functions, the model will show a simplicity bias; if biased toward high-frequency, it will show a "complexity bias."

Interestingly, the authors also discovered a "forgetting" phenomenon. During pretraining on a limited number of tasks, models often first generalize to a broad distribution (like general linear regression) and then later forget that generalization to overfit the specific tasks in their training set.

Limitations and Future Directions

While this Bayesian perspective offers profound insights, the study relies on synthetic mathematical functions. It remains to be seen how perfectly this maps to the messy, discrete world of natural language. Furthermore, while we know what the model is mimicking (the Bayesian predictor), we still lack a complete mechanistic understanding of the specific circuit-level "how" for many non-linear tasks.

Future work will likely explore how this Bayesian view can help us choose better "demonstrations" (few-shot examples) to make LLM outputs converge faster to a desired distribution in real-world applications.

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