In this lesson: attention, the step where every token collects information from the tokens before it, and why the token with the most attention is often not the one that matters. You'll see the real weights of a small model, two heads with very different habits, and why the first token soaks up so much weight. Read the lesson and take the 5-question quiz: each question has a hint if you get stuck, and its explanation shows once you get it right (all of them at full marks). Pass the quiz to finish the lesson and earn its badge.
1. Where does " mine" look?
Take the sentence "My friend lent me her pen because I forgot mine." When the model reaches " mine", which earlier token gets most of its attention? You'd probably guess " pen", the thing you forgot. We asked distilgpt2, which does attention in 72 small parts, its heads (12 side by side in each of its 6 layers; section 3). We loaded it with its plain version of attention, so that it hands its weights back (section 5 says why), split the sentence into its 11 tokens and ran it through once. Each head gives a grid of weights with one row per token: how much that token takes from each token it can see. We took the row of " mine" in all 72 heads and averaged them. The four biggest:
| Token | Share of " mine"'s attention |
|---|
| "My" | 0.53 |
| " forgot" | 0.15 |
| " mine" | 0.10 |
| " because" | 0.06 |
The winner was "My", the first token, with 0.53: more than half of all the attention. " pen", the word you might expect, got only 0.03. The rest of the lesson explains how those numbers are made and what they do and don't tell you.
\" mine\" looks most at \"My\", not \" pen\"
2. What attention does
In H2 (AI Hardcore lesson 2, on embeddings) you saw that a token's vector depends on the words around it. Attention is the step that does that. At each layer (the model works on the token vectors layer after layer; distilgpt2 has 6), every token:
- makes a query ("what am I looking for?"), and every token it can see offers a key ("what do I have?") and a value (the information itself);
- scores each key against its query with a dot product (multiply two lists number by number and add up), then softmax turns the scores into weights that add up to 1;
- takes the weighted average of the values; the results of all the heads (section 3) are joined, mixed by one more learned step (a small set of fixed numbers the model learned in training, not the weights that add up to 1), and added to its own vector.
Here are the figure's numbers worked through. The four dot products are made up for the example (not taken from the model); each is divided by 8 (the maths below says why), and softmax turns the results into weights:
| Key | Query · key | Divided by 8 | Weight |
|---|
| "My" | 16.0 | 2.0 | 0.56 |
| " pen" | 1.6 | 0.2 | 0.09 |
| " forgot" | 8.0 | 1.0 | 0.21 |
| " mine" | 4.8 | 0.6 | 0.14 |
The weights add up to 1.00. Each value is a list of 64 numbers; " mine" takes 0.56 of the value of "My", 0.09 of the value of " pen", and so on, and adds them up. The mix is one new list of 64 numbers, the same size as each value: that is what " mine" carries on to the next layer.
In maths, for one head with 64 numbers per key: weights=softmax(64QK⊤+M). In words: score every query against every key, divide by 64=8, add the mask, then softmax each row. Dividing keeps the scores from growing too big. A dot product adds up 64 products, so it grows with how many numbers each vector has. With scores that big, one token takes almost all the weight, and the weights barely move when the scores change; training improves the model by seeing which way small changes move its answers, so it can hardly tell which way to change it. Softmax (more in H4, the next lesson) raises e (a fixed number, about 2.718) to each score and divides by the row's total, so every weight is positive and each row adds up to 1.
The mask M hides every later token: it adds a huge negative number (in effect minus infinity) to their scores, so their weight comes out as 0. In distilgpt2 a token can look at itself and the tokens before it, never ahead, because when it writes, the next words don't exist yet. That's why the top-right half of every grid below is empty.
Score the keys, softmax, mix the values
3. Heads and layers
A layer doesn't do this once but 12 times side by side, with 12 sets of queries, keys and values: 12 heads. distilgpt2 has 6 layers, so 72 heads. For our 11-token sentence that is 6 × 12 × 11 × 11 = 8,712 weights, and every row adds up to 1.
Heads have habits. Here are two real ones, drawn as grids: each row is one token, each column a token it can see, and the grey, empty cells are the later positions the mask hides. (Heads and layers are numbered from 0, the way the model's makers count them.)
- Layer 2, head 11 looks at the token just before (0.99 on average): a stripe just below the diagonal.
- Layer 4, head 2 puts most of its weight on the first column, "My" (0.88–0.95 for the early tokens). Later tokens spread more: " because" gives " pen" 0.37.
- A third, not drawn: layer 0, head 1 looks at itself (0.98 on average).
So the 12 heads in a layer don't all learn the same pattern, and looking at one head tells you little about the others. In layer 2, head 11 looks at the previous token while head 4 puts 0.84 on the first token (averaged over the tokens after the first, since the first can see only itself; more in section 4).
Two heads, two habits, the same sentence
4. The first token soaks up the weight
In this sentence 49 of the 72 heads put more than half their weight on the first token, averaged over the tokens after the first (the first token's own row is always 1.00, since it can see only itself). Weights must add up to 1, so a head that has nothing useful to collect has to park them somewhere, and the first token, which every token can see, became that place. Researchers call it an attention sink.
You can move it. Put the special token "<|endoftext|>" (the token GPT-2 models, distilgpt2 among them, use to mark where a text begins and ends) in front of the sentence, and " mine" gives "<|endoftext|>" the same 0.53, while "My" drops out of its top four. A different sentence shows the same habit: in "After the exam we studied in the library until it closed.", " closed" gives the first token "After" 0.50 and " library" only 0.04.
49 of 72 heads park their weight up front
5. What attention isn't
So "My" got the most attention, but the 0.53 is about its position, not the word: put "<|endoftext|>" first and the 0.53 moves there. A weight says where a head collected information from, not why the model writes what it writes. After attention, each layer has more steps the heatmap (the grids of section 3, coloured by weight) doesn't show, and the 72 heads' results are mixed together. A heatmap is a picture of one part of the machine, not a window into its reasoning.
Here is a wrong line you might read: "The token with the biggest weight is the word the model treats as most important for its answer." Section 4 already broke it. To find out whether a token or a head really matters, change it and see whether the output changes. To test a head, a heatmap isn't enough: switch the head off (zero its output) and compare the model's top-10 next tokens with and without it.
To see the weights at all, we had to ask for the plain version of attention. Loaded the default way, the model used a faster version that does the same maths without keeping the weights, and the weights we asked for came back empty.
distilgpt2 learned only English, so this lesson's sentences are in English. Big chat models use the same idea (queries, keys, values, softmax, a mask, many heads), with far more layers and heads and some changes to make it faster, such as heads sharing their keys and values.
Attention: each token mixes in information from itself and the tokens before it, by weights that add up to 1.
A big weight shows where a head collected information (often just the first position), not what the model thinks matters.
To find what matters, change a token or switch a head off and see whether the output changes.
A weight shows where, not why
Do it yourself in the ML Lab Who Looks at Whom, step The swap log.