Worked examples — Transformers — attention mechanism, self-attention
Before anything, one reminder of what each symbol is, in plain words, so a first-time reader can follow every line below:
The scenario matrix
Every situation attention can hand you falls into one of these cells. The examples below are tagged with the cell(s) they cover.
| # | Case class | What is special | Covered by |
|---|---|---|---|
| A | Aligned query/key () | query points toward a key | Ex 1 |
| B | Opposed query/key () | query points away — small weight | Ex 1, Ex 2 |
| C | Orthogonal () | dot product zero — neutral | Ex 1 |
| D | Exact tie (all scores equal) | uniform weights, degenerate | Ex 3 |
| E | Zero query () | degenerate input | Ex 4 |
| F | Scaling effect (large ) | saturation vs healthy gradient | Ex 5 |
| G | Limiting case (score ) | softmax collapses to one-hot | Ex 6 |
| H | Full -token matrix | row-wise softmax on | Ex 7 |
| I | Real-world word problem | aerospace telemetry attends across time | Ex 8 |
| J | Exam twist (permutation) | shuffle inputs → what happens? | Ex 9 |
Example 1 — aligned, opposed, and orthogonal in one shot (cells A, B, C)

Step 1 — raw scores . , , . Why this step? The dot product measures alignment; look at the figure — the amber arrow points right, so it agrees with (+1), fights (), ignores (0).
Step 2 — scale by . , , . Why this step? Keeps the numbers in softmax's healthy range (parent Step 2).
Step 3 — softmax the row. Exponentials: , , . Sum . . Why this step? We need fractions that add to 1 so the output is a genuine average.
Step 4 — blend the values. . Why this step? This finally pulls in content, weighted by relevance.
Verify: weights sum ✓. The aligned key won the most weight (0.576), the opposed key least (0.140) — matching the forecast. Orthogonal sits in the middle, as it should for score 0.
Example 2 — negative scores don't mean negative weights (cell B)
Step 1 — exponentiate. . Why this step? is always positive, even for negative — this is exactly why softmax can never output a negative weight.
Step 2 — normalise. Sum . . Why this step? Softmax cares only about differences between scores, not their absolute sign.
Verify: all ✓, sum ✓. The least-negative score () got the largest weight (0.721). So the answer to the forecast: false — negative scores are fine.
Example 3 — the perfect tie (cell D, degenerate)
Step 1 — notice the symmetry. All exponentials equal . Why this step? Identical scores means the model has no reason to prefer any position.
Step 2 — softmax. for each. Why this step? The constant cancels top and bottom — proving softmax ignores an added constant.
Step 3 — output. : the plain average. Why this step? With uniform weights, "attention" degenerates into an ordinary mean.
Verify: ✓. Note and give the same result — softmax is invariant to adding a constant to every score.
Example 4 — the zero query (cell E, degenerate input)
Step 1 — scores. for every , whatever the keys are. Why this step? The zero vector is orthogonal to everything.
Step 2 — softmax of all-zeros. (here ). Why this step? All-equal scores → uniform weights (same mechanism as Ex 3).
Verify: ✓. A zero query doesn't crash — it just averages everything equally. Attention gracefully degrades to "look at all timesteps the same."
Example 5 — scaling saves the gradient (cell F)
Step 1 — unscaled softmax. , . Sum . . Why this step? One score dominates so hard the other is starved — nearly one-hot.
Step 2 — scaled softmax (). , sum . . Why this step? Bringing scores near variance 1 keeps both weights well away from 0/1, so gradients through both survive (avoids Gradient vanishing/saturation).
Verify: unscaled sum ✓, scaled sum ✓. The second weight jumps from to — a 5900× revival of its gradient. That is why we divide by .
Example 6 — the infinite-score limit (cell G, limiting behaviour)
Step 1 — write the weight. . Why this step? Divide top and bottom by so we can read the limit cleanly.
Step 2 — take . , so , . Why this step? This is the exact "one-hot" collapse: attention picks a single token and ignores the rest — the hard lookup that soft attention approximates.
Step 3 — check . Then , (the mirror case). Why this step? Covers both ends of the limit so no scenario is left unshown.
Verify: at , ✓, confirming the limit.
Example 7 — full attention matrix (cell H)

Step 1 — raw . Entry : Why this step? One matrix multiply gives all pairwise relevances at once — the parallelism attention is famous for.
Step 2 — scale by . Divide every entry by : row 3 becomes . Why this step? Same variance control as before, applied uniformly.
Step 3 — softmax row 3 (the interesting one). (twice), , sum . . Why this step? Each row must sum to 1 because it's token 3's own distribution over who to read.
Step 4 — output for token 3. . Why this step? Token 3, pointing diagonally, sensibly blends toward the diagonal value.
Verify: row-3 weights sum ✓. Rows 1 and 2 (scores ) are more spread than row 3, which self-peaks at — matching the forecast about spread.
Example 8 — aerospace telemetry across time (cell I, word problem)
Step 1 — exponentiate scaled scores. , , . Sum . Why this step? Softmax over the three source timesteps.
Step 2 — weights. . Why this step? The most-relevant past step dominates — but directly, with no RNN blurring over 60 steps.
Step 3 — pooled evidence. . Why this step? Token pulls in units of anomaly evidence from the pressure drop.
Verify: weights sum ✓; pressure-drop share (≈94%) ✓ — the model correctly "remembers" a cause 60 steps back. Contrast RNNs and LSTMs, where that signal would fade.
Example 9 — the permutation twist (cell J, exam trap)
Step 1 — see what a swap does to . Reordering rows/columns of the score matrix by permutation gives ; softmax and the value-blend follow the same . Why this step? Every operation (dot, softmax row-wise, sum) treats positions symmetrically — none looks at index order.
Step 2 — conclude. New outputs : the same three vectors, permuted identically to the input. This is permutation-equivariance. Why this step? It exposes that attention has no built-in order — the trap the exam is testing.
Step 3 — the fix. Add Positional Encoding to each input so identical tokens at different positions get different vectors, breaking the symmetry. Why this step? Without it, "engine spun up then failed" and "failed then spun up" look identical.
Verify: conceptual — swapping inputs by yields outputs by the same , values unchanged. No numeric claim, but confirmed by the structure in Ex 7 (the score matrix carries no index information beyond the vectors themselves).