Skip to content

Inference EngineeringInteractive lab

Attention heatmap

Type a sentence, see which words attend to which

A full single-head self-attention pass computed in the page: scores, the scale factor, the causal mask and the softmax, drawn as a heatmap you can read row by row.

What this teaches

  • Scaled dot-product attention
  • Causal masking
  • Softmax temperature

The input

11 tokens · d = 32 · √d = 5.657
Neutral
Modifiers

Attention weights

softmax( qᵢ·kⱼ / (√d × T) ) - every row sums to 1
0.001.00Colour intensity is √weight, so a small weight is still visible. The number in each cell is the weight itself.
Attention weights. Each row is one query token and each column one key token; the cell is how much of that query's attention goes to that key. Masked positions are shown as a dash. Choose a row header to see the arithmetic behind it.
query ↓ / key →0the1cat2sat3on4the5mat6because7the8cat9was10tiredΣ
1.00maskedmaskedmaskedmaskedmaskedmaskedmaskedmaskedmaskedmasked1.00
0.001.00maskedmaskedmaskedmaskedmaskedmaskedmaskedmaskedmasked1.00
0.000.060.94maskedmaskedmaskedmaskedmaskedmaskedmaskedmasked1.00
0.010.000.000.98maskedmaskedmaskedmaskedmaskedmaskedmasked1.00
0.500.000.000.010.50maskedmaskedmaskedmaskedmaskedmasked1.00
0.000.110.040.000.000.85maskedmaskedmaskedmaskedmasked1.00
0.010.000.000.000.010.010.98maskedmaskedmaskedmasked1.00
0.330.000.000.000.330.000.000.33maskedmaskedmasked1.00
0.000.460.030.000.000.060.000.000.46maskedmasked1.00
0.020.010.000.010.020.010.000.020.010.91masked1.00
0.010.000.010.000.010.000.030.010.000.000.931.00

Row 8, cat, attends most to cat at 0.46, with the causal mask on and temperature 1.00.

Inside row 8: “cat

the most divided row - pick another above
0.456
Weight on “cat
5.66
Scale divisor √d
9
Visible positions
2.19
Softmax denominator
The five steps that turn two token vectors into one attention weight, for every key against the selected query.
Keyq·k÷ √d÷ Texp (shifted)weight
0 the-4.57-0.808-0.8080.00160.0007
1 cat32.005.6575.6571.00000.4559
2 sat16.132.8512.8510.06040.0276
3 on0.000.0000.0000.00350.0016
4 the-4.57-0.808-0.8080.00160.0007
5 mat20.163.5633.5630.12330.0562
6 because-5.28-0.933-0.9330.00140.0006
7 the-4.57-0.808-0.8080.00160.0007
8 cat32.005.6575.6571.00000.4559
9 was4.570.808masked00.0000
10 tired-4.03-0.713masked00.0000

The exponent column is exp(logit − 5.657). Subtracting the row maximum before exponentiating is what keeps a softmax from overflowing on large logits; it cancels in the division, so the weights are unchanged.

What is real here, and what is standing in

Real: the scores, the 1/√d scaling, the causal mask, the temperature and the softmax - all computed in this tab, on your sentence.

Standing in: the token vectors. A trained model has learned embeddings; this page hashes character 2- and 3-grams into 32 dimensions instead, so the vectors encode spelling rather than meaning. That is why “runs” and “running” attend to each other and “cat” and “kitten” do not. Q, K and V are those vectors unprojected, so a score here is a plain similarity; a real head multiplies by three learned matrices first, which is what lets one head track syntax and another track coreference.

Everything on this page runs in your browser. Nothing you type is sent anywhere, there is no account, and it keeps working offline.