Visual walkthrough — Transformers — attention mechanism, self-attention
We work a running toy example the whole way: three telemetry snapshots from an engine — call them token 1, token 2, token 3 (imagine: temperature reading, pressure reading, rpm reading at three moments). Everything stays 2-dimensional so we can literally draw the vectors on the board.
Step 1 — Start with the raw inputs (what is a "token vector"?)
WHAT. We place three little arrows on the board, one per snapshot. Each arrow is a point in a flat plane — two numbers, so two axes.
WHY. Before any clever machinery, attention needs something to compare. The comparison will happen between arrows, so we must first see the arrows. Nothing is learned yet; these are the plain inputs.
PICTURE. Look at the three chalk arrows below. They point in different directions — that difference in direction is the only thing attention will care about in the next steps.

Step 2 — Split each input into three roles: Query, Key, Value
WHAT. Each single input arrow fans out into three arrows living in their own copies of the plane.
WHY. One number cannot play two contradictory jobs at once. "How relevant are you to me?" (matching) and "what information do you carry?" (content) are different questions — so we give each token a separate arrow for searching (), for being found (), and for being read (). This is exactly the search-request-vs-label idea.
PICTURE. Below, input (chalk white) splits into (yellow), (blue), (pink). Same origin, three destinations.

Step 3 — Measure similarity with the dot product
WHAT. For token 1's query, we compute one score against every key: .
WHY THIS TOOL (dot product, not something else)? We need a single number that is big when two arrows point the same way and small (or negative) when they don't. The dot product does exactly that: , where is the angle between them. Same direction ⇒ ⇒ large score; perpendicular ⇒ ⇒ zero; opposite ⇒ negative. No other simple operation reads "alignment" so cheaply. See Dot product & cosine similarity.
PICTURE. The angle between and each key controls the score. Notice the aligned pair (small angle) gets the fat score bar; the perpendicular pair gets ≈0.

Step 4 — Scale the scores by
WHAT. Divide every raw score by the same constant . In our toy example , so we divide by .
WHY. A dot product adds up little products. Add more terms and the sum swings wider — its spread grows like . If scores get huge, the next step (softmax) turns into a hard "winner takes all" that has flat gradients everywhere else — the model stops learning. See Gradient vanishing/saturation. Dividing by pulls the spread back to ≈1, keeping the next step responsive.
PICTURE. Two score profiles for the same directions: raw (tall, spiky) vs scaled (shorter, gentler). The spiky one is heading for saturation.

Step 5 — Turn scores into weights with softmax
WHAT. Convert token 1's three scaled scores into three non-negative numbers that add to 1.
WHY THIS TOOL (softmax, not "divide by the total")? We want a weighted average next, so the weights must be non-negative and sum to 1 — a probability spread over positions. Plain scores can be negative; softmax's exponential kills negatives (they map to small positives) and smoothly amplifies the leader while staying differentiable so gradients can flow. See Softmax function.
PICTURE. Three scaled scores go into the softmax box; out come three weight bars whose heights stack exactly to 1.

Step 6 — Blend the Values (the soft lookup)
WHAT. Stretch each value arrow by its weight and add them tip-to-tail. The landing point is .
WHY. Only now do we pull in actual content. Steps 3–5 decided how much to trust each token; this step finally reads them, most strongly the best match. This is the "soft, differentiable database lookup": no hard pick, a smooth blend.
PICTURE. scaled by 0.67 plus scaled by 0.33 — the pink resultant leans toward but is nudged by .

Step 7 — Stack every token: the whole thing is two matrix multiplies
WHAT. Do Steps 3–6 for all tokens simultaneously by replacing "one query dotted with all keys" with "matrix times matrix ."
WHY. This is the whole selling point over RNNs and LSTMs: every position reads every other position in one parallel matrix product — no step-by-step loop, no long-range forgetting.
PICTURE. The attention grid: row = asker, column = asked. Bright cells = high weight. Each row sums to 1.

Step 8 — Edge & degenerate cases (so no scenario surprises you)
WHAT / WHY. A derivation is only trustworthy if it survives the extremes. Four cases:
- All scores equal (queries match nothing in particular). Then every : attention becomes a plain average of all values. Nothing breaks — it degrades gracefully to "look at everyone equally."
- One score dominates (near one-hot). Softmax → ≈; output ≈ that single value. This is hard lookup as a limiting case, and is exactly what Step 4's scaling keeps us from hitting too early.
- Single token (). is ; softmax of one number is ; output is just . Self-attention on a lone element returns that element's value — sensible.
- Negative / opposite arrows. gives a small (but positive!) softmax weight — never zero, never negative. So a token is never fully ignored, just heavily down-weighted.
PICTURE. Three weight profiles side by side: uniform (case 1), spiky (case 2), and the single-token bar (case 3).

Recall One more edge case: order-blindness
Shuffle the input tokens and the output rows shuffle identically — attention has no built-in sense of time or order. That is why real Transformers bolt on Positional Encoding before this whole machine runs.
The one-picture summary
Everything above, compressed into a single flow: raw arrows → split into Q/K/V → dot products → scale → softmax → weighted sum. Follow the arrows left to right.

Recall Feynman retelling of the whole walkthrough
Picture a small group of engine snapshots on a chalkboard, each a little arrow. Give every arrow three jobs: a search request (query), a name tag (key), and a fact sheet (value). To decide who each snapshot should listen to, compare its search request to everyone's name tag by seeing how much the arrows point the same way — that's the dot product. Those raw match numbers can get wildly big when the arrows are long, which would make the next step panic, so we shrink them by to keep them calm. Then we run them through softmax, which turns them into a fair set of percentages that add up to 100%. Finally, each snapshot builds its answer by mixing everyone's fact sheets in those percentages — mostly the best match, a little of the rest. Do that for every snapshot at once with two matrix multiplies, and you have the whole attention formula. The only things ever learned are the three projection matrices; the attention percentages are recomputed fresh for every new input. And because it treats the sequence as a bag, we must separately tell it the order — that's positional encoding. See Multi-Head Attention for running several of these lookup strategies in parallel, and Time-series anomaly detection (aerospace telemetry) for where a distant sensor spike gets pulled in with no information loss.