In-context learning (ICL) is one of the most striking emergent properties of Large Language Models (LLMs). By simply providing a few examples (demonstrations) in a prompt, we can "teach" a model to perform a new task without updating a single weight. However, ICL is notoriously finicky—changing the order or selection of these examples can lead to wild swings in performance.
A paper from researchers at UC Santa Barbara and UC Irvine, presented at NeurIPS 2023, proposes an elegant theoretical framework to explain this phenomenon: LLMs are actually latent variable models.
TL;DR
The authors argue that LLMs perform in-context learning by implicitly inferring a latent "concept" variable from the provided demonstrations. By using a small model to explicitly learn these latent concepts and then selecting demonstrations that best represent them, they achieve significant performance gains that generalize across different model families and scales (up to 175B parameters).
What Problem Does This Paper Address?
While ICL is widely used, we don't fully understand why it works or how to pick the "best" demonstrations. Most current methods rely on heuristics, such as picking examples that are semantically similar to the test input.
Previous theoretical work suggested that LLMs act like Bayesian predictors, but these theories often relied on unrealistic assumptions (like the data being generated by a simple Hidden Markov Model). There was a massive gap between these "toy" mathematical proofs and the behavior of real-world models like GPT-3 or LLaMA. This paper seeks to bridge that gap with a theory that leads to a practical, high-performance algorithm for demonstration selection.
Methods
The authors view the LLM as a latent variable model. In their view, a prompt helps the model infer a latent variable $\theta$ (representing the task, format, and concept). The model then generates a continuation based on that inferred concept.
They propose a two-phase algorithm to optimize this process:
- Latent Concept Learning: They take a small LLM (like GPT2-large) and use prompt tuning to learn a set of "concept tokens" for a specific task. These tokens are essentially a numerical representation of the latent variable $\theta$.
- Demonstration Selection: Once they have the "ideal" concept tokens for a task, they look through a pool of candidate examples. They select the $k$ examples that are most likely to make the model infer those specific concept tokens.
Crucially, they found that demonstrations selected using a small model (e.g., 774M parameters) work exceptionally well when used with massive models (e.g., 175B parameters). This suggests that different LLMs share a similar underlying "concept space" due to their similar pre-training data.
Datasets
The researchers evaluated their method across two main categories:
- Text Classification: Eight datasets covering sentiment analysis (SST2, FPB), linguistic analysis (COLA), topic classification (DBpedia), emotion (EmoC, EmoS), and hate speech detection (ETHOS-SO, ETHOS-R).
- Mathematical Reasoning: The GSM8K dataset, which consists of high-quality math word problems requiring multi-step reasoning.
Key Insights and Ideas
- The Bayesian "Aha!" Moment: The paper reinforces the idea that ICL isn't just "pattern matching." Instead, the prompt acts as evidence in a Bayesian inference process, helping the model "locate" the correct task it already learned during pre-training.
- Causal Direction Matters: The authors highlight that the relationship between input ($X$) and label ($Y$) changes by task. In sentiment analysis, the sentiment ($Y$) "causes" the review ($X$). In other tasks, the input causes the label. Accounting for this causal flow improves the selection process.
- Transferability of "Goodness": One of the most practical findings is that "good" demonstrations are somewhat universal. You can use a cheap, small model to curate a prompt that makes an expensive, large model perform better.
- Order Independence: Unlike many heuristic methods where the order of examples matters immensely, the authors found that their latent-variable-selected examples were relatively insensitive to ordering. This suggests they are capturing the "core" essence of the task rather than just surface-level patterns.
Key Results
- Significant Gains: On average, their method provided a 12.5% relative improvement over uniform random selection across eight different GPT models.
- Outperforming Heuristics: Their method consistently beat the common "Similarity" baseline (selecting examples similar to the test query).
- Math Reasoning Boost: On the GSM8K dataset, using demonstrations selected by a 7B LLaMA-2 model improved ChatGPT’s accuracy from 76.5% to 81.2%.
- Visual Proof: t-SNE visualizations of the learned concept tokens showed that tokens for semantically similar tasks (like different sentiment datasets) naturally clustered together, providing qualitative evidence for their latent variable hypothesis.
Limitations and Future Work
- The "True Distribution" Assumption: The theory assumes LLMs perfectly capture the true distribution of language. In reality, LLMs often struggle with "long-tail" or rare sequences.
- Task Set Selection: The algorithm currently requires a set of "diverse tasks" to help calibrate the latent variable learning, and choosing this set is still somewhat manual.
- Beyond Classification: While they showed success on GSM8K, the authors acknowledge that more complex latent variables are needed to fully capture the nuances of logical reasoning and long-form generation.
- Computational Cost: While more efficient than searching through all combinations of examples, the initial "concept learning" phase still requires a GPU-intensive training step.
Reference: Wang, X., et al. (2023). Large Language Models Are Latent Variable Models: Explaining and Finding Good Demonstrations for In-Context Learning. NeurIPS 2023.
Summary updated