liveBook · Manning

Attention

As explained in How Large Language Models Work

Attention is a transformer mechanism that computes context-dependent relationships by matching queries with keys and combining the corresponding values.

Attention is a transformer mechanism that focuses computation on the most relevant earlier tokens. It behaves like a fuzzy dictionary: a query compares with many keys, weights their similarity, and combines the corresponding values. section 3.2.2

When generating the next token, attention works roughly like this:

  • Treat the current token as a query and previous tokens as keys. section 3.2.2
  • Compare the query with every key and assign greater weight to better matches. section 3.2.2
  • Combine the values according to those weights to produce an output used for token prediction. section 3.2.2

Dictionary lookup, made fuzzy

A Python dictionary needs an exact key or returns nothing. Attention instead returns a weighted result from the most similar keys, so every query receives an approximate response.

How How Large Language Models Work covers it

How deep it goes
Traces queries, keys, weights, and values, but defers the detailed mathematics.
Examples
Uses a Python dictionary analogy and a next-token prediction diagram.
What it assumes
Assumes basic familiarity with tokens and embeddings while defining queries, keys, and values through dictionary behavior.
What you can do after
Evaluate how context influences transformer token predictions without treating attention as exact lookup or guaranteed reasoning.
How it connects
Connects attention with transformers, embeddings, positional encoding, autoregressive models, and token sampling.

Also explained in

Compare how 28 books explain attention →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of How Large Language Models Work. The section links point to the parts of the book it draws on.