[Paper Review] What can a Single Attention Layer Learn? A Study Through the Random Features Lens
This paper studies the representational and generalization properties of a single multi-head attention layer in the random feature setting, where query and key matrices are randomly initialized and frozen, while value matrices are trainable. It shows that such a model can efficiently learn permutation-invariant functions of key vectors with improved sample complexity over standard two-layer networks, especially when query-key matrices have non-zero mean, enabling better approximation of correlation-based functions.
Attention layers -- which map a sequence of inputs to a sequence of outputs -- are core building blocks of the Transformer architecture which has achieved significant breakthroughs in modern artificial intelligence. This paper presents a rigorous theoretical study on the learning and generalization of a single multi-head attention layer, with a sequence of key vectors and a separate query vector as input. We consider the random feature setting where the attention layer has a large number of heads, with randomly sampled frozen query and key matrices, and trainable value matrices. We show that such a random-feature attention layer can express a broad class of target functions that are permutation invariant to the key vectors. We further provide quantitative excess risk bounds for learning these target functions from finite samples, using random feature attention with finitely many heads. Our results feature several implications unique to the attention structure compared with existing random features theory for neural networks, such as (1) Advantages in the sample complexity over standard two-layer random-feature networks; (2) Concrete and natural classes of functions that can be learned efficiently by a random-feature attention layer; and (3) The effect of the sampling distribution of the query-key weight matrix (the product of the query and key matrix), where Gaussian random weights with a non-zero mean result in better sample complexities over the zero-mean counterpart for learning certain natural target functions. Experiments on simulated data corroborate our theoretical findings and further illustrate the interplay between the sample size and the complexity of the target function.
Motivation & Objective
- To understand the function classes that can be learned by a single multi-head attention layer with benign sample complexity.
- To analyze the generalization performance of random-feature attention (RFA) models using finite samples and a large but finite number of heads.
- To investigate the impact of query-key matrix initialization—particularly non-zero mean distributions—on learning efficiency.
- To compare the sample complexity of RFA with standard two-layer random feature networks (RFMLP) and identify scenarios where attention offers advantages.
- To validate theoretical findings through experiments on simulated data, examining the interplay between sample size and target function complexity.
Proposed method
- The study models a single-attention-layer mechanism with a single query vector and N key vectors, using random, frozen query and key matrices and trainable value matrices.
- It introduces a Random Feature Attention (RFA) model where the attention mechanism computes weighted sums of values based on attention scores derived from query-key dot products.
- The paper derives excess risk bounds of the form $\widetilde{\mathcal{O}}(\sqrt{B(f_\star)/n})$, where $B(f_\star)$ measures the complexity of the target function $f_\star$ and $n$ is the sample size.
- It proposes a biased RFA variant (BRFA) where the query-key matrix product has a non-zero mean (e.g., identity matrix), modeling practical attention initialization patterns.
- Theoretical analysis shows that BRFA achieves better sample complexity than standard RFA for functions depending on correlations between query and key vectors.
- Experiments on simulated data compare RFA, BRFA, and RFMLP across varying sequence lengths $N$, input dimensions $d$, and target function complexities, validating theoretical sample complexity trends.
Experimental results
Research questions
- RQ1Can a single multi-head attention layer with random, frozen query and key matrices express a broad class of permutation-invariant target functions?
- RQ2What is the sample complexity of learning such target functions using the RFA model, and how does it compare to standard two-layer random feature networks?
- RQ3How does the initialization distribution of the query-key matrix product (specifically, non-zero mean vs. zero mean) affect the learning efficiency of correlation-based functions?
- RQ4Can the theoretical sample complexity bounds be validated in finite-sample settings with realistic model sizes and target functions?
- RQ5What is the empirical interplay between sequence length, input dimension, and model performance in learning target functions via RFA and BRFA?
Key findings
- The RFA model can express a broad class of permutation-invariant target functions that are averages of functions of two tokens, including polynomial and correlation-based functions.
- The excess risk bound for RFA is $\widetilde{\mathcal{O}}(\sqrt{B(f_\star)/n})$, which depends only on the input dimension and not on the number of key tokens $N$, leading to improved sample complexity over RFMLP.
- For functions depending on correlations between the query and key vectors, the biased RFA (BRFA) model with non-zero mean query-key matrices achieves a sample complexity that is superior to standard RFA, saving a $\text{Poly}(d)$ factor when $d \gg p$.
- Experiments confirm that RFA outperforms RFMLP in learning functions invariant to key permutation, especially as sequence length $N$ increases, validating the theoretical sample complexity advantage.
- BRFA significantly outperforms RFA in learning correlation-based functions, with performance gap increasing as the correlation strength (controlled by $\gamma$) increases, confirming the theoretical benefit of non-zero mean initialization.
- The analysis reveals that BERT's pre-trained query-key matrices are diagonally dominated, suggesting that real-world attention mechanisms may already exploit the inductive bias of non-zero mean initialization, aligning with the paper's theoretical findings.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.