Lesson 14: Sequences, Attention and Transformers
The transformer is the architecture behind modern language models, and its central idea is attention: instead of reading a sentence step by step and hoping the memory survives, it lets every word look directly at every other word and decide which ones matter. When the model reads the word "bank" in "deposit a cheque at the bank", attention lets it pull information from "deposit" and "cheque" and push away the river meaning; in "down by the river bank", the same word gathers a different context. Meaning comes from the company a word keeps.
Attention works with three roles for each word: a query, a key and a value. Think of the query as the question a word asks — "which other words help define me?" — the key as the label each word offers, and the value as the information each word contributes. The model compares each query with every key, turns the matches into weights, and takes a weighted mix of the values. The result is a new representation of each word, enriched by exactly the context it needs. Because every word is compared with every other word at once, the whole sentence is processed in parallel, unlike an RNN's slow step-by-step walk.
Heads, Layers and Position
A transformer uses many attention heads at once. Each head learns a different kind of relationship: one might track which words belong to the same phrase, another might link a pronoun to the noun it refers to, a third might watch for the subject of a verb. The heads run in parallel and their results are combined, so each layer of the transformer refines every word's meaning from several angles at once. Stacking many such layers builds the deep understanding that lets these models handle language so well.
One thing attention does not know on its own is order: if you shuffled the words, the set of comparisons would be the same. Transformers therefore add positional information to each word's representation, so the model can tell first from last. That combination — attention for meaning, position for order, many heads, many layers — is the whole architecture, repeated in a simple, regular block.
Why It Replaced the RNN
Transformers won for three reasons. They process a whole sequence in parallel, so training on long texts is far faster. Their attention links words directly, however far apart, so long-range meaning survives where an RNN's memory faded. And they scale: bigger transformers, trained on more text, keep getting better, which is why the same design now powers translation, summarisation, question answering and chat. The cost is real — comparing every word with every other word grows quickly with length — but for language, the trade has been worth it.
Attention Beyond Language
Attention began in language, but the idea travels. In image models, attention lets a model relate distant parts of a picture — the head of a dog to its tail, say — without passing through many layers. In audio, it lets a model connect a sound now with one several seconds earlier. The common thread is the same: when the important relationship is between two things far apart, attention draws a direct line between them instead of hoping the information survives a long chain of steps. That is why the transformer has spread beyond language into vision, sound and science.
There is a cost, and it is worth naming. Comparing every element with every other grows quickly: a sequence twice as long needs roughly four times the comparisons. For short texts this is fine; for a whole book, or a long recording, it becomes expensive, and researchers use tricks such as attending only to nearby elements, or to a summary of the distant ones. The trade-off between direct connections and cost is the central engineering question for transformers, and it is still an active area of research.
Key Terms
- Transformer
- A neural architecture built from attention layers, used for language and many other sequence tasks.
- Attention
- A mechanism that lets each word gather information from the other words that matter most to it.
- Query, key, value
- The three roles in attention: the question a word asks, the label each word offers, and the information it contributes.
- Attention head
- One attention mechanism in a layer; different heads learn different kinds of relationship.
- Positional information
- Extra numbers added to each word's representation so the model knows the word's position.
- Parallel processing
- Handling a whole sequence at once, rather than step by step as an RNN does.
Discussion Questions
- Why does the word 'bank' need attention to be understood? What would go wrong without it?
- Attention compares every word with every other word. Why does that make long documents expensive to process?
- An RNN reads in order; a transformer reads everything at once. What does each gain, and what does each lose?
Practice Questions
- What is attention in a transformer?
- What are the query, key and value for a word?
- Why does a transformer need positional information?
- Give one advantage transformers have over RNNs.
- What does it mean for a transformer to have multiple attention heads?
Show answers
- A mechanism that lets each word gather information from the other words that matter most to it, by comparing queries with keys and mixing the values.
- The query is the question the word asks; the key is the label each word offers for matching; the value is the information each word contributes. Attention weights the values by how well queries match keys.
- Because attention on its own does not know word order — shuffling the words would give the same comparisons. Positional information tells the model which word came first, second, and so on.
- Any one of: they process a sequence in parallel so training is faster; they link distant words directly so long-range meaning survives; or they scale well with size and data.
- It runs several attention mechanisms in parallel in each layer, so different heads can learn different kinds of relationship — such as phrase structure or pronoun links — at the same time.