What does bank mean? In “I deposited money at the bank” it's a financial institution. In “The dog ran to the bank of the river” it's the side of a river. In “The plane began to bank” it's a verb: to tilt while turning. The word is the same; only the context differs. To understand a word, you have to read the words around it.
The Transformer is the architecture behind nearly every modern language model, and its central idea mirrors exactly this: each word updates its own representation by looking at all the other words in the sentence and deciding how much each one matters. This reading builds that idea intuitively, without the full machinery, and explains why it works better than reading a sentence one word at a time.
1Two problems with reading one word at a time
A recurrent neural network (RNN) reads a sentence word by word, keeping a hidden state $h_t$ that it updates at every step: $h_t$ depends on the current word and on $h_{t-1}$. This design has two well-known problems:
- Long-distance information is hard to keep. For information about word 1 to affect word 50, it must survive 49 updates of the hidden state, and the training signal must travel back through all of them. Over long distances the gradients tend to vanish, so the model struggles to use far-away context.
- It can't be parallelized. $h_t$ can't be computed until $h_{t-1}$ is done, so the steps must run one after another. Modern hardware (GPUs) is fast precisely because it does many computations at once, and a strictly sequential model can't take advantage of that.
In 2017, Vaswani et al. proposed a model that drops recurrence entirely and relies on attention alone, in a paper titled “Attention Is All You Need.” They called the model the Transformer.
2How do humans decide what a word means?
Read “This is the bank of the river.” When you reach bank, your initial understanding holds several possible senses at once. Then you use the context: the word river strongly suggests a riverbank, so your refined understanding settles on that sense.
Notice two things about this process:
- Different context words have different weights. river matters a lot for interpreting bank; this, is, and the matter very little.
- Every word goes through this. of is also interpreted in context: here it links bank and river, so it pays most attention to those two words.
3How a Transformer mimics this
Each word starts with an initial word vector $\mathbf x_1,\dots,\mathbf x_n$, one per word. To update the vector of one word, the Transformer assigns an attention weight $\alpha_i$ to every word $i$ in the sentence, including the word itself, and takes the weighted sum:
$$\text{updated vector}=\sum_{i=1}^{n}\alpha_i\,\mathbf x_i,\qquad \alpha_i\ge0,\quad \sum_{i=1}^{n}\alpha_i=1.$$Because the weights are non-negative and sum to 1, this is a weighted average of the word vectors. A word with a large weight pulls the updated vector toward itself; a word with weight near 0 barely affects it. The updated vector is a contextualized representation: it reflects both the word and its context.
To see this concretely, imagine word vectors with just two dimensions: one for the “finance” sense and one for the “river” sense. On its own, bank sits in the middle, because it could mean either.
Where do the weights come from? In a real Transformer they are not set by hand: they are computed from the word vectors themselves, so that related words automatically receive large weights. Exactly how is a story of its own; for now, the key idea is the weighted average.
4Every word, all at once
The same update is applied to every word in the sentence, each with its own set of attention weights. Arrange the weights in a table, with one row per word being updated and one column per word being attended to:
Crucially, no row depends on the result of another row: every updated vector uses only the initial vectors. So all the words can be updated at the same time, in parallel, which is exactly what GPUs are good at.
And the process can be repeated. The updated vectors become the input to another round of the same kind of update, producing vectors that are refined again. Stacking many such layers lets the model build up increasingly rich, context-aware representations.
5Fixing the two problems
| RNN | Transformer | |
|---|---|---|
| Steps for information to travel between words $i$ and $j$ | $|i-j|$ | 1 (direct connection) |
| Sequential steps to process $n$ words (per layer) | $n$ | 1 (all words in parallel) |
- Long-distance information: every word connects directly to every other word in a single step, so bank can use river whether it is two words away or two hundred.
- Parallelizability: all word vectors are updated at the same time, so training scales well on modern hardware, which is what made today's very large language models possible.
6Check your understanding
Exercise 1: a weighted average
Three words have 2-dimensional vectors $\mathbf x_1=[1,0]$, $\mathbf x_2=[0,1]$, $\mathbf x_3=[1,1]$. With attention weights $\alpha=(0.2,\,0.5,\,0.3)$, compute the updated vector.
Exercise 2: steps
In a 100-word sentence, how many sequential steps does an RNN need to carry information from word 3 to word 90? How many does a single Transformer layer need? How many sequential steps does each need to process all 100 words in one layer?
Exercise 3: word order
Using only the weighted-average update (with weights that depend only on which words are present), would “dog bites man” and “man bites dog” give the word bites the same updated vector? Why is this a problem?
Quick quiz
7Further reading
- Alammar. The Illustrated Transformer. (A classic, highly visual walkthrough of the whole architecture.)
- 3Blue1Brown. Attention in Transformers, Visually Explained. (Uses exactly this kind of context-dependent meaning as its starting point.)
- Vaswani, Shazeer, Parmar, Uszkoreit, Jones, Gomez, Kaiser & Polosukhin (2017). Attention Is All You Need. NeurIPS.