Skip to content

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.

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 kk 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.

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.

  1. 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.
  2. Bahdanau additive attention: the first attention mechanism, a one-hidden-layer score per source position, with padding masked to −∞-\infty on the scores and the key projection computed once per sentence.
  3. Luong attention and input feeding: the dot, general, and concat scores, 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.
  4. Beam search, generic over a step function: keeping the KK best prefixes, ranking all extensions together, gathering any model state by parent, and normalizing by length.
  5. 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.