What Can Transformers Learn In-Context? A Case Study of Simple Function Classes

Shivam Garg, Dimitris Tsipras, Percy Liang, Gregory Valiant

TL;DR

This paper demonstrates that standard Transformers can be trained from scratch to "learn how to learn." By training on sequences of input-output pairs from simple function classes (like linear functions or decision trees), Transformers develop the ability to perform in-context learning at inference time, matching or even exceeding the performance of optimal, task-specific algorithms like Least Squares or Lasso.

What Problem Does This Paper Address?

In-context learning (ICL)—the ability of a model to adapt to a new task using only a few examples in a prompt without updating its parameters—is one of the most striking properties of Large Language Models (LLMs). However, it remains a "black box." Is the model simply retrieving a task it saw during pre-training, or is it executing a general-purpose learning algorithm?

To move beyond the ambiguity of natural language, the authors study ICL through the lens of well-defined mathematical function classes. They ask: Can a Transformer be trained to implement a learning algorithm in its forward pass? By stripping away the complexity of text and focusing on regression and classification of synthetic functions, they provide a controlled environment to probe the mechanisms and limits of in-context learning.

Methods

The authors use a standard decoder-only Transformer (GPT-2 architecture, ~9.5M parameters). Instead of text tokens, the model receives a sequence of interleaved vectors $x_i$ and scalars $y_i = f(x_i)$.

Figure 1: Can we train a model that in-context learns a function class (here linear functions)? We train Transformers by repeatedly sampling a random function f from that class, as well as random inputs x1, . . . , xk and training the model to predict each f (xi) given the prompt x1, f (x1), . . . , xi−1, f (xi−1), xi (wrt squared loss). Then, during inference, we evaluate the model’s ability to predict accurately on new, unseen functions.

Key methodological highlights include: * Meta-Learning Objective: The model is trained to minimize the squared error of its prediction for $y_i$ given all previous pairs $(x_1, y_1, \dots, x_{i-1}, y_{i-1})$ and the current query $x_i$. * Curriculum Learning: Training directly on high-dimensional problems often fails to converge. The authors start with low-dimensional subspaces and short prompt lengths, gradually increasing complexity as training progresses. This was essential for training models on 20+ dimensional inputs. * Algorithmic Baselines: The Transformer's performance is compared against "gold standard" algorithms for each class, such as the Ordinary Least Squares (OLS) estimator for linear functions and Lasso for sparse ones.

Datasets

The study relies entirely on synthetic datasets generated from specific function classes $\mathcal{F}$:

  1. Linear Functions: $f(x) = w^\top x$, where $w$ and $x$ are drawn from Gaussian distributions.
  2. Sparse Linear Functions: Linear functions where only a few coordinates of $w$ are non-zero.
  3. Decision Trees: Depth-4 trees with axis-aligned splits.
  4. Two-layer Neural Networks: ReLU networks with 100 hidden units.

Evaluation also includes several Out-of-Distribution (OOD) scenarios, such as skewed input covariances, noisy labels, and query inputs drawn from different orthants than the in-context examples.

Key Insights and Ideas

  • Transformers as Implicit Optimizers: The most profound insight is that the Transformer's forward pass effectively "implements" a learning algorithm. For linear regression, the model's behavior closely mimics the Least Squares estimator.
  • Matching Specialized Algorithms: For sparse linear functions, the Transformer doesn't just do basic regression; it learns to exploit sparsity, matching the performance of Lasso—an iterative optimization procedure—in a single forward pass.
  • Surpassing Heuristics: In the case of decision trees, the Transformer significantly outperforms standard greedy algorithms like ID3 and even tree-boosting methods like XGBoost for the studied distribution.
  • Robustness to Distribution Shift: The models aren't just memorizing training prompts. They generalize to OOD prompts (e.g., handling noisy data even when trained on clean data), suggesting they have learned a somewhat general version of the underlying algorithm.

Key Results

  • Linear Regression: The Transformer matches the optimal Least Squares error across varying numbers of in-context examples. As shown in Figure 2, the error drops sharply and hits near-zero exactly when the number of examples $k$ reaches the dimension $d$.
Figure 2: Transformer performance on linear functions compared to Least Squares and other baselines.
  • Algorithmic Alignment: By analyzing the gradients of the model's predictions, the authors found that the Transformer's internal "weight vector" aligns almost perfectly with the true $w$ (or its projection) once enough examples are provided.
  • Scaling: Increasing model capacity (number of parameters) consistently improves ICL performance and robustness, especially for higher-dimensional problems and OOD scenarios.

Limitations and Future Work

  • Simplicity of Tasks: While impressive, these function classes are far simpler than the reasoning required for natural language. It remains to be seen how these "algorithmic" capabilities scale to the complexity of LLMs.
  • Mechanistic Interpretability: While the paper shows that Transformers learn these algorithms, the exact internal "circuitry" (e.g., how attention heads collaborate to perform a matrix inversion or gradient step) is still largely a mystery.
  • Training Cost: The models require significant synthetic data and curriculum learning to converge, which might be a bottleneck for more complex function classes.

The authors suggest that future work should focus on "reverse-engineering" the specific algorithms discovered by the Transformer, potentially leading to new, sample-efficient learning algorithms for difficult problems.

Summary updated

BibTeX
@inproceedings{garg2022what,
  author = {Shivam Garg and Dimitris Tsipras and Percy Liang and Gregory Valiant},
  title = {{What Can Transformers Learn In-Context? A Case Study of Simple Function Classes}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2022},
  url = {https://openreview.net/forum?id=flNZJ2eOet},
}
Figure