1. The playground
The four tokens are the, cat, sat, and mat. Select one token to use as the query. The orange arrow is its query vector; the cyan arrows are the keys. Click and drag in the plane to move the selected query or key, or use the four sliders for the same adjustment.
The table below the canvas shows every step: the raw dot product, the scaled score, the softmax weight, each value vector, and the weighted contribution. The green arrow is the output, the weighted sum of the values.
Causal mask hides keys that come after the selected token. The remaining weights renormalize, which is how a decoder prevents itself from reading the future.
2. What to notice
- The query decides the row. Changing the selected token changes which query is compared with all four keys.
- Keys score, values mix. Moving a key changes the scores. The values are fixed; the output is a weighted blend of them.
- Scores are not weights. A raw score can be negative or larger than 1. The weight is the softmax of the scaled score.
- Weights sum to 1. The output is a convex combination of the value vectors, so it stays inside the region spanned by them.
- Scaling matters. The scores are divided by √2 because the vectors are two-dimensional. Without scaling, the softmax would be sharper.
- The output is not the query. It lives in value space. A high score on a key does not mean the output equals that key.
3. The math
For the selected query q and each key k_j, the scaled score is:
Softmax turns the scaled scores into weights:
The output is the weighted sum of the value vectors:
Every value in the table is computed from the vectors. Nothing is copied from a fixed answer key. The playground uses two-dimensional vectors so the geometry is visible; a real attention head uses many dimensions and several heads in parallel.
4. The causal mask
A language model predicts the next token, so it must not see the future. The causal mask sets the score for every key after the current position to −∞:
Softmax then gives those keys weight 0, and the visible weights renormalize to sum to 1. Select the first token and turn the mask on: only the first key remains visible. Select the last token: all four remain.
The mask is a property of the task, not of attention itself. Bidirectional encoders such as BERT do not use a causal mask; decoder-only language models do.
5. Related lessons
The attention lesson derives the score, scale, softmax, and weighted sum. The transformer lesson puts the same operation inside a full block.