Does learning the right latent variables necessarily improve in-context learning?

Sarthak Mittal, Eric Elmoznino, Leo Gagnon, Sangnie Bhardwaj, Tom Marty, Dhanya Sridhar, Guillaume Lajoie

TL;DR

Forcing Transformers to explicitly infer underlying task parameters (latent variables) makes them highly interpretable, but surprisingly does not improve their out-of-distribution generalization. The bottleneck in in-context learning isn't a failure to extract the underlying rules of a task; it's the model's inability to learn how to apply them downstream.

What Problem Does This Paper Address?

Large language models excel at In-Context Learning (ICL)—the ability to pick up a new task at inference time purely from a few demonstrations in the prompt, without updating their weights. But how do they actually do this?

There are two competing hypotheses:

  1. The Parametric Approach: The Transformer uses the context to infer the underlying "rules" or hidden factors (latent variables) of the task, and then applies those rules to the new query.
  2. The Non-Parametric Approach: The Transformer bypasses rule-learning entirely, relying on shallow statistical shortcuts and direct pattern-matching between the query and the demonstrations (similar to nearest-neighbor or kernel regression).

Because standard Transformers often suffer from poor out-of-distribution (OOD) generalization, many researchers assumed they were falling into the trap of non-parametric shortcuts. This paper tests a compelling question: If we force a Transformer to explicitly learn the right latent variables, will its OOD performance actually improve?

Methods

To test this, the authors compare two architectural archetypes:

  • Implicit Model: A standard, unmodified Transformer. The query token can directly attend to all context demonstrations, allowing it to potentially exploit non-parametric shortcuts.
  • Explicit Model: A modified architecture that introduces a strict information bottleneck. The context demonstrations are processed by a "Context Transformer" and compressed into a low-dimensional latent vector ($z$). The query token cannot see the context directly; it only has access to this latent vector and must use a downstream "Prediction Function" to generate the output.
Figure 1. We compare the benefits of the implicit (left) and the explicit (right) model. Explicit models disentangle context aggregation and prediction into two separate functions with an inductive bias for inferring generative latent variables in order to solve the task. Implicit models are more expressive, but can learn non-parametric shortcut solutions that bypass latent variable inference.

By stripping away the ability to directly look back at the context, the explicit model is forced to adopt a parametric strategy.

Datasets

The authors evaluated these models across a diverse suite of algorithmic and reasoning tasks that possess well-defined latent structures:

  • Synthetic Regression: Linear regression, non-linear regression (MLP-generated), Sinusoidal functions, Gaussian Processes, and Hodgkin-Huxley neural ODE prediction.
  • Synthetic Classification: Linear and non-linear (MLP) classification.
  • Compositional & Reasoning Tasks: Raven’s Progressive Matrices (a classic visual IQ test format), Alchemy (a meta-reinforcement learning benchmark based on causal graphs), Gene Targeting (a real-world biological dataset mapping gene expressions), and a reusable modular Mixture of Experts (MoE) task.

Key Insights and Ideas

  • Transformers are excellent latent extractors: Through linear probing and counterfactual interventions (using Distributed Alignment Search), the authors proved that the explicit model's bottleneck successfully captures the true latent variables of the data generative process. The "rules" are definitely in there.
  • Knowing the rules isn't enough: Despite successfully isolating the correct latent variables, the explicit model showed no significant performance benefit over the standard implicit model when tested on out-of-distribution data.
  • The flaw lies in the prediction function: Why does explicit latent inference fail to boost generalization? The authors discovered that while the model easily learns to extract the latents, the downstream prediction network struggles to learn how to mathematically utilize them. When the authors replaced the learned prediction function with a hard-coded "oracle" function (manually implementing the true mathematical relationship), OOD performance generalized near-perfectly.
Comparison of implicit and explicit models across synthetic and compositional tasks

Key Results

  • In-Distribution (ID): Both implicit and explicit models perform exceptionally well, with the implicit model maintaining a very slight edge due to its unconstrained expressivity.
  • Out-of-Distribution (OOD): On regression tasks, both models fail to generalize effectively when queries are sampled far outside the training distribution. For classification and compositional tasks, both architectures degrade gracefully but perform remarkably similarly.
  • The Oracle Proof: In a linear regression task where the true prediction function is a simple dot product ($z^T x_*$), hard-coding this operation into the explicit model completely solves the OOD generalization problem, whereas letting the model learn the dot product end-to-end fails.

Limitations and Future Work

While this study provides a clean, controlled look at the mechanics of ICL, it focuses entirely on synthetic, algorithmic, and structured reasoning tasks rather than large-scale natural language processing.

The authors note that their explicit model represents a minimal architectural intervention. Future work should focus on designing better inductive biases or specialized architectures for the prediction function itself, ensuring that once a model captures the underlying rules of a task, it actually knows how to apply them to novel scenarios.

Summary updated

BibTeX
@inproceedings{mittal2025does,
  author = {Sarthak Mittal and Eric Elmoznino and Leo Gagnon and Sangnie Bhardwaj and Tom Marty and Dhanya Sridhar and Guillaume Lajoie},
  title = {{Does learning the right latent variables necessarily improve in-context learning?}},
  booktitle = {International Conference on Machine Learning},
  year = {2025},
  url = {https://openreview.net/forum?id=tuYmhlu62K},
}
Figure