How does a transformer know which words matter? Attention, shown for real
Every modern model, ChatGPT, Claude, Gemini, is a transformer, and the idea at its centre is called attention. It is usually explained with equations. Here you hover a word and watch a real model's attention light up across the sentence.
- Say what attention does in one sentence, without the word 'matrix'
- Read an attention map and say which word a head is looking at
- Explain why 'the model reads left to right like a person' is wrong
- Say what the 2017 paper changed and why it mattered
The problem attention solves
In the last lesson the model scored the next token using "the text so far". That hides a hard question. The text so far might be 500 words. Which of them matter for the next guess?
Take "The animal did not cross the street because it was too tired." To guess what comes after "it was too tired", the model needs to know "it" means the animal. Swap the last word for "wide" and "it" means the street. Nothing else in the sentence changed. An older kind of model read text one word at a time and tried to carry everything forward in one memory, which blurred by the time it reached "it". The 2017 paper from Google, "Attention Is All You Need", threw that away. Every word gets to look directly at every earlier word and decide, for itself, how much each one matters. That lookup is attention, and it is why the paper's architecture, the transformer, is behind every model you have used since.
What a head is, and what it looks at
A transformer has many of these lookups running side by side. Each is called a head. GPT-2, the model in our lab, has 12 layers with 12 heads each, 144 in total. Each head produces a table: for every word, a share of attention over the words before it, adding up to 100 percent. The numbers in the lab are those tables, read straight out of the model.
Two rules to know before you hover. A word can only attend to itself and the words before it, because this model is trained to predict what comes next and is not allowed to peek. And every head learned its own habit from the data; nobody assigned them jobs. Some of the habits are easy to name.
Real attention weights from GPT-2, all 144 heads. Start with the three heads we picked, then hover any word. The grid below shows the whole table: each row is a word looking, each column a word being looked at.
Three heads worth knowing
The first preset is a head that almost always looks at the word just before. Hover anything and the arc goes one step back. This is how a model keeps word order without being told what order is. "Not cross" and "cross not" look different to it because of heads like this.
The second is the one that earns the lesson. In "Rahul gave his sister the book she had asked for", hover "she". The head sends half of its attention to "sister" and under a third to "Rahul", with the rest scattered. That is pronoun resolution, the thing we said older models blurred, happening in one head of a small 2019 model, learned from nothing but predicting next words.
The third looks like a failure and is not. Many heads dump most of their attention on the first word of the sentence whatever you hover. Researchers call it an attention sink. The head has nothing useful to look at for this word, and the first position is where it parks. Once you know this you will see it in every attention picture ever published.
Now drag the layer and head sliders yourself. Most heads are messy and hard to name. That is the honest picture: a few heads do readable jobs, the rest do their part in ways no one has fully decoded. Interpretability researchers at Anthropic and elsewhere spend their careers on exactly this.
Why it is not reading like you
The biggest wrong mental model is that the model reads left to right, one word at a time, like a person. It does not. All the words in the window are processed at once, and attention is the mechanism that lets any word reach any earlier word in one step, however far back. That is why the 2017 paper could train on eight GPUs in three and a half days: the work parallelises. It is also why context windows are measured in tokens and why they cost so much to grow: every token attending to every earlier token is a cost that grows with the square of the length.
The lesson on context windows picks that up. For now, hold on to the picture: not a reader moving along a line, but a sentence where every word is looking at every other word, in 144 different ways, at the same time.
What came out of a 15-page paper
"Attention Is All You Need" was published in June 2017 by eight researchers at Google. It proposed the transformer for translation. It has been cited more than 250,000 times and is in the top ten most-cited papers of this century. GPT stands for Generative Pre-trained Transformer. Claude and Gemini are transformers. So are the image models, mostly, and the models that write code and the ones that fold proteins.
The paper did not predict any of that. It reported that a model built only from attention translated English to German a little better and much faster than the models of the day. Everything else came from the fact that the same mechanism, given more data and more compute, kept getting better, and nobody has found the ceiling yet.
Q1What does an attention head produce for each word?
Each head is a lookup: how much should this word draw on each earlier word. The next-word prediction comes later, from all the heads combined.
Q2In the lab, why can a word not attend to the words after it?
This is a causal model. In training, letting a word see what follows would make the prediction task trivial, so the mask forbids it.
Q3A head puts 95% of its attention on the first word of the sentence no matter what. This is most likely
Attention sinks are common and well documented. The first token is a convenient place to put attention that is not needed.
What to take with you
- Attention lets every word look at every earlier word and decide how much each matters. A transformer is a stack of these lookups.
- A head is one lookup. GPT-2 has 144. A few do readable jobs (previous word, pronoun to noun); most do not have a name.
- The model does not read left to right. Every word is processed at once, which is why transformers train fast and why long windows are expensive.
- The 2017 paper proposed this for translation. Scale did the rest.
Questions people are asking
How do transformers work? Text becomes tokens, each token looks at every earlier token through attention heads to decide what matters, the results pass through layers of simple maths, and the final layer scores every possible next token. That block is stacked many times.
What is attention in a transformer? A lookup in which each word assigns a share of its focus to each earlier word. The shares add to 100 percent and are learned from data. Many such lookups, called heads, run side by side in every layer.
Is ChatGPT a transformer? Yes. GPT stands for Generative Pre-trained Transformer. Claude, Gemini and nearly every current language model are transformers too.
Why are transformer models called transformers? The 2017 paper that introduced the architecture, 'Attention Is All You Need', named it the Transformer because it transforms an input sequence into an output sequence using attention alone, with no recurrence.
What did 'Attention Is All You Need' change? It replaced models that read text one step at a time with one where every word attends to every other word at once. That made training parallel and fast, and the same design kept improving with scale, which is the story of the last nine years of AI.
Sources
- Vaswani et al., Attention Is All You Need, Jun 2017 arxiv.org/abs/1706.03762
- Wikipedia, Attention Is All You Need (citation count) en.wikipedia.org/wiki/Attention_Is_All_You_Need
- Xiao et al., Efficient Streaming Language Models with Attention Sinks, 2023 arxiv.org/abs/2309.17453
- Clark et al., What Does BERT Look At? An Analysis of BERT's Attention, 2019 arxiv.org/abs/1906.04341
- 3Blue1Brown, Attention in transformers, step-by-step, Apr 2024 youtube.com/watch?v=eMlx5fFNoYc
How are ChatGPT and Claude actually trained? Pretraining, human feedback, and why they are so polite
One email a week. Written by people.
New lessons and how-tos, the tools we tested and what we think, in one short email.