10  Attention

Recurrent networks summarize a variable-length sequence in a fixed-dimensional state. This state can become a bottleneck when later predictions require specific information from earlier positions. Attention instead retains a representation at every position. A query is compared with a key for each position, the comparison scores are normalized into weights, and the output is a weighted average of the corresponding values. The terms query, key, and value emphasize the connection to database lookup, while the weighted average makes the operation differentiable.

Attention was introduced in neural machine translation to let a decoder access all source representations rather than a single sentence vector (Bahdanau et al. 2015). The Transformer later removed recurrence and used attention as its primary sequence-mixing operation (Vaswani et al. 2017); that architecture is developed in Chapter 11. Attention is now used in models for text, images, speech, and biological sequences. This chapter studies the mechanism itself: its algebra, positional information, computational cost, and the circuits that trained attention layers can implement.

The chapter follows six dependencies. A soft lookup first defines queries, keys, values, and normalized weights. Learned scoring functions replace fixed kernels, and multiple heads provide several independently projected lookups. Because this operation is permutation equivariant, sequence models must then represent position explicitly or through a causal mechanism. The resulting all-pairs interaction has quadratic cost, which motivates exact memory-saving, sparse, and kernelized alternatives. Finally, an attention-only model provides a controlled setting in which attention patterns and value transformations can be analyzed together.

The experiments use a small character-level attention-only language model. Complete Transformers add feed-forward layers, normalization, and a broader training procedure; Chapter 11 develops those components. Optimizer details appear in Chapter 9, and Chapter 12 develops the recurrent and state-space side of the linear-attention correspondence.

Resources and Further Reading

These references cover the mechanism, its computational cost, positional representations, and circuit analysis.

Visual introductions

Foundational papers

The cost of attention

What attention computes

Exercises