liveBook · Manning

Attention

As explained in Machines that Think

A neural-network mechanism that computes relevance weights over elements so a model can selectively use information pertinent to its current computation.

Attention lets a neural network selectively use information relevant to its current computation. Rather than carrying all context in a single evolving state, a model assigns different weights to available representations and combines their information. This helps it use relationships between distant parts of a sequence. section 3.5.5 section 3.6

In self-attention, learned projections turn each token representation into a query, a key and a value. A query represents what the token seeks; keys provide characteristics for judging relevance; values carry information to be combined. Comparing a query with keys produces relevance scores that determine how values contribute to a new context-dependent representation. section 4.1.3

Figure 4.5 The model turns each token’s representation into three vectors: a query that expresses what it seeks, a key that describes characteristics used to assess relevance, and a value that carries the information it can contribute. Each is produced by its own learned projection. By comparing queries and keys, the attention head produces an attention pattern that reflects how strongly tokens are related in the model’s internal representation, with darker cells in the matrix indicating stronger connections. The values are then combined according to this pattern, giving each token a new representation, known as a hidden state, that integrates contextual information from across the sequence.

The diagram follows learned projections from token representations through relevance patterns to contextualized representations. section 4.1.3

Interpreting a token

In “The sun rises in the east,” the query for “rises” can match the key for “sun” more strongly than the key for “the.” The value associated with “sun” then contributes more to the contextual representation of “rises.” section 4.1.3

Transformers make attention a central organizing mechanism, allowing direct interactions between tokens and parallel sequence processing. Multi-head attention provides several concurrent views of an input. Attention itself has no inherent sense of token order, so positional information must also be supplied. section 4.1.1 section 4.1.2 section 4.8

Cross-attention connects different sets of representations. In an encoder–decoder Transformer, the decoder supplies queries while the encoder supplies keys and values; the decoder’s own attention remains constrained to avoid future output tokens. Cross-attention can also let a textual prompt selectively query visual representations in a multimodal model. section 4.1.7 section 4.5.3

Access is not the same as reliable use. Attention can connect tokens in the active sequence, but information outside it must be supplied as input to be directly available. Even information inside a long context may have diminished influence: studies have found that answers placed in the middle can be used less reliably than answers near the edges. The term “attention” describes a computational mechanism, not evidence of human-like attention. section 6.4.1 section 6.4.3 section 5.1.2

How Machines that Think covers it

How deep it goes
Explains learned queries, keys, values and contextual combinations, then distinguishes self-attention from cross-attention.
Examples
Uses “The sun rises in the east” to illustrate token relevance and examines answer placement in long contexts.
What you can do after
Evaluate/understand how attention combines context and why available information may not reliably influence an answer.
How it connects
Connects attention to Transformers, positional encoding, multi-head attention and cross-attention.

Also explained in

Compare how 40 books explain attention →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Machines that Think. The section links point to the parts of the book it draws on.