Attention, step by step
Score every word against a query, softmax the scores and blend the values, by hand.
Score every word against a query, softmax the scores and blend the values, by hand.
Each word of a sentence starts as a short list of numbers (its vector) that knows nothing about the other words. Attention lets every word look at the others and borrow from them. The word asks with a query (q), every word answers with a key (k), and what it borrows is their values (v). Here each query, key and value has just 2 numbers, and they are set by hand, not trained, so you can check every step on paper.
The question: how does a word decide which other words to look at, and what does it take from them?
Chapter 1 of 5 · One word
One word asks a question: its query q. Every word’s key k is scored against it with a dot product, q·k. Each score is divided by √d_k = √2, e is raised to it, and the results are shared out so they add up to 1. Those weights blend the words’ values into the output. Tap a word to make it the query, and drag the purple arrow.Tap a word to make it the query. Shading and percentages: the share of attention it gets.
kite: q·k = (−1.00)(0.00) + (2.00)(2.00) = 4.00. On the picture: the shadow of q on the key’s line (2.00) times the key’s length (2.00). A shadow pointing the other way gives a negative score.
Keys of 0 (score 0 whatever q is): bought, a, and, it.
Masked (later words, −∞ whatever q is): flew.
Drag the purple tip of q, tap the plot, or focus the tip and use the arrow keys, to ask a different question; tap a key’s name to see its shadow.
Score = q·k ÷ √2. Weight = e^score ÷ the total of the e^score column, so the weights add up to 1.
| word | key k | q·k | ÷√2 | e^score | weight |
|---|---|---|---|---|---|
| (2.00, 0.00) | −2.00 | −1.41 | 0.243 | 0.01 | |
| (0.00, 0.00) | 0.00 | 0.00 | 1 | 0.05 | |
| (0.00, 0.00) | 0.00 | 0.00 | 1 | 0.05 | |
| (0.00, 2.00) | 4.00 | 2.83 | 16.9 | 0.80 | |
| (0.00, 0.00) | 0.00 | 0.00 | 1 | 0.05 | |
| (0.00, 0.00) | 0.00 | 0.00 | 1 | 0.05 | |
| (0.00, 0.00) | — | −∞ | 0 | 0 | |
| total | not added | 21.2 | 1.00 | ||
| word | weight | value v | weight × v |
|---|---|---|---|
| Riya | 0.01 | (1.00, 0.00) | (0.01, 0.00) |
| bought | 0.05 | (0.00, 0.00) | (0.00, 0.00) |
| a | 0.05 | (0.00, 0.00) | (0.00, 0.00) |
| kite | 0.80 | (0.00, 1.00) | (0.00, 0.80) |
| and | 0.05 | (0.00, 0.00) | (0.00, 0.00) |
| it | 0.05 | (0.00, 1.00) | (0.00, 0.05) |
| output | add them up: | (0.01, 0.85) | |
“it” is the word’s 8 numbers x: its own (person, thing, he/she, it) plus a position vector (pos₁, pos₂), so x = word vector + position vector. The last two numbers start at 0 for every word: head 2 writes into them (chapter 4). Multiply each number of x by its row of W_Q and add up to get q; the same with W_K gives k, and W_V gives v.
| number | x | W_Q | W_K | W_V | |||
|---|---|---|---|---|---|---|---|
| person | 0 | 2 | −1 | 2 | 0 | 1 | 0 |
| thing | 0 | −1 | 2 | 0 | 2 | 0 | 1 |
| he/she | 0 | 2 | −1 | 0 | 0 | 1 | 0 |
| it | 1 | −1 | 2 | 0 | 0 | 0 | 1 |
| pos₁ | −1 | 0 | 0 | 0 | 0 | 0 | 0 |
| pos₂ | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| prev person | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| prev thing | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| x · W | x is not multiplied | q = (−1, 2) | k = (0, 0) | v = (0, 1) | |||
Query, key, value. Think of a library. Your query is what you are looking for, each key is the label on a shelf, and each value is what the shelf holds. Each comes from the word’s numbers x times a matrix: q = x·W_Q, k = x·W_K, v = x·W_V.
Scores and softmax. The score q·k is a dot product: big when q and k point the same way, 0 when they are at right angles, negative when they point apart. Divide by √d_k, then softmax: raise e to each score and divide by the total, so the weights are positive and add up to 1. The output is the weighted sum of the values, a blend that leans towards the words with most weight.
The mask and heads. A model that writes the next word must not peek at the words after it, so GPT sets those scores to −∞ (weight 0). A layer runs several heads, each with its own matrices, so each can look for something different; their outputs are joined and mixed by W_O. GPT-2 gives every token 768 numbers and 12 heads of 64. Here a word has 8 numbers and a head uses 2, so every number fits on the screen.
Set by hand. No number here was learned. We chose them so that you can read them: one number means “a person”, another “a thing”. A real model learns W_Q, W_K and W_V from text, and its numbers are rarely this tidy.
Query, key, value. Think of a library. Your query is what you are looking for, each key is the label on a shelf, and each value is what the shelf holds. Each comes from the word’s numbers x times a matrix: q = x·W_Q, k = x·W_K, v = x·W_V.
Scores and softmax. The score q·k is a dot product: big when q and k point the same way, 0 when they are at right angles, negative when they point apart. Divide by √d_k, then softmax: raise e to each score and divide by the total, so the weights are positive and add up to 1. The output is the weighted sum of the values, a blend that leans towards the words with most weight.
The mask and heads. A model that writes the next word must not peek at the words after it, so GPT sets those scores to −∞ (weight 0). A layer runs several heads, each with its own matrices, so each can look for something different; their outputs are joined and mixed by W_O. GPT-2 gives every token 768 numbers and 12 heads of 64. Here a word has 8 numbers and a head uses 2, so every number fits on the screen.
Set by hand. No number here was learned. We chose them so that you can read them: one number means “a person”, another “a thing”. A real model learns W_Q, W_K and W_V from text, and its numbers are rarely this tidy.
Things to try