For a long time, the empirical success of Transformers in natural language processing and computer vision outpaced our theoretical understanding of their power. While we knew they worked, the mathematical proof that they could approximate any sequence-to-sequence function was a missing link. Recent research has crystallized a core intuition: Transformers are universal approximators because their attention mechanism acts as a "contextual mapper," ensuring that every token in a sequence receives a unique signature.
The Problem of Sequence Ambiguity
In a standard feedforward neural network, a specific input vector always produces the same output. However, sequences are more complex. The word "bank" in "river bank" requires a different representation than "bank" in "bank account." If a model processed these tokens in isolation, it would be unable to distinguish between them.
To approximate a sequence-to-sequence function, a model must resolve this ambiguity. As Chulhee Yun, Srinadh Bhojanapalli, and colleagues first proved in Are Transformers universal approximators of sequence-to-sequence functions?, Transformers solve this by using self-attention to generate unique contextual signatures. By incorporating information from the entire sequence, the model ensures that identical tokens in different contexts receive distinct representations.
Attention as a Contextual Mapper
The "why" behind Transformer expressivity lies in a division of labor between the attention layers and the position-wise feedforward networks (MLPs).
- Attention does not necessarily do the heavy lifting of function approximation itself. Instead, its primary role is token distinguishability. It maps identical or similar input tokens to unique points in a high-dimensional space based on their surroundings.
- MLPs then act as the universal approximators for these now-unique vectors. Since the attention layer has ensured that no two tokens (in their specific contexts) "overlap," the MLP can map each unique vector to its desired output.
This framework, formalized by Cheng Jingpu, Qianxiao Li, and others in A unified framework for establishing the universal approximation of transformer-type architectures, suggests that almost any Transformer variant—whether sparse, kernel-based, or symmetric—is a universal approximator as long as it maintains this "distinguishability" property.
The Surprising Power of a Single Layer
Early proofs suggested that Transformers needed to be excessively deep to achieve universal approximation. However, Tokio Kajitsuka and Issei Sato demonstrated in Are Transformers with One Layer Self-Attention Using Low-Rank Weight Matrices Universal Approximators? that even a single layer of self-attention is sufficient.
The secret weapon is the Softmax function. Unlike a "Hardmax" (which only picks the most relevant token), Softmax preserves relative distances between all elements in a sequence. This allows a single attention head to aggregate global sequence information into a unique "ID" for each token. Even with low-rank weight matrices, the Softmax operator can prevent distinct input representations from collapsing into identical vectors, preserving the information necessary for the subsequent MLP to function.
Memorization and Efficiency
This ability to create unique contextual IDs also explains why Transformers are so efficient at memorization. In Provable Memorization Capacity of Transformers, Junghwan Kim, Michelle Kim, and Barzan Mozafari showed that Transformers can "hard-code" massive amounts of data using far fewer parameters than the total number of tokens. By exploiting sequence structure to create unique identifiers, the model can resolve ambiguities in structured data, allowing it to memorize $N$ sequences of length $n$ with only $\tilde{O}(d + n + \sqrt{nN})$ parameters.
Beyond Text: Visual and Prompt Tuning
The principle of token distinguishability extends to other domains:
- Visual Autoregressive (VAR) Transformers: Yifang Chen and colleagues proved in Fundamental Limits of Visual Autoregressive Transformers: Universal Approximation Abilities that single-head attention can distinguish between identical image patches by incorporating spatial information from the surrounding image pyramid.
- Prompt Tuning: Even when the core Transformer is frozen, Jerry Yao-Chieh Hu and Zhao Song showed in Fundamental Limits of Prompt Tuning Transformers: Universality, Capacity and Efficiency that a single layer with a learned prompt can serve as a universal approximator. The prompt-induced keys and queries provide the necessary context to differentiate tokens, enabling the model to approximate any sequence function.
In summary, the Transformer's power doesn't come from a single complex operation, but from the attention mechanism's ability to ensure that every token is seen as a unique entity within its environment. Once tokens are distinguishable, the rest of the network can treat sequence processing as a massive, solvable coordinate transformation.