Attention
On its own, a token embedding is context-blind: "bit" is the same vector whether a dog did it or you only want a little. Attention is the step that lets each token look at the others and rewrite itself. Here is how, one matrix at a time.
the problem: context-blind tokens
One vector. Three meanings.
Before attention, every token is just a lookup. The word "bit" maps to the exact same vector in all three sentences below, even though it means something different in each.
01 / 04 · look up row in W_E
Three sentences, one word
"bit" shows up in three sentences. To a human, context settles the meaning instantly. The model has not looked at the context yet.
embedding · 1024-dim
the big picture
One head, end to end.
Before we zoom in, here is the whole thing in one diagram. A single attention head turns each token into a Query, Key, and Value, scores every pair, masks the future, softmaxes into weights, and mixes the Values into a new, context-aware vector. Every scene below zooms into one box.
01 / 07
Tokens go in
The sentence enters as one vector per token: the, dog, bit.
the machinery: Q, K, V
One vector becomes three.
Attention starts by multiplying each token's embedding by three learned matrices, producing three small vectors: a Query (what it is looking for), a Key (what it advertises), and a Value (what it carries). For example, bit's Query asks "who did the biting?", dog's Key matches it well, and dog's Value carries the content that flows into bit.
recapNew to Q, K, V? The intuition, with the same "the dog bit" example, is in Positional Encoding.
bit embedding · 1024
01 / 04
Start from the embedding
Take "bit" as a 1024-dim vector. Every token in the sentence does the same three projections; we follow "bit".
what a token is looking for
e.g. bit: a verb hunting for its subject, who did the biting?
what a token advertises about itself, like a billboard
e.g. dog: I am a noun, an agent.
the content a token carries, blended in when it is attended to
e.g. dog's meaning gets pulled into bit.
col 1 / 8
why a score is high
A score is just alignment.
A score is a dot product, and a dot product measures alignment: two vectors pointing the same way score high, at a right angle score zero, opposite score negative. Drag dog's Key and watch bit's score rise as the two arrows line up.
angle 64°
The next scene fills a whole matrix of these scores. Here you can move one pair by hand.
step 1: attention scores
How well does every pair match?
To decide where "bit" should look, attention compares its Query against every Key with a dot product. Do that for all pairs and you get a score for each (Query, Key) cell.
Key (looked at)
01 / 03
the looks around
First row: "the" compares its Query against every Key. It is a determiner, so nothing lines up strongly.
Every cell is one dot product: how aligned is Query i with Key j.
step 2: scale and mask
Shrink the scores, mask later tokens.
Two quick fixes before the scores become weights. Divide them down so they stay stable, then block every token from looking ahead.
left to right
Generation fills the rows one token at a time. Each new token attends to itself and the past, never ahead, so the upper triangle stays dark. That is what makes the model autoregressive.
step 3: softmax
Scores become attention.
Softmax turns each row of masked scores into a clean set of weights: all positive, summing to 1. The -inf cells fall to 0. Pick a token to see its row.
row · bit
"bit" can see all three. Most of its attention, 37%, goes to "dog", with the rest split across "the" and itself.
Softmax exponentiates each score and normalizes, so the row becomes a probability distribution.
who attends to whom
The attention map.
Put the weights back onto the words. Each token sends its attention along edges to the tokens it looks at; a thicker, brighter edge means more weight. This is the picture people mean when they say attention. Hover or tap a token to see only its attention.
step 4: weighted sum
bit, rewritten by its neighbors.
The final step: take "bit"'s attention weights and use them to mix the Values. The output is a weighted average, so the tokens "bit" attended to flow into its new vector.
bit's attention · Values (V)
new bit · 64
01 / 04
Start with bit's weights
From the softmax step, "bit" attends 30% to "the", 37% to "dog", 33% to itself.
context-blind bit
context-aware bit
all at once: 16 heads
Sixteen heads, one forward pass.
Everything so far was one head. A real layer runs many in parallel, each with its own W_Q, W_K, W_V. The model here uses 16 heads of size 64. Their outputs are concatenated back to 1024.
input · 3 × 1024
what different heads attend to
verb ↔ subject
nearby words
coreference
Same sentence, three heads. Each learned its own attention pattern: one tracks the verb and its subject, one stays local, one links a word back to what it refers to. Concatenating their outputs gives the layer 16 views at once.
concat · 16 × 64 = 1024
context-aware output · 3 × 1024
01 / 03
One input, many heads
The same "the dog bit" goes into all 16 heads. Each head has its own projection matrices, so each builds a different Query, Key, Value.
Sizes at a glance
- d_model (embedding)
- 1024
- heads
- 16
- head size dₖ
- 64
- √dₖ (scale)
- 8
back to the start
Same token, three vectors.
Remember the three sentences where "bit" started as the exact same vector. After attention, each "bit" has attended to different neighbors, so the three come out as three different vectors. The context-blind input became a context-aware output.
the dog bit
a little bit
set the bit
01 / 02
Three identical starts
All three "bit" tokens begin as the same vector, exactly as at the top of the page.
the takeaway
Same token, new meaning.
Attention projects each token into Query, Key, Value, scores every pair with a scaled dot product, masks the future, softmaxes into weights, and mixes the Values. The context-blind vector comes out context-aware, and 16 heads do it from 16 angles at once, blended back together by a final learned projection.
One thing attention can't do on its own: tell word order apart. "the dog bit the man" and "the man bit the dog" look identical to it. That is what Positional Encoding fixes.