A matrix encoding how much each sequence element should attend to every other.
The attention matrix is a core computational structure within attention mechanisms, representing the pairwise relevance scores between all elements in a sequence. For a sequence of length n, the attention matrix is an n × n grid where each entry quantifies how strongly one position should "attend to" another when producing a representation. These scores are derived by computing dot products between query and key vectors — learned projections of the input — and then applying a softmax normalization so that each row sums to one, yielding a valid probability distribution over positions. The resulting weights are used to compute a weighted sum of value vectors, producing context-aware representations that reflect the most relevant parts of the input.
The attention matrix is what enables transformer-based models to capture long-range dependencies without the sequential bottlenecks of recurrent architectures. Because every position can directly attend to every other position in a single operation, the model can relate distant tokens — such as a pronoun and its antecedent several sentences apart — far more efficiently than RNNs or LSTMs. In multi-head attention, multiple attention matrices are computed in parallel using different learned projections, allowing the model to simultaneously capture different types of relationships (syntactic, semantic, positional) across the same input.
In practice, the attention matrix has become an important tool for interpretability as well as performance. Researchers often visualize attention weights to understand which input tokens a model focuses on when generating a particular output, offering partial insight into model reasoning. However, attention weights are not always reliable proxies for importance, and their interpretation requires care. Techniques like attention rollout and gradient-weighted attention have been developed to produce more faithful explanations.
The attention matrix scales quadratically with sequence length — an n-token sequence requires an n × n matrix — which becomes a significant computational and memory bottleneck for long documents. This limitation has motivated a wave of efficient attention variants, including sparse attention, linear attention, and sliding-window approaches, all of which approximate or restructure the full attention matrix to reduce cost while preserving most of its expressive power.