The bottleneck, and the fix: look back
A minister gives a speech in English, and an interpreter must say it in Hindi. The first interpreter listens to the whole speech, holds it in her head, and then speaks. For a short speech this works. For a long one, the details are gone before she opens her mouth.
The second interpreter keeps notes, one line for every phrase. Before each Hindi word she glances back at her notes, and she looks hardest at the line that matters right now. She never has to remember the whole speech. She only has to know where to look.
The question. How can a machine translate a fifty-word sentence if only a handful of numbers may pass from the part that reads to the part that writes?
That was the trouble with the translator of Unit 17. Its encoder reads "I drink tea" one word at a time. After each word it writes a note — a short list of numbers: after "I", after "drink", after "tea". Then it hands the decoder only the last note, the summary. A five-word sentence and a fifty-word sentence must squeeze through the same few numbers. That squeeze is the bottleneck. It is the first interpreter, speaking from memory.
The fix (Bahdanau, Cho and Bengio, 2015) is the second interpreter. Keep every note. Before the decoder writes each word, it makes three small moves.
- Score every note. How well does this note match what I am about to write? The simplest score is the dot product (Unit 3) of the decoder's current state with the note.
- Turn the scores into shares with softmax (Unit 14). The shares are positive and add up to 1. They are called the attention weights.
- Blend the notes with those shares. The blend is the context vector. The decoder uses it, together with its own state, to choose the next word.
By hand. We keep the numbers tiny and make them by hand, so that you can see everything. Each note simply points at its own word: , , . (Real notes are long lists that mix many words together.) The decoder has written मैं (main, "I") and is about to write चाय (chai, "tea"). Its state — the decoder's own running note, as in Unit 17 — is, say, .
Almost three quarters of the attention goes to the note of "tea". The machine is about to write "chai", and it is looking at "tea". Nobody told it to — the scores did.
Do this for every Hindi word and stack the weights as rows. You get the alignment map: one row for each word written, one column for each word read. In "I drink tea" → "मैं चाय पीता हूँ" (main chai peeta hoon) Hindi puts the verb at the end. So the bright cells do not run straight down the diagonal. They bend: चाय looks at "tea", and then पीता (peeta, "drink") looks back at "drink". The last word, हूँ (hoon, "am"), has no single English partner, so it spreads its attention over "I" and "drink".
Two reasons. First, nothing is squeezed. A long sentence simply has more notes, and every note stays exactly as it was written. Second, the blame has a short road. When the decoder writes a wrong word, backprop (Unit 15) sends the blame back through the context vector straight into the notes it used — one hop, in proportion to their shares. In the relay, the blame for an early word had to crawl back through every step, and it faded on the way (Unit 17).
Three ways to score a note. The dot product is not the only way to ask "how well does this note match?". Here are the three classic choices side by side.
| score | formula | extra weights | in plain words |
|---|---|---|---|
| dot (Luong, 2015) | none | "how much do we agree?" — needs and of the same length | |
| general (Luong, 2015) | one matrix | first re-express the note with , then agree | |
| additive (Bahdanau, 2015) | a tiny one-layer network judges the match |
An additive score by hand. Take , , a decoder state and three notes , , . Both matrices are , so is just : add the state to each note: , , . Squash each entry with tanh: , , . Now simply adds the two entries: the scores are , and softmax turns them into the weights .
Rule of thumb. The dot product is the cheapest, and it is the one the transformer uses (with the of §4). Additive scoring came first. Like general, it copes when the decoder state and the notes have different lengths: its matrices first bring both to one size.
There is not one context vector for the sentence. There is a new one for every word the decoder writes: four Hindi words, four context vectors, four rows of the map. The notes stay the same; what changes is where the decoder looks.
Before writing word , score every note against the decoder's state , turn the scores into shares , and blend the notes into the context . Keep every note and look back, instead of carrying one summary and hoping it holds everything.
In the worked example you make the decoder's state for चाय twice as long: . What happens to the three attention weights?
A 30-word English sentence is translated into 35 Hindi words, with attention. How many context vectors does the decoder build, and how many numbers are in the alignment map?
If you want the algebra · 1 proof, step by step
Claim. With dot-product scores , shares and context , the slope of the context with respect to one note is . The blame reaches note directly, scaled by its own share — however long ago the note was written.
The road ahead. The unit has four acts.
- Look back instead of remembering (§1–§2): the decoder's look-back, and attention as a soft lookup.
- Every word asks every word (§3–§7): self-attention by hand, why we divide by , one word with many meanings, an attention lab where you type your own sentence, and many heads.
- Order and depth (§8–§9): three ways to tell the model where each word stands, and the transformer block.
- How transformers learn (§10–§14): the mask that trains every next word at once; BERT, GPT and the encoder–decoder; the price; and a tiny transformer walked end to end.
In one sentence: Instead of squeezing the sentence into one summary, the decoder keeps every note and, before each word it writes, scores the notes, turns the scores into shares and blends them — an interpreter glancing back at the speaker's notes, looking hardest where it matters now.
That was section 1. The rest of the unit opens when you unlock it.
13 more sections and the practice arena — 18 widgets, 31 checks and 16 solved problems in the whole unit (this preview had 1 widget and 2 checks).
- 2
Attention is a soft lookup
- 3
Self-attention: every word asks every word
- 4
Why we divide by the square root of d
- 5
The payoff: one word, many meanings
- 6
The attention lab: who looks at whom
- 7
Many heads: many questions at once
- 8
Where is each word? Three ways to add position
- 9
The transformer block
- 10
No peeking: the mask, and every guess at once
- 11
Three families: BERT, GPT and the encoder–decoder
- 12
The price: every pair of words
- 13
A tiny transformer, whole
- 14
What to carry forward
- 15
Practice arena — sixteen problems, solved in full
Unlock this unit for ₹299, or all seven paid units for ₹999 — one-time payment, full refund within 7 days. See pricing.