Attention map

Activations · lens

A grid for one attention head: rows are the tokens doing the looking, columns the tokens being looked at, each cell how much weight one gives the other.

Also called Attention heat map, Attention weights, Attention pattern

An attention map for one head: rows are the tokens doing the looking (queries), columns the tokens looked at (keys). Each row is a softmax, so it adds up to 100%. The five heads are hand-set to imitate patterns researchers found in trained language models; the numbers are not read from a model. Pick a cell, or focus the map and use the arrow keys.
[CLS]thefoxsawthehenanditran[SEP][CLS]thefoxsawthehenanditran[SEP]

“it” (row 8) gives

[CLS]1%
the1%
fox89%
saw1%
the1%
hen4%
and1%
it2%
ran1%
[SEP]1%

“it” looks at “fox”; the other tokens spread out. One bright cell carries the meaning.

A bright cell says where a head looked, not what it took from there or whether the answer depended on it: the separator head puts most of its weight on a token that carries no meaning. A real model has many heads in every layer, and reading one map at a time is a small window on all of them.

Figure 1. Every number is computed in your browser.

What it shows

In a transformer every token decides, head by head, how much to take from every other token. Those weights are a softmax per query, so each row adds up to one, and for a sentence of n tokens they fill an n × n grid. Drawing it shows what a head pays attention to: the previous word, a separator, the noun a pronoun refers to. Text generators hide later tokens, which makes the map triangular.

How to read it

Row
The query token: the one gathering information.
Column
The key token: the one being read from.
Bright cell
A large share of that row’s attention. Each row adds up to 100%.
Diagonal stripe
A positional head: each token looks at itself or a neighbour.
Bright column
A token everyone looks at, often a separator used as a resting place.

What it does not tell you

Attention weight is not importance: a head can attend strongly to a token and pass on almost nothing, and studies have found attention maps that can be changed without changing the answer. One head in one layer is a small part of the model; real models have dozens of heads per layer. The map shows where information could flow, not what was used.

Exploring what transformer heads do, finding positional or syntactic heads, debugging tokenization, and illustrating how self-attention works.

In WeightSpace

Sixteen models in WeightSpace contain attention, among them the emotion, retrieval and question-answering text models and the vision transformers. None of their workbenches draws attention maps yet: the figure uses five hand-set heads that imitate patterns reported in trained models, and says so.

Where to see it

No workbench draws this yet: the figure at the top of this page is the only place in WeightSpace for now.

Model structures: Name the emotion in a sentence, Find the most relevant answer, Find an answer in a passage, Fill a gap in Chinese, Translate a Chinese sentence, Estimate depth from a photo