cd .. / course
Part 1 · Lesson 6/15

How attention actually computes

Click a query token and watch the whole pipeline run on real numbers: dot products become raw scores, softmax turns them into weights that sum to 1, and the Values blend into a brand-new vector. Nothing here is looked up — every number falls out of arithmetic you can trace.

⏱ ~7 min 🎚 the full pipeline, computed math: some

Last page we built three vectors from every token's embedding: a Query (what it's looking for), a Key (what it advertises), and a Value (the content it hands over). Now they get used.

To decide how much token A should listen to token B, take A's Query and B's Key and dot them together: q·k. A bigger dot product means the two vectors point the same way — a better match, a higher raw score. Those raw scores are the logits of the attention softmax. (These are attention-logits — one score per token in the sentence. Later, next-token prediction produces vocab-logits — one score per word in the whole vocabulary. Same softmax, different lists; lesson 10 builds the second.)

Run all the scores through softmax and you get positive fractions that add up to 1 — the attention weights. This is the exact same softmax that turns next-token logits into probabilities — the one you'll dial with temperature on lesson 11. Finally, A's new vector is the weighted blend of every token's Value: a token literally becomes a mix of what it paid attention to.

The attention pipeline · computed live
query token: it — every number below is q·k → softmax → blend for this token
1
Raw scores — the logitsscore(j) = q · kⱼ (raw dot product, no scaling)
2
Softmax → attention weightsweight(j) = e^score(j) / Σ e^score → fractions that sum to 1
running sum of all weightsΣ = 100.0%
Same operation as next-token probability. These are the identical e^z / Σe^z fractions the sampling page (lesson 11) reshapes with temperature — here they weight attention instead of picking a word.
3
The weights, drawn as arcsarc thickness ∝ computed attention weight
4
Blend the Values into a new vectorout = Σⱼ weightⱼ · vⱼ (element-wise)

TRY → Click a different token (try The or tired) — all four stages recompute from scratch. Hit Run to animate the pipeline one stage at a time, or Step to walk it yourself. Watch the cat arc: for it it's always the fattest outward one.

Show me the actual numbers (query = it)

e_it = [0.8, 0.1, 0.2, 0.3]. Its Query is q = Wq·e_it = [2·0.8+1·0.3, 2·0.1, 1·0.2, 1·0.3] = [1.9, 0.2, 0.2, 0.3].

The seven raw scores are q·kⱼ for each token's Key (Keys have their function dim damped by ×0.3):

tokenq · k written outscoree^scoreweight
cat1.9·1.0 + 0.2·0.2 + 0.2·0.1 + 0.3·01.967.1035.8%
it1.9·0.8 + 0.2·0.1 + 0.2·0.2 + 0.3·0.091.614.9925.2%
tired1.9·0.3 + 0.2·0.1 + 0.2·1.0 + 0.3·00.792.2011.1%
sat1.9·0.2 + 0.2·1.0 + 0.2·0.1 + 0.3·00.601.829.2%
was1.9·0.1 + 0.2·0.5 + 0.2·0.4 + 0.3·0.060.391.477.4%
because1.9·0 + 0.2·0.1 + 0.2·0.2 + 0.3·0.270.141.155.8%
The1.9·0 + 0.2·0 + 0.2·0 + 0.3·0.30.091.095.5%

The e^score column sums to 19.83; each weight is that row's e^score divided by 19.83 (e.g. 7.10 / 19.83 = 0.358 = 35.8%). The seven weights add to 1.00.

Blending the Values (each token's v = Wv·e, dim 3 halved) with those weights gives out ≈ [0.62, 0.24, 0.25, 0.10] — animacy-dominant (from cat) with a state bump (from tired).

The √d note: real transformers divide each score by √(key-dimension) before softmax to keep the numbers from getting too large — with d = 4 that's ÷2. It changes the sharpness of the weights but not which token wins. We skip it here so the arithmetic stays clean.

MythThe attention weights are looked up in a table or hand-set by the designer.
RealityThey're computed every single step — dot products of Queries and Keys, run through softmax. Change one vector and every weight downstream changes. (This widget just proved it: edit the toy data and the arcs re-draw.)
Takeaway

Attention is three ops in a row: Q·K dot products → softmax → weighted blend of Values. The output vector for a token is literally a mixture of the tokens it scored highest.

An attention weight of 0.36 from it to cat means…
Why: softmax'd Q·K scores are the fractions of each token's Value that get blended in; they sum to 1. It's a routing weight, not a next-word probability.