Skip to content

Attention map

Tap a word and see where it looks, with the attention arithmetic laid open.

What this lab shows

In a Transformer, each word asks every other word how much it matters to it. Here you see that question in miniature: short sentences, two attention heads and the arithmetic laid open, from the query Q and the key K to the softmax and the mix of the values V.

Attributes and matrices written by hand to show the mechanism. The arithmetic is that of attention; the numbers of a real model come from training.

How to use it

  1. Pick a sentence and tap a word in the left column.
  2. Switch between the meaning head and the position head and compare the links.
  3. Open the five steps, turn off the division by √dk and watch the weights get more extreme.
LabAttention mapTry it

Illustrative dataThe attributes and matrices were written by hand to show the mechanism. The arithmetic (Q, K, V, softmax and weighted sum) is the real one; in a real model, the numbers come from training and have no names.

the cat drank the milk

Attention weights of «drank» (Head 1: meaning)

  • the7%
  • cat39%
  • drank7%
  • the7%
  • milk39%

The verb «drank» splits its attention between who drank and what was drunk. Swap «milk» for «quickly» and watch the attention on the last word drop.

What to notice: The same word, with the same arithmetic, looks at different places in different sentences, and its output changes too. This is how context gets into meaning.

Books that use this lab

No published book uses this lab yet.