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

Sarthak Mittal, Eric Elmoznino, Léo Gagnon, Sangnie Bhardwaj, Dhanya Sridhar, Guillaume Lajoie · OpenReview

TL;DR

This paper investigates whether explicitly inferring task-specific latent variables improves out-of-distribution generalization in Transformer models performing in-context learning (ICL). The authors found that while their modified "explicit" model successfully extracts these latent variables, it doesn't lead to better generalization compared to standard "implicit" Transformers, suggesting limitations in how models utilize learned latents for prediction.

What Problem Does This Paper Address?

Large language models (LLMs) have shown remarkable ability to perform new tasks with just a few examples provided in the input, a phenomenon known as in-context learning (ICL). A key question is how they achieve this. One hypothesis is that for many tasks, the underlying data generation process can be explained by a few "latent variables" (like the coefficients in linear regression). An ideal ICL model would infer these latents from the context and then use them to predict new outputs. However, it's unclear if Transformers actually do this, or if they rely on simpler "shortcuts" (like direct pattern matching) enabled by their attention mechanisms. This paper aims to resolve this by directly testing whether explicitly forcing Transformers to infer these latent variables improves their ability to generalize to unseen data distributions (out-of-distribution or OOD generalization).

Methods

The core of the paper is a comparison between two model architectures:

  • Implicit Model: This is the standard Transformer architecture used for ICL. It processes the context and query together, allowing for direct attention between any tokens.
  • Explicit Model: This is a modified Transformer. It introduces a "bottleneck" between processing the context and generating the prediction for the query. This bottleneck is designed to force the model to summarize the context into a lower-dimensional representation (the inferred latent variables) before making a prediction. This architecture explicitly separates "context aggregation" (inferring latents) from "predictive modeling" (using latents to predict).

The authors also explored variations, including using an auxiliary loss to encourage the explicit model to predict the true latent variables and using an "oracle" (ground-truth) prediction function to isolate the impact of latent inference versus prediction.

Datasets

The authors evaluated their models on a variety of tasks, including:

  • Synthetic Regression Tasks: Linear regression, nonlinear regression (using MLPs), and sinusoidal regression.
  • Synthetic Classification Tasks: Linear and nonlinear (MLP) classification.
  • Compositional Tasks: Raven's Progressive Matrices (Raven's PM), Alchemy, and Gene Targeting.
  • Reusable Modular Mixture of Experts (MoE): A task involving sequential application of experts.
  • Hodgkin-Huxley ODE Prediction: A task based on simulating neural activity dynamics.

These tasks were chosen because their underlying latent variable structure is well-understood, allowing for a direct assessment of whether the models are inferring the correct latents.

Key Insights and Ideas

  • Latent Inference ≠ Generalization: The most surprising finding is that explicitly forcing a Transformer to infer task-specific latent variables (via a bottleneck) does not improve its out-of-distribution generalization performance. This challenges the intuition that learning the "right" underlying structure is the key to robust ICL.
  • Bottleneck Learns Latents, But Prediction Fails: The "explicit" model successfully extracts meaningful latent variables from the context, as evidenced by interpretability analyses. However, the downstream prediction mechanism struggles to effectively utilize these latents for accurate predictions, especially when faced with OOD queries.
  • Prediction Function is the Bottleneck: When the prediction function is provided as an "oracle" (i.e., the ground-truth function), the explicit model does generalize well. This strongly suggests that the failure lies not in latent inference, but in the model's ability to learn a generalizable prediction function that can leverage those latents.
  • Implicit Models Aren't Necessarily "Lazy": The paper provides evidence that implicit Transformers might not solely rely on shallow shortcuts. They can potentially learn structured solutions, but perhaps in a more distributed manner across layers, or they might be better at learning the prediction function itself.
  • Interpretability Benefit: While not improving performance, the explicit model's bottleneck architecture offers significant interpretability benefits, allowing for clearer decoding and manipulation of learned latent variables.

Key Results

  • No OOD Improvement: Across various synthetic and compositional tasks, the explicit model showed no significant performance gains over the implicit Transformer on out-of-distribution generalization. In some cases, the implicit model even performed slightly better.
  • Latent Extraction Confirmed: The explicit model's bottleneck was shown to successfully capture the true latent variables, as confirmed by linear decoding and counterfactual intervention experiments.
  • Oracle Prediction Boosts Generalization: When the explicit model was paired with a known, ground-truth prediction function, it achieved substantial OOD generalization improvements, highlighting the importance of the prediction mechanism.
  • Classification vs. Regression: Classification tasks showed stronger OOD performance across the board, likely because decision boundaries are less prone to shifting than regression functions.

Limitations and Future Work

  • Architectural Limitations: The authors suggest that the limitations might be more fundamental to the Transformer architecture itself, rather than solely due to shortcut learning. The ability to learn a generalizable prediction function that leverages inferred latents appears to be a key challenge.
  • Future Work: The paper calls for further research into designing more effective prediction functions that can properly utilize learned latent variables. It also suggests exploring other inductive biases beyond the bottleneck to encourage structured learning and generalization. The interpretability benefits of the explicit model are highlighted as a promising avenue for future research in understanding and debugging AI systems.

Summary updated

BibTeX
@inproceedings{mittal2025does,
  author = {Sarthak Mittal and Eric Elmoznino and Léo Gagnon and Sangnie Bhardwaj 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