The Attention Mechanism · Session 2 · Mix = weighted blend of values

Attention: a token
reaches across the sentence.

A token scores every other token by alignment, focuses those scores into a soft distribution with softmax, then mixes the tokens' values in those proportions. This demo isolates that mechanism end-to-end on a real sentence — driven by GPT-2's actual attention across all 12 layers and heads, or by GloVe word vectors as a transparent stand-in. Click ⤢ Expand to blow the panel up for the room.

Query — what a token is looking for Key — what a token offers Value — what gets moved

AMix — a weighted blend of values

The point: the output at a position is the attention-weighted average of the value vectors, outi = Σj aij vj — a soft lookup. A causal mask blocks the future so a token can only read what came before it. With real GPT-2 weights, the same machinery produces recognisable head behaviours — previous-token heads, attention sinks, induction.
sentence
causal mask
attention matrix  aij = softmaxj(qi·kj/√d)
Try: click a token to make it the query; toggle the mask; then hit randomize WQ,WK — the pattern changes completely, because what counts as “relevant” is learned. Switch to GPT-2 (real) and slide layer/head, or jump to a famous head. GloVe mode uses WQ=WK=WV=I on real vectors — a transparent stand-in for learned projections

ΣPutting it together

Q Kᵀ is the score · softmax(·/√dk) is the focus · multiplying by V is the mix you've been driving above. One head does this once. Multi-head attention runs h of them in parallel on different learned subspaces, then concatenates and projects:

Different heads specialise — one tracks syntax, another coreference, another position. Slide through GPT-2's heads above to find them.

⚠What we're simplifying

Sources

Vectors: GloVe 100-d via npm wink-embeddings-sg-100d (MIT; Stanford GloVe, PDDL). Real attention exported from gpt2-small. Math: KaTeX. Runs entirely from this folder — no build, no network.