Lesson 3: How Transformers Pay Attention
A language model must understand each word in context. The word 'bank' means one thing in 'down by the river bank' and another in 'deposit a cheque at the bank', and the model can only tell which by looking at the other words around it. The mechanism that lets it do that is called attention, and it is the heart of the transformer — the architecture that nearly all modern language models are built on. The transformer was introduced in 2017 by a team at Google in a paper called 'Attention Is All You Need', and its design is what made today's large language models possible.
Here is the idea in plain words. Each token starts as a list of numbers — its embedding — which captures something of what the word means on its own. Then, in each layer of the transformer, every token looks at the other tokens in the sentence and asks: which of you are relevant to me? The word 'bank' in the river sentence pays attention to 'river'; in the money sentence it pays attention to 'cheque' and 'deposit'. Attention is a set of weights — numbers saying how much each token should listen to each other token — and the token's numbers are updated by mixing in what it heard. After several layers of this, each token's numbers carry a rich sense of its meaning in this particular sentence.
Many heads, and doing it all at once
A transformer does not pay attention in only one way. It uses several attention heads in parallel, each learning a different kind of relationship: one head might track which words refer to the same person, another might track verbs and their subjects, another might track cause and effect. The heads' results are combined, so each token ends up carrying many kinds of context at once. The other great advantage of the design is that it processes all the tokens at the same time, rather than one after another, which lets it use modern computer hardware — which does many calculations in parallel — extremely well. That combination, attention plus parallelism, is why transformers could be trained on far more text than anything before them.
There is a limit worth knowing: attention gets expensive as text gets longer. Every token must consider every other token, so doubling the length of the text roughly quadruples the work. That is why models have a context window — a maximum number of tokens they can consider at once — and why very long documents are hard for them. Engineers have found many ways to soften this cost, but the basic trade remains: the more context a model can see, the more it costs to run.
How the model knows the order of words
Attention has a blind spot: on its own, it treats a sentence as a bag of words, with no idea which word came first. 'Dog bites man' and 'man bites dog' would look the same to it, which would be a serious problem for a language model. The fix is to add positional information: each token's numbers are combined with another set of numbers encoding where the token sits in the sequence — first, second, third — so the model can tell the order apart. Early transformers added a fixed pattern based on waves of different frequencies; newer ones learn positions from data, or encode the relative distance between words rather than their absolute place. The details differ, but the principle is the same: meaning depends on order, so order must be part of what the model sees. It is a small piece of engineering with a large effect — without it, the whole architecture would be unable to tell a subject from an object.
You do not need the mathematics of attention to use language models well, but the picture matters. When a model answers well, it is often because attention let the right words inform each other; when it answers badly — confusing two people with the same name, or losing track of a long instruction — it is often because attention failed to connect the right pieces. In the next lesson you will see what those token numbers actually contain, and how they give a model its sense of meaning.
Key Terms
- Transformer
- The architecture most modern language models are built on, introduced in 2017, based on the attention mechanism.
- Attention
- The mechanism that lets each token look at the other tokens and take in information from the ones relevant to it.
- Attention head
- One of several parallel attention mechanisms in a layer, each learning a different kind of relationship between words.
- Embedding
- A list of numbers representing a token, capturing something of what it means; embeddings are updated by attention as context flows in.
- Context window
- The maximum number of tokens a model can consider at once; text longer than the window cannot all be seen together.
- Parallel processing
- Doing many calculations at the same time, which transformers exploit by handling all tokens together rather than one by one.
Discussion Questions
- The word 'bank' changes meaning with context. How does attention let the model tell the two meanings apart?
- Why does a transformer need several attention heads instead of just one?
- If attention gets expensive with longer text, what problems would that cause for a model asked to summarise a whole book?
Practice Questions
- What problem does attention solve?
- What is the transformer?
- What is an attention head?
- Why can transformers be trained on so much text?
- Why do models have a context window?
Show answers
- It lets each word take in meaning from the other words around it. Without attention, 'bank' would mean the same thing in a river sentence and a money sentence.
- The architecture most modern language models use, based on attention, introduced in 2017. It was introduced by a team at Google in the paper 'Attention Is All You Need'.
- One of several parallel attention mechanisms, each learning a different kind of relationship. One head might track who refers to whom, another verbs and their subjects.
- Because they process all tokens at once, using hardware that does many calculations in parallel. Earlier designs read words one after another, which was far slower to train.
- Because attention work grows fast as text gets longer — roughly four times the work for twice the text. So there is a maximum number of tokens a model can consider at once.