Chapter 10: Attention from First Principles
Attention was not invented for language models. It was invented for translation, as a fix for a specific failure: an encoder-decoder that squeezes a whole source sentence into one fixed-size vector forgets the start of a long input by the time it writes the end. This chapter retraces that history in code. You build a sequence-to-sequence model trained with teacher forcing, then Bahdanau’s additive attention and Luong’s dot, general, and concat scores with input feeding, then a beam search that works over any step function, and finally the metrics that grade generated sequences: exact match, BLEU, and chrF.
Concepts from other sections (12)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- teacher forcing: feeding the true previous target token at every training step. Read more: 10.1 Encoder-decoder with teacher forcing
- concat score: , Bahdanau’s network with one matrix on the concatenation. Read more: 10.3 Luong attention and input feeding
- input feeding: Luong’s practice of feeding the previous attentional state into the next decoder input. Read more: 10.1 Encoder-decoder with teacher forcing
- beam search: keeping the best prefixes by summed log-probability, ranking all extensions together at each step. Read more: 10.4 Beam search, generic over a step function
- exact match: the fraction of hypotheses equal to their reference after stripping white space at both ends. Read more: 10.5 Sequence metrics: exact match, BLEU, chrF
- chrF: a recall-weighted F score over character n-grams of orders 1 to 6, white space removed. Read more: 10.5 Sequence metrics: exact match, BLEU, chrF
- scheduled sampling: training with probability of feeding the true token and of feeding the model’s argmax. Read more: 10.1 Encoder-decoder with teacher forcing
- Padding mask: a per-step boolean that freezes a finished sequence’s state and zeros its output. Read more: 9.2 LSTM (torch gate order)
- greedy decoding: taking the single most likely token at every step; a beam of size 1. Read more: 10.4 Beam search, generic over a step function
- attentional state: , what the output layer reads. Read more: 10.3 Luong attention and input feeding
- dot score: , the parameter-free score between a query and a key of the same width. Read more: 10.3 Luong attention and input feeding
- sufficient statistics: the per-sentence count vectors whose column sums determine the corpus score. Read more: 10.5 Sequence metrics: exact match, BLEU, chrF
The arc matters for the rest of the book. Attention here is already the operation the 2017 transformer keeps: a score per source position, a softmax, a weighted sum. The transformer simply drops the recurrence around it. And the separation you build at the end, a model that is only a step function and a search that knows nothing else about it, is how production decoders are structured too.
Three ideas carry the chapter:
- The bottleneck. A fixed-size encoder state cannot hold a long sentence; attention lets each decoder step read a weighted average of all encoder states.
- Scores to weights. A score per source position, a softmax, a weighted sum; the 2017 transformer keeps exactly this and drops the recurrence.
- Search is separate from the model. Beam search keeps the best prefixes by summed log-probability, with length normalization.
Learning objectives
- Build an encoder-decoder with a bridge, train it with teacher forcing and scheduled sampling, and show that attention beats the bottleneck on the same budget.
- Implement Bahdanau and Luong attention with exact padding masks, and explain where each reads the source.
- Implement beam search generic over a step function, and show by hand a case where it beats greedy decoding.
- Compute exact match, BLEU, and chrF exactly as sacreBLEU does, with a bootstrap interval.
Before you begin
Section titled “Before you begin”You should have built the recurrent layers of Recurrent networks, in particular the GRU, the LSTM, and the bidirectional RNN, on top of the tensor, op, and module foundations of Foundations. The metrics section uses the bootstrap from LLN, CLT, confidence intervals, bootstrap in Mathematics; its problem set is good practice before you start.
In this chapter
Section titled “In this chapter”- Encoder-decoder with teacher forcing: an encoder, a bridge, and a decoder written as one step function, trained with teacher forcing or scheduled sampling, with or without attention. It ends with the experiment that motivates the chapter: on the same budget, attention reverses digit strings almost perfectly and the bottleneck model does not.
- Bahdanau additive attention: the first attention mechanism, a one-hidden-layer score per source position, with padding masked to on the scores and the key projection computed once per sentence.
- Luong attention and input feeding: the
dot,general, andconcatscores, attending after the recurrent step, and feeding the attentional state back into the next input. The unscaled dot score is one step from the transformer’s. - Beam search, generic over a step function: keeping the best prefixes, ranking all extensions together, gathering any model state by parent, and normalizing by length.
- Sequence metrics: exact match, BLEU, chrF: the metrics that grade generated text, built from sufficient statistics to match sacreBLEU exactly, each with a bootstrap interval.
Further reading
Section titled “Further reading”- Bahdanau, Cho, and Bengio, Neural Machine Translation by Jointly Learning to Align and Translate; Luong, Pham, and Manning, Effective Approaches to Attention-based NMT.
- Papineni et al., BLEU (ACL 2002); Post, A Call for Clarity in Reporting BLEU Scores (sacrebleu).