Self-Attention Concept › Visualizing Self-Attention

Michael BrenndoerferUpdated February 6, 20263 min read
Language AI HandbookAll chapters
Part 13 of 67Self-Attention
Chapter 90Self-Attention Concept › Visualizing Self-Attention
Reading section 6 of 13 · Visualizing Self-Attention · 3 minSection 6 of 13 · 3 min

Book contents

Section 6 of 13 · 3 min read

Self-Attention Concept › Visualizing Self-AttentionOne section · Sections in this chapter6 / 13 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

A heatmap provides an intuitive view of self-attention patterns. Rows represent query positions (which token is gathering information), and columns represent key positions (which tokens are being attended to). Darker cells indicate stronger attention.

Out[10]:
Visualization
Heatmap showing attention weights between four positions, with strong diagonal weights near 1.0 and off-diagonal weights near zero.
Self-attention weight heatmap for a four-token sequence with random embeddings. Each row shows how one token distributes attention across all positions, with darker colors indicating stronger attention. Even with random embeddings, the softmax normalization ensures each row sums to 1, producing a valid probability distribution over positions.

In this random example, we see some diagonal emphasis where tokens attend to themselves, plus distributed attention to other positions. In trained models, these patterns become meaningful: related words attend strongly to each other, syntactic structures emerge, and semantic relationships become visible.

Researchers have spent considerable effort interpreting what trained attention patterns mean. In models like BERT, some attention heads learn to attend to the next token, some to the previous token, some to the sentence separator tokens. Other heads develop more linguistically interpretable patterns: there are heads that attend from verbs to subjects, from pronouns to antecedents, from nouns to modifying adjectives. This does not mean that each head has a single clean function. In practice, the patterns are messy and context-dependent. But the existence of interpretable patterns suggests that self-attention does learn to exploit syntactic and semantic structure.

The diagonal is informative. Random embeddings tend to produce some diagonal emphasis because the dot product of a vector with itself is always larger than with a random other vector. In trained models, the diagonal is often strong, meaning tokens attend significantly to themselves. This makes sense: a token's own embedding contains the most concentrated information about that token. The off-diagonal weights then represent how much additional context each token gathers from its neighbors.