15 / 37 · Neural Sequence Models
Attention before Transformers
At every decoder step, compare the decoder state with every encoder state, turn the scores into weights with a softmax, and feed the weighted sum of encoder states to the decoder; this removes the fixed-vector bottleneck, and an ablation on the translation task measures what it is worth.
On this page 10
The encoder–decoder of the previous note compresses the source into one fixed-size state before the decoder writes anything, and the outputs show the cost: fluent sentences about the wrong things. The decoder should be able to look back at the source as it writes, and to look at different parts of it for different target words. Bahdanau, Cho and Bengio (2015) added exactly this and called it attention. This note builds the mechanism, works one step by hand, and shows an experiment that measures what it buys.
Notation. Encoder states (one per source token; if bidirectional). Decoder state at step . Scores , weights , context vector .
Baseline: the final state as the only channel#
The decoder is initialized with and then runs on its own. There is no operation by which step of the decoder can read specifically. The states are computed and discarded.
The mechanism#
At decoder step :
- Score each encoder state against the decoder state. The simplest score is a dot product, ; it is large when the two vectors point the same way. Other choices exist (below), and whichever is used, the output is one number per source position.
- Normalize with a softmax over source positions: The weights form a distribution over the source: “how much of each source token to use now”.
- Aggregate into a context vector, the weighted sum of encoder states:
- Predict from the decoder state and the context together, for example by concatenating them before the output layer: , and feeding into the next decoder step as well.
Every operation is differentiable, so the scoring is learned by backpropagation from the translation loss; nothing tells the model which source word corresponds to which target word. The weights are recomputed at every step: can attend to the first source word and to the last.
Shapes for one step and a batch of : encoder states , decoder state , scores , weights , context . Over a whole target of length the weights form a tensor, which is what the heatmaps in the translation note display.
Worked example#
Three encoder states in two dimensions, , , , and a decoder state :
| 1 | |||
| 2 | |||
| 3 |
The denominator is . The context is . It is a point inside the triangle spanned by the encoder states, closest to the ones the decoder state resembled; nothing was selected, everything was mixed in proportion.
One attention step: what goes where
The figure runs the four steps of the mechanism on these numbers; the stepper under it shows them one at a time. Compared with the encoder–decoder figure, nothing about the encoder changes. What changes is the line from the encoder to the decoder: instead of one vector handed over once, the decoder reaches all the encoder states at every step, through weights it computes itself.
Scoring functions#
The dot product assumes and live in the same space and have comparable scale. Two generalizations are common (Luong, Pham and Manning, 2015):
| Name | Parameters | |
|---|---|---|
| dot | none | |
| general (bilinear) | ||
| additive (Bahdanau et al., 2015) |
The additive form was the original; the dot form is the cheapest and, with a learned projection of both sides, is what the self-attention note uses. Cosine similarity, which divides by the norms, is a possible score too but discards magnitude information that the model can use; the standard choice is the unnormalized dot product, rescaled by a constant in the Transformer.
Evidence: removing the bottleneck from a Transformer#
The clean comparison is not LSTM-with-attention versus LSTM-without, because the two models differ in several ways. A sharper experiment uses the one-layer Transformer of the translation note, whose decoder reads the encoder through exactly the mechanism above (there called cross-attention). Two versions are trained identically for 10 epochs on Multi30k:
| Decoder may attend to | Test BLEU |
|---|---|
| all encoder states | 0.320 |
| only the first encoder state (an artificial bottleneck) | 0.190 |
| LSTM encoder–decoder, no attention (previous note) | 0.152 |
Restricting the decoder to one encoder vector costs 0.13 BLEU and brings the Transformer down near the LSTM baseline. Everything else (self-attention inside the decoder, the feed-forward layers, the embeddings) is unchanged, so the difference is attributable to the decoder’s access to the full set of source states. The bottleneck is the problem, and attention is its removal.
The learned weights are interpretable in this task. Plotted as a matrix with target tokens as rows and source tokens as columns, the one-layer model’s cross-attention is close to diagonal, with each English token attending to the German token after the one it translates, because the model is predicting the next token and the aligned source word for the next output is the most useful thing to read.
What attention gives#
- No fixed-size summary. The decoder reads vectors, however long the source is. Long sentences are no longer compressed harder than short ones.
- Alignment for free. The weights are a soft word alignment, learned from translation pairs alone.
- A short gradient path. The loss at target step reaches encoder state through one weighted sum instead of through recurrent steps, which eases the vanishing problem of the RNN note.
What changed#
| Encoder–decoder | With attention | |
|---|---|---|
| Source information per step | , fixed | , recomputed |
| Encoder states used | only | all |
| Extra computation per step | — | scores, one softmax, one weighted sum |
| Extra parameters | — | none (dot) to (general, additive) |
| BLEU, Multi30k, 1-layer Transformer decoder | 0.190 (bottlenecked) | 0.320 |
Limitations#
- The encoder is still recurrent. The states are computed sequentially, so the encoder cannot be parallelized along the source, and each still carries the source only through a chain of recurrences.
- Attention is only between decoder and encoder. Within the source, token learns about token only through the recurrence; within the target, likewise. There is no reason the same scoring could not relate positions of one sequence to each other.
- Cost per step is . Fine for sentences; a concern for documents, and quadratic once every position attends to every other.
- Soft weights are not explanations. A near-diagonal alignment on a caption dataset does not generalize to a claim that attention weights show what the model “uses” (Jain and Wallace, 2019).
What you should remember#
- Attention at step : scores between the decoder state and each encoder state, softmax over , context , prediction from .
- The weights are a distribution over source positions, recomputed per output token and learned from the task loss.
- Dot-product scoring is the cheapest; additive and bilinear variants add parameters.
- Removing the decoder’s access to all encoder states costs 0.13 BLEU on Multi30k in an otherwise identical model.
- Attention removes the bottleneck but not the recurrence.
Notebook#
week04_lstm_attention_transformer.ipynb. Cell numbers count every cell, markdown included, from 1. Quoted numbers come from the run saved with the course; cells changed since then for current library versions have no saved output.
- Cells 58–64 define and train the one-layer
Seq2SeqTransformer(hidden size 128, 8 heads); the training cell has no saved output, and the markdown cell 64 states the test BLEU, 0.32. The translation note describes the model. - Cells 72–79 extract the decoder’s cross-attention weights with a forward hook (the
[32, 31, 32]tensor of batch × target × source weights, each row summing to 1) and plot them for six short test pairs; the near-diagonal, one-ahead pattern described above is visible in the saved figure. - Cells 81–86 define
Seq2SeqTransformerNoCA, whosedecodepasses onlyencoded_input[:, :1](the state of the first source token) as the encoder memory, train it the same way; the markdown cell 86 states its BLEU, 0.19.
Next#
Attention compared a decoder state with encoder states. Nothing in the mechanism requires the two sides to come from different networks: a sequence can attend to itself, each position scoring every other position and aggregating their values. Doing that removes the recurrence entirely, because every position’s new representation is a weighted sum computed in one matrix product, in parallel over the sequence. The next note writes self-attention in matrix form, with queries, keys and values, and the Transformer is built on it.
References#
- D. Bahdanau, K. Cho, Y. Bengio. Neural Machine Translation by Jointly Learning to Align and Translate. ICLR, 2015. Additive attention.
- M.-T. Luong, H. Pham, C. D. Manning. Effective Approaches to Attention-based Neural Machine Translation. EMNLP, 2015. Dot, general and concat scores; global versus local attention.
- S. Jain, B. C. Wallace. Attention is not Explanation. NAACL, 2019.
- D. Jurafsky, J. H. Martin. Speech and Language Processing, 3rd ed. draft. Chapter 13 (machine translation; the encoder–decoder with attention).