Abstract
Transformers are one of the most successful architectures of modern neural networks. At their core there is the so-called attention mechanism, which recently interested the physics community as it can be written as the derivative of an energy function in certain cases: while it is possible to write the cross-attention layer as a modern Hopfield network, the same is not possible for the self-attention, which is used in the GPT architectures and other autoregressive models. In this work we show that it is possible to obtain the self-attention layer as the derivative of local energy terms, which resemble a pseudo-likelihood. We leverage the analogy with pseudo-likelihood to design a recurrent model that can be trained without backpropagation: the dynamics shows transient states that are strongly correlated with both train and test examples. Overall we present a novel framework to interpret self-attention as an attractor network, potentially paving the way for new theoretical approaches inspired from physics to understand transformers.
Introduction
While Transformers dominate modern AI, a first-principles understanding of their success remains elusive. In theoretical physics, Hopfield networks provide a robust framework for memory storage, where memories are represented as "attractors"—stable states that a system naturally evolves toward. Previous research has successfully linked cross-attention to these networks, but self-attention (where a sequence attends to itself) has proven more difficult to describe with a simple energy function because its queries and keys are dynamic.
This paper bridges this gap by proposing a mathematical framework that treats self-attention as a physical system seeking to minimize local energy. By mapping self-attention to the dynamics of a "vector spin" system, the researchers demonstrate how memories can be retrieved as transient states during the system's evolution.
The Bare Self-Attention Model
The authors introduce a "Bare Self-Attention" model built on three conceptual pillars:
- Tokens as Vector Spins: Tokens are treated as $d$-dimensional vector spins on a sphere rather than mere data points.
- Local Energy Functions: The self-attention update rule is derived from a pseudo-likelihood energy function. Each token minimizes its energy based on the "local field" generated by other tokens in the sequence.
- Backpropagation-Free Training: Framing the model as a physical system allows for training without standard backpropagation. Instead, coupling matrices ($J_{ij}$) are trained by minimizing the negative pseudo-likelihood of the training data, effectively teaching the network the statistical structure of the data distribution.
This model is applied recurrently, with the same layer processing the input iteratively until the system evolves toward a stable state.
Results and Transient Dynamics
The framework was evaluated using the MNIST dataset on two tasks: Masked Token Prediction (reconstructing images with 30% missing pixels) and Denoising (removing heavy Gaussian noise).
Key Insights
- Transient Memories: The most significant finding is the non-monotonic behavior of the system. As shown in the MSE plots, the error initially drops as the system moves toward a state resembling the original image (the transient memory). However, if allowed to evolve further (e.g., to $t=100$), the error increases as the system moves toward a blurry global attractor—essentially the average of the training distribution.
- Efficiency: The Bare Self-Attention model performed masked prediction optimally in just one iteration, while denoising required approximately 10 iterations to reach the transient memory state.
- Task-Agnosticism: Unlike standard Transformers that require task-specific fine-tuning, this model was trained once on the data distribution and could handle both denoising and masking "out of the box."
- The Role of the MLP: Comparisons with full Transformer blocks revealed that the Multi-Layer Perceptron (MLP) is vital for modulating signals within patches. Without it, the bare model tends toward uniform patches and struggles with borders.
Discussion and Limitations
While the model provides a compelling physics-based interpretation of self-attention, several limitations remain. The current $N \times N \times d \times d$ coupling tensor is large and difficult to scale to high-resolution images or massive datasets. Additionally, the framework currently assumes a fixed sequence length, making it challenging to apply to variable-length data like text without further development in positional encoding. Future work will focus on incorporating trainable embeddings and MLPs into the pseudo-likelihood training framework while maintaining physical tractability.
Summary updated