Mapping of attention mechanisms to a generalized Potts model

Riccardo Rende, Federica Gerace, Alessandro Laio, Sebastian Goldt · DOI

Abstract: Transformers are neural networks that revolutionized natural language processing and machine learning. They process sequences of inputs, like words, using a mechanism called self-attention, which is trained via masked language modeling (MLM). In MLM, a word is randomly masked in an input sequence, and the network is trained to predict the missing word. Despite the practical success of transformers, it remains unclear what type of data distribution self-attention can learn efficiently. Here, we show analytically that if one decouples the treatment of word positions and embeddings, a single layer of self-attention learns the conditionals of a generalized Potts model with interactions between sites and Potts colors. Moreover, we show that training this neural network is exactly equivalent to solving the inverse Potts problem by the so-called pseudo-likelihood method, well known in statistical physics. Using this mapping, we compute the generalization error of self-attention in a model scenario analytically using the replica method.

Introduction

Transformers have revolutionized artificial intelligence, yet they are often treated as "black boxes." We still lack a fundamental understanding of the statistical structures these models actually learn. Most theoretical work treats neural networks as functions of independent and identically distributed (i.i.d.) random variables. However, real-world data—such as natural language or protein sequences—are highly structured and interdependent. This research provides a rigorous theoretical bridge between Transformer architectures and the well-understood world of statistical physics by mapping self-attention to the Potts model, a classic framework for interacting particles.

Factored Attention and the Potts Mapping

A central challenge in understanding attention is how it mixes information about a token's position and its content (embedding). Standard "vanilla" attention often conflates these representations, making it difficult to learn simple pairwise interactions. To address this, the researchers introduced factored self-attention, which decouples positions and embeddings.

Figure 1: Comparison of Vanilla and Factored Attention mechanisms, showing the mathematical mapping of the factored version to a Potts model Hamiltonian.

In physics, a Potts model describes "spins" (analogous to tokens) that interact based on their identity and position. The study proves that a single layer of factored self-attention is mathematically equivalent to a generalized Potts model. Specifically, training this layer via Masked Language Modeling (MLM)—the standard training heuristic for models like BERT—is exactly equivalent to solving the "inverse Potts problem" using the pseudo-likelihood method.

Analytical Generalization and Sample Complexity

By establishing this mapping, the authors were able to apply the replica method, a sophisticated tool from the physics of disordered systems, to calculate the model's performance limits analytically. This allows for the prediction of generalization error based on sample complexity ($M/L$, where $M$ is the number of samples and $L$ is the sequence length).

Figure 3: Analytical prediction (Theory) vs. numerical results (Simulations) for test loss as a function of sample complexity $M/L$, highlighting the interpolation peak at $M/L=1$.

The analytical results match numerical simulations almost perfectly. A key finding is the identification of a sharp peak in test loss when the number of samples equals the number of parameters ($M/L = 1$). This "interpolation threshold" is where the model perfectly fits the training data but generalizes poorly. Furthermore, the study shows that the "double descent" phenomenon—where error increases then decreases as more data is added—is driven by the intrinsic noise of the input data itself in self-supervised learning contexts.

Key Insights and Interpretability

  • MLM as Physics Inference: Masked Language Modeling is shown to be a mathematically consistent estimator for the parameters of a physical system, rather than just a clever training heuristic.
  • Optimal Generalization: Factored self-attention reaches theoretical limits for generalization error and converges significantly faster than vanilla multi-layer transformers on structured synthetic tasks.
  • Interpretability: Due to the mapping, the attention weights in a factored model are directly interpretable as the physical interaction strengths between tokens, providing a clear window into what the model has learned.

Conclusion and Future Work

While this mapping currently applies to a single layer of attention capable of learning pairwise interactions, it sets the stage for future theoretical exploration. Extending this work to multiple layers could reveal how Transformers learn higher-order dependencies. Additionally, studying the learning dynamics through the lens of statistical physics and extending the analysis to logistic loss functions remain promising avenues for future research.

Summary updated

BibTeX
@inproceedings{rende2024mapping,
  author = {Riccardo Rende and Federica Gerace and Alessandro Laio and Sebastian Goldt},
  title = {{Mapping of attention mechanisms to a generalized Potts model}},
  booktitle = {Physical},
  year = {2024},
  doi = {10.1103/physrevresearch.6.023057},
}
Figure