3Blue1Brown neural networks ch. 6: attention — notes and key slides
Watch the video (26 min)
Watch the original video on YouTube
Chapter 6 of 3Blue1Brown’s deep learning series opens up the attention block, the part of a transformer that lets each token’s vector take in information from the rest of the text. It walks through a single attention head with a made-up example, then scales up to multi-head attention and counts GPT-3’s parameters. Watch Chapter 5 on transformers first: you need embeddings, dot products and softmax from there. Grant says himself that many people find attention confusing, so expect to rewatch parts. The next chapter is how LLMs store facts.
Key takeaways
- Attention moves context into embeddings. The first embedding of “mole” is the same in every sentence; attention computes what to add so it points toward the right meaning (2:00).
- Query-key dot products score relevance, and a column-wise softmax turns them into the attention pattern (8:30).
- Masking keeps later tokens from influencing earlier ones, and the pattern grows with the square of the context size (12:14).
- Values carry the actual update. Each column adds a weighted sum of value vectors to the original embedding (13:37).
- Many heads run in parallel, 96 per block in GPT-3, and all attention adds up to about 58 billion parameters, a third of the model (24:16).
Chapter notes
0:00 – 2:00 Recap
A quick reminder of Chapter 5 (0:30): text becomes tokens, tokens become high-dimensional vectors, and directions in that space carry meaning. The transformer’s goal is to adjust those vectors step by step so they encode the meaning of a word in its context, not just the word.
2:00 – 4:34 What attention is supposed to do
Grant’s mental picture: there are separate directions in embedding space for each meaning, and a well-trained attention block computes the step to add to the generic vector to land in the right one. “Tower” after “Eiffel” should move toward Paris and steel; add “miniature” and it should stop pointing toward large, tall things.
Attention can also move information over long distances. If the input is a whole mystery novel ending in “therefore the murderer was”, the last vector must somehow carry everything relevant to name the murderer (4:06). This connects back to Chapter 5: only the last vector is used for the prediction.
4:34 – 10:08 One head: queries, keys and the attention pattern
The working example is “a fluffy blue creature roamed the verdant forest”, with one imagined job: adjectives should update their nouns. Grant is clear that this is a made-up behaviour to make the matrices concrete. Real heads learn whatever helps prediction and are much harder to read.
A second matrix, W_K, maps every embedding into the same small space to produce keys. A key “answers” a query when the two vectors point the same way, so relevance is just their dot product.
The raw scores can be any real number. To use them as weights, each column goes through softmax so its entries are between 0 and 1 and sum to 1. The normalised grid is the attention pattern (10:08).
10:08 – 13:16 The formula, masking and the cost of context
During training the model predicts the next token at every position simultaneously (Chapter 5 mentioned this). That creates a leak: if a later word could influence an earlier one, the model could see the answer. The fix is masking.
Grant also points out that the pattern has one entry per pair of tokens, so it grows with the square of the context size (12:37). This is why long context windows are hard, and why research into sparse and blockwise attention variants exists. The video names a few but does not go into them.
13:16 – 16:47 Values: actually updating the embeddings
Knowing who is relevant is not enough; something has to be added. A third matrix, the value matrix, turns each embedding into a value vector that lives in the full embedding space. Think of it as “if I am relevant to you, here is what to add to you”.
Every column does the same in parallel, so a whole sequence of refined embeddings comes out of the head. That is one head of attention, defined by three learned maps: query, key and value.
16:47 – 18:22 Parameter count and the low-rank value map
Query and key matrices are 128 × 12,288 each, about 1.5 million weights. A full value matrix would be 12,288 × 12,288, about 150 million, which is far more. In practice the value map is split into two thin matrices through the same small space.
18:22 – 19:22 Side note: cross-attention
19:22 – 22:04 Multi-head attention
Adjectives updating nouns is only one kind of context. “They crashed the” changes what “car” means; “wizard” or “Queen, Sussex, William” changes which Harry is meant. Each kind of update needs its own key, query and value maps. So a block runs many heads in parallel, each with its own pattern, and adds all their proposed changes to each embedding.
22:04 – 24:54 Output matrix, layers and the 58 billion
One detail if you read papers or code: the “value up” matrices of all heads are usually stapled together into one output matrix for the whole block, and “the value matrix” of a head then means only the down-projection (22:47). It is the same computation organised differently.
Data also passes through many layers of attention and MLP. Each time, embeddings that are already richer get to absorb context from neighbours that are also richer, which is where higher-level features such as tone or topic can form.
24:54 – 26:09 Why attention won
Grant’s closing point is not about any behaviour attention enables but about hardware: it is highly parallelisable, so it runs well on GPUs (24:54), and scale has been the main driver of better models. He recommends work by Andrej Karpathy and Chris Olah for going deeper.
Skip list
- 0:00 – 2:00 Recap of Chapter 5. Skip if you just watched it.
- 18:22 – 19:22 Cross-attention. Not used in GPT models; skip on a first pass.
- 22:04 – 23:37 Output-matrix convention. Only matters when you read papers or code.
- 25:28 – end Recommendations for other channels, and credits.
Glossary
- Attention head — one set of query, key and value maps producing one attention pattern and one set of updates.
- Query — a small vector representing what a token is looking for.
- Key — a small vector representing what a token can offer; matched to queries by dot product.
- Value — the vector a token contributes to another token’s embedding when relevant.
- Attention pattern — the grid of softmax-normalised relevance scores between every pair of tokens.
- Masking — setting future-to-past scores to −∞ before softmax so later tokens cannot affect earlier ones.
- Multi-head attention — many heads run in parallel, with their updates summed.
- Cross-attention — attention where keys and queries come from two different sequences.
FAQ
What are queries, keys and values in attention?
Each token's embedding is multiplied by three learned matrices. The query is what that token is looking for, the key is what it can offer, and the value is what it would add to another token's embedding if it turns out to be relevant.
Why does attention divide by the square root of the key dimension?
The video describes it as a numerical stability detail. Dot products of longer vectors tend to be larger, and scaling them down keeps the softmax from becoming too peaked.
What is masking in a GPT-style transformer?
Before softmax, every score where a later token would influence an earlier one is set to negative infinity, so it becomes zero after softmax while the columns still sum to one. This stops the model from seeing the answer it is supposed to predict during training.
How many parameters does attention use in GPT-3?
About 6.3 million per head, roughly 600 million per multi-head block with 96 heads, and just under 58 billion across all 96 layers. That is only about a third of GPT-3's 175 billion parameters; most of the rest sit in the MLP blocks.
Studying another course? Paste its YouTube or Bilibili link on the home page to request a handout — or make your own with the desktop app (coming soon).