Attention lets a token's representation combine information from other positions in the text. Imagine reading “The cup fell because it was unstable.” Interpreting “it” requires context. Attention provides a numerical way to mix context; it does not contain a handwritten rule that identifies pronouns.1
A token is a piece of input handled by the model; it may be a word or part of one. A vector is simply a list of numbers. This lesson follows one attention head, one of several parallel calculations inside a Transformer layer.
Attention: build a useful view of the context
Follow one token as it compares, weighs and combines information from earlier tokens.
AI-assisted learning visualization · simplified worked example
A token is represented by a list of numbers: a vector. Three learned transformations produce a query Q, a key K and a value V. The query is what this position uses to look for matching information.
The whole calculation in plain language
- Create three views. Learned transformations turn each token's current vector into a query (Q), key (K) and value (V). Think of Q as what this position looks for, K as what another position can match, and V as the information it can contribute. These are analogies for numerical vectors.
- Compare Q with K. Multiplying corresponding entries and adding the results produces a score. A scaling factor accounts for the number of entries in Q and K.
- Hide unavailable positions. In causal self-attention, a position can use itself and earlier positions. Later positions are excluded before the next step.
- Turn scores into weights. Softmax makes the allowed weights nonnegative and makes them sum to one. A higher score receives a larger share.
- Mix V. Multiply each value vector by its weight and add the results. The result is a new vector containing a weighted mixture.12
Read the diagram actively
Follow one position through the sequence. At the scoring step, ask which comparisons are allowed. At the weighting step, check that the displayed shares add up to approximately 100%; rounded labels can differ slightly. At the mixing step, notice that several positions can contribute at once.
For a separate tiny example, suppose two scalar values are 2 and 10. Weights of 75% and 25% produce 4: three quarters of 2 plus one quarter of 10. The lesson performs the same kind of mixture with lists of numbers.
What this example does and does not show
The vectors and scores are teaching examples, not measurements from a trained model. Position handling and the rest of the Transformer are simplified. The original Transformer combines multiple heads and adds further processing around attention.1
An attention weight is not the probability of the next word. It controls a contribution to an intermediate representation. Predicting the next token requires the rest of the model and a separate output over its vocabulary.1
Continue
Next: see why the KV cache keeps keys and values. For the surrounding model, read How LLMs Actually Work, or return to the visual learning path.
Sources
Footnotes
-
Vaswani et al., Attention Is All You Need, sections 3.2–3.4. ↩ ↩2 ↩3 ↩4
-
PyTorch, Scaled dot-product attention: reference computation and masking. ↩