In this lesson
Attention, where tokens start talking
Inside the model
From text to the next token
Watch the same starting vector change across two contexts, then inspect one toy attention head from scores to output.
By the end, you can
- Distinguish the roles of queries, keys, and values in one attention head.
- Trace scores, mask, softmax, and weighted sum into the output vector.
Before you start: Tokenization, or how a model reads, Embeddings: meaning becomes geometry
“Eat an orange.” “Paint it orange.” You did not hesitate over the meaning. The course trace now brings the same target token - orange, #901, starting at [−0.68, 0.44] - into both phrases. Only its neighbors change. The question is visible and numerical: can a different context move the target’s output even though its own starting point stays fixed?
Observe one token in two contexts
Compare “eat an orange” with “paint it orange.” In the teaching fixture, the final orange begins with exactly the same vector in both phrases. Only the vectors around it change.
Predict the fate of orange
The lab reports the output as [x, y]. Bet on the direction of the change before revealing the numbers.
Your bet
Switching from “eat an orange” to “paint it orange,” what happens to orange’s output?
Nobody grades this. Commit, and let the instrument settle it.
Switch contexts: target token #901 starts from the same vector, but the mixture produces a different output.
Explore score sharpness
Teaching control: this is not generation temperature.
Scores
- orange compares with eat-0.481
- orange compares with an-0.240
- orange compares with orange0.464
Weights after softmax
- eat0.206
- an0.263
- orange0.531
Contextualized vector
orange′[0.09, 0.22]Σ weight × V
Inspect Q, K, V, and the full matrix
| Token | Q | K | V |
|---|---|---|---|
| [1.00, 0.00] | [1.00, 0.00] | [1.00, 0.50] | |
| [0.50, 0.00] | [0.50, 0.00] | [0.50, 0.25] | |
| [-0.68, 0.44] | [-0.68, 0.44] | [-0.46, 0.10] |
| Token | Weights after softmax × V | Contextualized vector |
|---|---|---|
| eat | 0.206 × [1.00, 0.50] | [0.21, 0.10] |
| an | 0.263 × [0.50, 0.25] | [0.13, 0.07] |
| orange | 0.531 × [-0.46, 0.10] | [-0.24, 0.05] |
score = (Q · K) × sharpness / √2
| Q ↓ / K → | eat | an | orange |
|---|---|---|---|
| 1.000 | 0.000 | 0.000 | |
| 0.544 | 0.456 | 0.000 | |
| 0.206 | 0.263 | 0.531 |
Every cell shows its exact value; the blue surface redundantly represents its weight.
Manipulate how tokens look around
Keep orange selected and switch contexts. Its starting vector is unchanged, but the values available to the mixture differ, so its output moves. These numbers are hand-authored to expose the mechanism; they are not evidence that a trained head would discover this exact linguistic pattern.
Next, select the first token with the causal mask enabled. Later positions become unavailable. Disable the mask and watch those weights reappear. Finally, open Explore score sharpness and move its slider: the higher the value, the more the largest score dominates; the lower it goes, the more evenly the weights spread. This control is a teaching lens, not the sampling-temperature control used during text generation; standard Transformer attention normally uses the fixed 1 / √dₖ scale.
Challenge Two missions. One: use score sharpness to give orange its largest possible share in this lab, and read its weight. Two: with the causal mask on, one of the nine weights in the matrix is structurally forced to equal 1, whatever the context. Find it, and say why.
Explain the Q, K, V library
Self-attention is one mechanism that can make the two occurrences diverge. It builds a new vector for each position by mixing information from the available context - multiplications, percentages, and bookkeeping rather than mind reading.
- Same lookuporange · #901 · [−0.68, 0.44]
- Compare Q with Kscores → weights
- Mix the V vectorscontextualized output
In a trained Transformer, three learned projections turn every input vector e into:
- a query
Q: what this position is looking for; - a key
K: how this position can be found; - a value
V: the information it contributes if selected.
A library, in short. You arrive with a question (the query), compare it against the labels on the folders (the keys), and leave with a blend of contents (the values): a lot from the folders that match well, a little from the rest. Softmax is the accountant of the story: it converts affinities into shares of an attention budget that add up to exactly 100%. The output is no longer an isolated lookup: it is a contextualized vector.
- Each token’s vectore
- Three projectionsQ · K · V
- Q meets every Kscores + mask
- Softmaxweights, Σ = 100 %
- Weights × V, summedcontextualized output
Open “Inspect Q, K, V” to trace every stage of this path - projected vectors, scores, weights, and the final weighted sum - in the lab’s exact numbers.
The same story, in formulas
For one query, every key receives the score Q · K / √dₖ, where dₖ is the key dimension. The √dₖ factor keeps dot products from growing too large as that dimension grows. The causal mask then replaces forbidden scores with −∞.
Softmax turns the remaining scores into positive weights summing to 1:
softmax(sᵢ) = exp(sᵢ − max(s)) / Σⱼ exp(sⱼ − max(s))
Subtracting the maximum preserves the proportions while avoiding enormous exponentials. Finally, the head computes Σ weightᵢ × Vᵢ.
An attention weight is not a complete explanation. It describes one mixture at one place, in one head and layer. Final behavior also depends on other heads, later layers, residual paths, and nonlinear transformations.
Reflect on where the information travels
Checkpoint
After Q·K scores, masking, and softmax produce the weights, what gets multiplied by those weights and added into the output?
Connect the loop
We started with text, split it into tokens, looked up a starting vector for each token, then used attention to make those vectors context-sensitive. A Transformer combines this operation with position information, multiple heads, residual paths, normalization, feed-forward layers, and many repeated blocks before producing logits, probabilities, and a next token. The mechanism you manipulated survives intact in the full model, but it is only one piece of the computation - and the very next chapter walks one complete block, position vectors, MLP, residuals and all. As for the cat from chapter 2, it can doze but not retire: you can now trace its first transformations without calling any of them magic, and the rest of the tower is waiting.
Sources and scope
- Vaswani et al. (2017) defines scaled dot-product attention, causal masking, multi-head attention, and the Transformer architecture.
- Jain & Wallace (2019) shows why attention weights alone should not be treated as a complete explanation of model behavior.
- The lab is one handcrafted 2D head. Its score-sharpness slider is pedagogical and is not generation temperature.
- Content and claims reviewed on 2026-07-18.