Transformers possess a remarkable ability known as In-Context Learning (ICL): they can solve new tasks simply by observing a few examples in a prompt, without any weight updates. While researchers have theoretically shown that Transformers can implement algorithms like Gradient Descent (GD) to solve linear regression, these studies usually rely on hand-crafted parameters rather than examining how models actually behave after training.
Furthermore, standard Gradient Descent isn't particularly efficient for sparse problems—where only a few features actually matter. This raises an important question: How does a trained multi-head Transformer utilize its architecture to handle sparsity? Does it just execute basic GD, or has it "learned" a more sophisticated internal algorithm?
This paper reveals that trained Transformers solve sparse linear regression through a learned "preprocess-then-optimize" strategy. The first layer uses multi-head attention to identify and reweight important features, while subsequent layers use a single dominant head to perform iterative optimization on that refined data.
A Case Study: Sparse Linear Regression
To peek under the hood, the authors investigate these questions using a case study on Sparse Linear Regression. They train encoder-based Transformers on synthetic data where the target $y$ depends on only a small subset of input features $x$.
The study relies on synthetic datasets generated from noisy sparse linear models:
- Input ($x$): Sampled from a Gaussian distribution $N(0, \Sigma)$.
- Ground Truth ($w^*$): A sparse vector where only $s$ out of $d$ dimensions are non-zero.
- Labels ($y$): Generated as $y = \langle w^*, x \rangle + \text{noise}$.
The authors employ several clever diagnostic techniques to understand the model's internal workings, including systematically masking individual attention heads (Heads Assessment), creating pruned models to test head redundancy (Pruning & Probing), and extracting hidden representations to see if the first layer simplifies the data for standard GD (P-Probing).
The "Preprocess-Then-Optimize" Mechanism
The most striking finding is that the Transformer doesn't treat every layer the same. There is a distinct division of labor across the network:
- Multi-Head is for Preprocessing (Layer 1): The first layer acts as a feature selector. Here, all attention heads are significant. Each head likely focuses on different subspaces of the features to estimate their correlation with the target. This explains why multi-head models significantly outperform single-head models on sparse tasks.
- Single-Head is for Optimization (Layers 2+): In subsequent layers, the model acts as an optimizer and typically relies on just one dominant head. Once the features are "cleaned up" and reweighted by the first layer, a single head is sufficient to perform the iterative steps of an optimization algorithm akin to Gradient Descent.
By reweighting features based on their importance, the Transformer implements an algorithm that achieves lower "excess risk" than standard Ridge regression or Ordinary Least Squares (OLS), especially in high-noise or sparse scenarios.
Experimental and Theoretical Validation
Experimental results strongly support this functional split.
As seen in the Heads Assessment heatmaps, head importance is distributed relatively evenly in Layer 1, but in subsequent layers, a single head accounts for nearly all the weight. Furthermore, a "pruned" model—forced to have only one head in layers 2 and beyond—performed almost identically to the full multi-head model. This confirms that extra heads in later layers are largely redundant for this specific task.
Beyond empirical results, the authors provide mathematical proofs showing that a Transformer can implement a specific correlation-based preprocessing algorithm followed by multi-step GD. This proposed internal algorithm achieves a better error bound than standard GD on raw inputs, theoretically validating why the Transformer "prefers" this two-phase approach.
Limitations and Future Directions
While this study provides a fascinating glimpse into the mechanics of ICL, it is limited to sparse linear regression. Although this is a standard "fruit fly" task for ICL research, it remains to be seen if these exact patterns hold for more complex, non-linear, or real-world natural language tasks. Additionally, the theoretical analysis focuses on "linear-attention-only" Transformers, omitting non-linear MLP blocks to make the math tractable.
Finally, while the paper explains what the trained model does, it leaves open how the training process (SGD) leads the model to discover this specific strategy. Investigating these training dynamics remains a key area for future research.
Summary updated