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

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

Large Language Models (LLMs) have demonstrated a remarkable ability to perform In-Context Learning (ICL)—solving novel tasks simply by observing a few examples in a prompt, without any weight updates. However, the underlying mechanisms of ICL remain a subject of intense debate. Do these models perform parametric modeling by inferring the underlying "rules" or latent variables of a task (e.g., the slope of a line in linear regression)? Or do they rely on non-parametric shortcuts, acting essentially as sophisticated nearest-neighbor matching systems between the query and the provided examples?

A common intuition in machine learning is that if we could force Transformers to explicitly find the underlying rule—the task latent—they would generalize much better to novel, out-of-distribution (OOD) scenarios. This paper rigorously tests that intuition by comparing standard Transformers against a modified architecture designed to eliminate statistical shortcuts and force latent variable inference.

Architecting a Bottleneck: Implicit vs. Explicit Models

To investigate whether explicit latent inference improves generalization, the authors compare two primary architectures:

  1. Implicit Model (Standard Transformer): A standard architecture that can attend to all context examples and the query simultaneously. It is completely free to use any statistical shortcuts or heuristics it finds in the attention layers.
  2. Explicit Model (Bottlenecked): This model is split into two distinct parts separated by a strict dimensionality bottleneck.
  3. First, a Context Aggregator processes the in-context examples and compresses them into a single, fixed-size vector ($z$), which represents the inferred "task latent."
    • Second, a Prediction Function (implemented as an MLP or another Transformer) takes this latent $z$ and the new query to make a prediction.
  4. Crucially, the query cannot directly attend to the context examples. The model is forced to summarize the entire task into $z$.
Comparison of Implicit (standard Transformer) and Explicit (bottlenecked) architectures for In-Context Learning.

The researchers also tested "Oracle" versions where the true latent variables or the true mathematical prediction functions were provided. This ablation helps pinpoint exactly where the system breaks down if it fails to generalize.

Experimental Setup

The study evaluated these architectures across a diverse battery of ICL tasks, categorized into:

  • Synthetic Regression: Linear regression, nonlinear (MLP-based) regression, sinusoidal functions, Gaussian Processes, and Hodgkin-Huxley ODEs (modeling neuron activity).
  • Synthetic Classification: Linear and nonlinear decision boundaries.
  • Compositional and Reasoning Tasks: Visual-style IQ tests (Raven’s Progressive Matrices), a meta-RL benchmark involving causal graphs (Alchemy), real-world genetic knockout data (Gene Targeting), and a modular mixture-of-experts task requiring compositional logic.

Key Insights: Interpretability vs. Generalization

The findings of the paper present a surprising dichotomy between interpretability and robust generalization.

The Bottleneck Works for Interpretability

The explicit model successfully learns to extract the "correct" latent variables. The authors demonstrated that they could linearly decode the true task parameters (such as the weights of a linear regression) directly from the bottleneck vector $z$ with high accuracy. Furthermore, the explicit model is highly "steerable." By manually altering the values in the bottleneck (a counterfactual intervention), the authors could predictably change the model's output—a feat that is nearly impossible with the entangled representations of a standard implicit Transformer.

Despite successfully isolating the correct latent variables, the explicit model fails to translate this into better OOD generalization. When the authors replaced the learned prediction head with the actual mathematical formula (the Oracle), the model generalized perfectly. This proves that the failure point isn't the extraction of the latent variable; rather, Transformers and MLPs struggle to learn a robust downstream function that effectively uses those latents when faced with out-of-distribution queries.

No OOD Performance Boost

Contrary to prevailing intuition, forcing the model to learn the "right" rules does not make it more robust. Across almost all tasks—regression, classification, and compositional reasoning—the explicit model showed no significant improvement over the implicit model in OOD settings.

Performance comparison across synthetic and compositional tasks, showing no significant out-of-distribution (OOD) improvement for explicit models.

In fact, in tasks like sinusoid regression, the implicit model actually outperformed the explicit one. The standard Transformer could leverage kernel-like interpolation shortcuts that worked reasonably well, whereas the explicit model's learned prediction function completely fell apart when the query drifted far from the training distribution. In-distribution, the shortcuts found by standard Transformers proved to be highly effective and "benign."

Conclusion and Future Directions

This study highlights a critical intrinsic limitation of current neural architectures: while we can force models to infer the right latent variables (a massive win for interpretability and steerability), this is not sufficient to achieve structured ICL solutions that generalize out-of-distribution. The downstream prediction mechanisms simply do not know how to utilize these latents robustly.

The authors suggest that future work must look beyond just representation learning and focus on:

  1. Designing better inductive biases specifically for the prediction head to ensure it uses latents robustly.
  2. Exploring more sophisticated Bayesian architectures capable of handling the inherent uncertainty of latent inference.
  3. Investigating whether these structural limitations persist in much larger-scale LLMs (billions of parameters), where emergent properties might alter how latent variables are utilized.

Summary updated

BibTeX
@inproceedings{mittal2025does,
  author = {Sarthak Mittal and Eric Elmoznino and Leo Gagnon and Sangnie Bhardwaj and Guillaume Lajoie and Dhanya Sridhar},
  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