NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Transformer Learns Optimal Variable Selection in Group-Sparse Classification

Chenyang Zhang, Xuran Meng, Yuan Cao

Abstract

While Transformer architectures have achieved state-of-the-art performance across diverse domains, the theoretical mechanisms enabling their success—particularly how they identify relevant information within high-dimensional data—remain partially understood. This paper investigates the training dynamics of a one-layer self-attention Transformer on a group-sparse classification task. We prove that gradient descent, starting from zero initialization, enables the model to perform optimal variable selection by concentrating attention on label-relevant features while suppressing noise. Our analysis demonstrates that Transformers achieve superior generalization bounds compared to traditional linear models and exhibit significant efficiency in transfer learning settings.

Introduction

The "black box" nature of Transformers poses a significant challenge for theoretical machine learning. A central question is how the self-attention mechanism learns to prioritize specific parts of an input. This study addresses this by examining group sparsity, a statistical scenario where an input is composed of multiple groups of variables, but only a single group determines the target label. In such settings, a model must effectively filter out irrelevant "noise" groups to generalize well. We provide a rigorous mathematical foundation showing that Transformers are not merely pattern matchers but are capable of learning hard feature selectors through standard training procedures.

Problem Formulation: Group-Sparse Classification

In the group-sparse setting, an input $X$ is partitioned into $D$ groups, each of dimension $d$. Only one specific group $j^*$ contains the signal necessary to determine the classification label $y$, while the remaining $D-1$ groups consist of Gaussian noise.

A standard linear model, such as logistic regression, treats the entire input as a single concatenated vector. This approach often struggles with high-dimensional noise, leading to a generalization error bound of $O(dD/n)$, where $n$ is the number of samples. The goal of this research is to prove that a Transformer can achieve a much tighter bound by leveraging its attention mechanism to isolate the group $j^*$.

Methodology

The authors analyze a simplified one-layer self-attention Transformer to keep the mathematical dynamics tractable while preserving the core mechanics of the architecture:

  1. Architecture Simplification: The Query and Key matrices are collapsed into a single trainable matrix $W$, and the Value matrix is represented by a trainable vector $v$.
  2. Positional Encodings: Orthogonal positional encodings are utilized to allow the model to distinguish between different input groups.
  3. Training Dynamics: The study focuses on the trajectory of gradient descent on a population cross-entropy loss. Crucially, the analysis starts from zero initialization, avoiding the common theoretical shortcut of assuming a favorable starting point.
  4. Transfer Learning: The framework is extended to evaluate how a model pre-trained on one group-sparse task performs when fine-tuned on a downstream task sharing the same sparsity structure.

Key Theoretical Insights

Attention as an Optimal Filter

The paper proves that during the training process, the attention weights for the label-relevant group $j^*$ converge toward 1, while the weights for the $D-1$ noise groups vanish toward 0. This confirms that the Transformer learns a "hard" selection mechanism. Interestingly, the value vector $v$ aligns with the ground truth signal early in the training process, even before the attention mechanism has fully isolated the correct group. Once the attention "locks in," the loss decreases precipitously.

Superiority Over Linear Baselines

A pivotal result of the paper is the comparison of generalization bounds. The Transformer achieves a bound of $O((d+D)/n)$. Compared to the linear baseline of $O(dD/n)$, the Transformer's error scales additively rather than multiplicatively with respect to the number of groups and their dimensions. This explains why Transformers are significantly more robust to high-dimensional structured noise than flatter architectures.

The Role of Positional Encodings

The theory reveals a sophisticated use of positional encodings: the model uses them to compute the attention scores (deciding where to look), but effectively filters them out of the final output. This ensures that the positional information does not contaminate the classification signal derived from the features themselves.

Empirical Validation

To complement the theoretical proofs, the authors conducted experiments using both synthetic Gaussian data and a modified version of the CIFAR-10 dataset. In the visual experiment, a single $32 \times 32$ image patch was embedded within a $7 \times 7$ grid, with the other 48 patches filled with Gaussian noise.

Consistent with the theory, the Transformer's attention heatmaps showed near-exclusive focus on the single relevant image patch. Furthermore, in transfer learning tests, the pre-trained Transformer reached high accuracy on new tasks with significantly fewer samples than a model trained from scratch, demonstrating that the learned variable-selection mechanism is highly transferable.

Conclusion and Future Work

This research provides a rigorous proof that Transformers are naturally suited for group-sparse data, performing optimal variable selection through gradient descent. While the study is limited to a single-layer model without multi-head attention or feed-forward networks, it offers a vital "look under the hood" at the fundamental mechanics of self-attention. Future work will aim to extend these findings to multi-layer architectures and explore how these selection mechanisms interact with other common components like normalization layers and residual connections.

Summary updated

BibTeX
@inproceedings{zhang2025transformer,
  author = {Chenyang Zhang and Xuran Meng and Yuan Cao},
  title = {{Transformer Learns Optimal Variable Selection in Group-Sparse Classification}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=fuoM5YDBX4},
}
Figure