Attention: a computed weighted mixture
Queries and keys determine weights; those weights combine values from permitted positions.
In this lesson
Attention is a numerical routing operation. Its weights are not a complete account of why a model answered.
Queries, keys, and values
For each position, learned projections produce query, key, and value vectors. A query is compared with eligible keys to produce scores. Those scores become normalized weights, which mix the value vectors. The names are helpful roles, not separate stores of facts or human questions. The documented scaled dot-product operation makes each step explicit.
Imagine three index cards, each carrying a key vector and a value vector. A query chooses a weighted combination of their values. No card must be selected exclusively. A weight of 0.6 means sixty percent of this particular weighted mixture, not sixty percent confidence that a sentence is true.
From scores to weights
In scaled dot-product attention, a score is q·k divided by the square root of the key dimension. A mask excludes disallowed positions before softmax. The surviving weights sum to one for each query, then the output is the sum of weight × value across eligible positions.
Take invented scalar values [2, 10] and weights [0.75, 0.25]. Their weighted output is 0.75 × 2 + 0.25 × 10 = 4. The output is neither an index nor the most-attended value. This small calculation is the core of attention’s mixing behavior, even when the real vectors have many dimensions.
Attention makes a weighted mixture.
Show the vector calculation
First coordinate: 0.75 × 2 + 0.25 × 0 = 1.5. Second coordinate: 0.75 × 0 + 0.25 × 4 = 1.0. The two attention weights add to 1.
The diagram is a calculation
The attention demo derives its matrix from stated small inputs. Changing a query or a mask must change the scores and normalized weights coherently. A triangular masked region means those entries contribute zero; it is not an artistic pattern or a sample from a production model.
Inspect a single row first. Which positions are eligible? Which key matches the query most strongly under the stated dot product? Do the visible probabilities sum to one within rounding? Then inspect the weighted output. This sequence connects the heatmap to the arithmetic behind it.
What weights cannot tell you alone
A large weight does not by itself establish the causal importance of a token to the final answer. Values, output projections, other heads, later layers, and residual paths all matter. Research has challenged treating attention distributions as self-sufficient explanations. Jain and Wallace, 2019.
For example, a heavily weighted value could be nearly zero, while a smaller weight multiplies a large informative vector. Even that comparison omits later transformations. Use attention plots to study one operation, and use additional experiments when making claims about the model’s reasoning or the influence of an input.
Weighted values, not weighted words
With two scalar values 2 and 10 and weights 0.75 and 0.25, attention returns 4. If both values become zero, it returns zero even though the attention weights have not changed. The weights alone therefore do not determine the information carried.
See attention add up
A small numerical attention calculation, not a window into reasoning.
The brightest attention cell explains the model’s reasoning.
It shows one mixing coefficient within a much larger computation. A complete explanation requires more evidence.
Take it with you
- Queries and keys produce scores; values supply what is mixed.
- Masks remove positions before normalization.
- Attention weights are not truth scores or complete explanations.
Check your understanding.
Choose an answer, then see the explanation. You can try again.
Practice only. Your answer is not stored or sent anywhere.
Causal masking: no peeking at the answer
Continue when you’re ready. Marking this lesson as read is optional.
Sources & further reading
Primary references for this lesson. The explanation and examples above are original.
- Scaled dot-product attention — PyTorch 2.15 documentation ↗PyTorch contributors · Version 2.15; accessed 2026-11-13 · checked 13 Nov 2026
- Attention is not Explanation ↗Sarthak Jain, Byron C. Wallace · 2019-05-08 (v3) · checked 13 Nov 2026