Transformer learns the cross-task prior and regularization for in-context learning

Fei Lu, Yue Yu · DOI

TL;DR

This paper demonstrates that Transformers don't just "pattern match" during in-context learning; they actually perform sophisticated statistical inference. By studying Inverse Linear Regression (ILR) in underdetermined settings, the authors show that a linear Transformer implicitly learns both the prior distribution of tasks and an optimal regularization strategy, allowing it to solve ill-posed inverse problems better than traditional ridge regression.

What Problem Does This Paper Address?

In-context learning (ICL) is the "magic" that allows Large Language Models to solve new tasks using only a few examples in a prompt. While we know Transformers are great at forward prediction (predicting $y$ given $x$), we understand less about their ability to solve inverse problems—inferring the underlying rule or "weight vector" $w$ that generated the data.

The authors focus on a particularly difficult scenario: rank-deficient inverse problems. This happens when the number of examples in the context ($n$) is smaller than the number of unknowns in the weight vector ($d$). In these cases, the problem is "ill-posed," meaning there are infinitely many possible solutions. To find the right one, a model needs a "prior" (knowledge of what typical solutions look like) and "regularization" (a way to penalize unlikely solutions). The researchers wanted to see if a Transformer could learn these statistical tools purely from data.

Methods

The researchers designed a Linear Transformer specifically for Inverse Linear Regression (ILR).

  • The Architecture: It consists of $L$ layers. The first $L-1$ layers use linear self-attention to process the context $(X, Y)$, and the final layer performs the actual inversion to estimate the weight vector $\hat{w}$.
  • The "Linear" Catch: Despite the name, the mapping is highly non-linear due to layer normalization and the multi-layer composition. This non-linearity is essential for performing effective regularization.
  • Benchmarks: They compared the Transformer against three standard statistical estimators:
  • Ridge Regression (RE): A standard baseline that regularizes each task independently.
  • Two-stage Ridge Estimator (TRE): A more advanced baseline that tries to estimate a prior from the training data first.
  • Oracle Ridge Estimator (ORE): The theoretical "gold standard" that has perfect knowledge of the true prior and noise levels.

Datasets

The study uses synthetically generated datasets where the ground-truth properties are strictly controlled:

  • Weight Vectors ($w$): Drawn from a Gaussian prior with a specific mean and a low-rank covariance matrix.
  • Task Dimensionality ($r_w$): The "true" number of degrees of freedom in the weights, kept smaller than the context length ($n$).
  • Inputs ($X$): Drawn from a Gaussian distribution with varying "condition numbers" (a measure of how stable the data is).
  • Noise: Controlled levels of Gaussian noise added to the outputs.

Key Insights and Ideas

  • Implicit Prior Recovery: The Transformer successfully "internalizes" the distribution of tasks it was trained on. By analyzing the Transformer’s outputs, the authors could actually reconstruct the mean and covariance of the hidden prior.
  • End-to-End Regularization: Unlike traditional methods that require manual tuning of a regularization parameter ($\lambda$), the Transformer learns a data-dependent regularization strategy that is baked into its weights.
  • The Necessity of Low-Dimensionality: A critical finding is that ICL for inverse problems only works well if the task's intrinsic dimensionality ($r_w$) is lower than the context length ($n$). If the task is too complex for the given context, even an "Oracle" model fails.
  • Knowledge Extraction: This suggests that the "knowledge" in a Transformer isn't just a collection of facts, but a learned statistical framework for interpreting new, ambiguous data.

Key Results

  • Outperforming Baselines: The Transformer significantly outperformed standard Ridge Regression and even the Two-stage Ridge Estimator, nearly matching the performance of the theoretically optimal Oracle.
  • Scaling Laws: The estimation error of the Transformer follows predictable "scaling laws." The error increases linearly with:
  • The noise level in the data.
  • The ratio of task dimension to context length ($r_w/n$).
  • The condition number of the input data (how "messy" the inputs are).
  • Convergence: As the number of training tasks increases, the Transformer's learned regularization becomes increasingly refined, eventually hitting the performance ceiling set by the Oracle.

Limitations and Future Work

  • Linearity Constraints: The study focuses on linear transformers and linear regression. Real-world tasks are often non-linear, and it remains to be seen how these insights translate to standard Softmax-based Transformers.
  • Data Requirements: The "meta-learning" process requires a large number of training tasks to learn the prior effectively, which might not be available in all domains.
  • Theoretical Gap: While the numerical results are strong and align with the Oracle, a rigorous mathematical proof for why the Transformer architecture converges to this specific optimal estimator is still an open challenge.
  • Future Directions: The authors suggest applying this paradigm to more complex scientific problems, such as learning the underlying physics of a system (partial differential equations) from sparse observations.

Summary updated

BibTeX
@misc{lu2025transformer,
  author = {Fei Lu and Yue Yu},
  title = {{Transformer learns the cross-task prior and regularization for in-context learning}},
  year = {2025},
  eprint = {2505.12138},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2505.12138},
}
Figure