liveBook · Manning

Attention

As explained in Grokking AI Algorithms, Second Edition

Attention is a neural-network mechanism that weights relationships among input representations so relevant information contributes more strongly to a computation.

Attention lets a neural network decide which parts of its input matter most to a particular part. In a Transformer, self-attention gives each token a way to weigh other tokens when building a context-aware representation. Think of reading with a highlighter: when you reach “tired” in “Alice was beginning to get very tired,” you look back to “Alice” for who is tired and “very” for how tired. section 11.17

Why use it? An RNN compresses preceding text into a fixed-size memory vector, which can lose earlier context. A CNN detects local word patterns but is less suited to long-range meaning. A Transformer processes a sequence simultaneously; self-attention lets tokens weigh one another. Because that operation alone does not identify word order, positional encodings add position information to token embeddings. section 11.11 section 11.15

For one attention head, the computation follows four steps:

  • Project each input embedding through three learned weight matrices to produce a Query (Q), Key (K), and Value (V). A Query represents what a token seeks, a Key helps identify a match, and a Value supplies what a matching token contributes. section 11.18
  • Compare each Query with the Keys using dot products, then scale the scores by the square root of the dimension. section 11.21
  • Apply softmax to turn the scores into attention weights. section 11.21
  • Multiply each Value by its weight and add the results. The resulting context vector blends information from the tokens receiving attention. section 11.21

A small context-vector calculation

In a 32-token, four-value-wide example, one token assigns weights of 0.0632 to token 0 and 0.4064 to token 31. Multiplying every token’s Value vector by its weight and summing produces the first token’s context vector: [0.2283, 2.1990, 0.4526, 1.9747]. The larger weight makes token 31’s Value contribute more strongly to that sum. section 11.21

Figure 11.34 An analogy of the purposes of Query, Key, and Value
11_34

The ingredient analogy separates what a Query seeks, what a Key advertises, and what a Value contributes. section 11.18

A head offers one perspective on token relationships; multiple heads can notice different patterns. Stacked Transformer blocks repeat attention with their own learned weights, refining representations before next-token prediction. Attention also connects language to images: in a U-Net, cross-attention uses image features as Queries and text embeddings as Keys and Values, helping image regions respond to relevant prompt words. section 11.17 section 11.28 section 12.29

How Grokking AI Algorithms, Second Edition covers it

How deep it goes
Works through learned projections, scaled scores, softmax weights, and weighted context vectors for one attention head.
Examples
Traces a reading-highlighter analogy, an ingredient analogy, and a 32-token context-vector calculation.
What it assumes
Introduces token embeddings and positional encodings before applying self-attention; explains Query, Key, and Value roles with an analogy.
What you can do after
Build it from scratch with a learning-oriented Python function that projects embeddings, calculates weights, and blends Values.
How it connects
Connects self-attention with Transformers, positional encodings, attention heads, and cross-attention in U-Nets.

Also explained in

Compare how 28 books explain attention →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking AI Algorithms, Second Edition. The section links point to the parts of the book it draws on.