Attention Visualization › Visualizing Attention Patterns

Michael BrenndoerferFebruary 11, 20266 min read
Language AI HandbookAll chapters
Part 59 of 67Interpretability
Chapter 447Attention Visualization › Visualizing Attention Patterns
Reading section 4 of 15 · Visualizing Attention Patterns · 6 minSection 4 of 15 · 6 min

Book contents

Section 4 of 15 · 6 min read

Attention Visualization › Visualizing Attention PatternsOne section · Sections in this chapter4 / 15 ↓
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

Raw numbers are difficult to interpret. Heatmap visualizations of the attention matrix make patterns visible at a glance, and the choice of visualization format significantly affects what patterns you notice and what you miss.

Single-Head Heatmap

The most common visualization maps the attention matrix to a color grid where rows correspond to query tokens and columns to key tokens. Cell (i,j)(i, j) is colored proportionally to the attention weight that token ii places on token jj. Reading across a row tells you the full attention distribution for a single query token: where does this token "look" when updating its representation? Reading down a column tells you which queries attend heavily to this key token: which tokens find this position useful?

Out[7]:
Visualization
Heatmap of attention weights where rows are query tokens and columns are key tokens with varying shades
Attention weight heatmap for BERT layer 6, head 1 (zero-indexed layer 5, head 0), processing an ambiguous sentence about 'bank'. Each row shows the attention distribution for one token across all sequence positions. Darker cells indicate stronger attention, with most rows concentrated on a small subset of positions rather than spread evenly across all tokens.

Patterns often visible in these heatmaps include diagonal bands (each token attending mostly to itself), strong attention toward the [CLS] token or punctuation tokens, and occasional long-range connections between semantically related tokens. The fact that certain patterns recur across different sentences is evidence that a head has learned a general function rather than fitting a single example.

Choosing Color Scales

The color scale in an attention heatmap matters more than it might seem. By default, matplotlib normalizes the colormap to span the range of values present in the matrix. If one cell has an attention weight of 0.9 and all others are below 0.1, the single dominant cell will appear very dark and everything else will appear nearly identical shades of light blue. This can make diffuse patterns look more uniform than they are.

Two useful alternatives are: (1) fixing the color scale from 0 to 1 (vmin=0, vmax=1), which puts all heads on a common scale and makes sparse versus dense patterns visually distinct, and (2) using a logarithmic color scale, which amplifies small differences in low-weight cells and can reveal secondary patterns that would otherwise be washed out. Each choice tells a different story, so it is worth producing both when exploring a new head.

Comparing Multiple Heads

Because each head learns an independent set of query, key, and value projections, different heads in the same layer often develop distinct attention patterns. Visualizing multiple heads side by side reveals this specialization and demonstrates that the layer is computing multiple things simultaneously.

In[8]:
Code
# Extract four heads from layer 6 (zero-indexed layer 5) for comparison
layer_idx = 5
heads_to_show = 4  # Show first 4 heads

layer_attns = [
    attention_weights[layer_idx][0, h].numpy() for h in range(heads_to_show)
]
Out[9]:
Visualization
Heatmap of attention weights for head 1 of BERT layer 6, with a partial diagonal and strong final SEP column
Layer 6, Head 1. Attention follows a partial diagonal across neighboring content tokens and also concentrates on the final `[SEP]` position.
Heatmap of attention weights for head 2 of BERT layer 6, dominated by attention to SEP
Layer 6, Head 2. Most query positions direct their strongest attention to `[SEP]`, with comparatively little weight elsewhere.
Out[10]:
Visualization
Heatmap of attention weights for head 3 of BERT layer 6, with diffuse content attention and a strong SEP column
Layer 6, Head 3. Attention is more diffuse across content tokens while retaining a strong column at `[SEP]`.
Heatmap of attention weights for head 4 of BERT layer 6, combining SEP attention with localized token connections
Layer 6, Head 4. Attention combines a prominent `[SEP]` column with several localized content-token connections.

Looking across heads, you can see that attention is not monolithic. Some heads produce sharp, focused patterns while others spread attention diffusely. This diversity is intentional: the multi-head mechanism exists precisely to allow different heads to capture different types of relationships simultaneously. If all heads learned the same pattern, most of the model's parameter budget for attention would be wasted. The fact that heads specialize is evidence that the training objective is pushing each head toward a distinct functional niche, even though no explicit diversity loss is imposed during training.

Aggregating Attention Across Heads

A tempting operation is to average all attention matrices in a layer into a single summary heatmap. This produces a view of "what the layer attends to on average." However, averaged attention is almost always more diffuse than any individual head and often has lower information content than the most specialized heads. The averaging smears together patterns that operate at very different scales and for different purposes.

A better aggregation strategy for identifying important tokens is to take the maximum attention weight received by each key token across all heads. This shows you which tokens any head found relevant, rather than which tokens all heads found moderately relevant. For understanding model structure, examining individual heads and comparing them directly produces deeper insight than any aggregate.