Worked examples — Attention visualization and limitations
This is a deep-dive companion to the parent topic. There we argued why attention is a tricky explanation tool. Here we compute — we take every kind of situation the topic can throw at you and grind through the numbers, so that when you meet a real heatmap you already know what each cell of the matrix means.
Before line one, three plain-word reminders so no symbol arrives un-earned:
We will need the softmax repeatedly. In plain words: softmax turns a list of raw scores into a list of positive fractions that sum to 1, and it does so by exponentiating (to force positivity) then dividing by the total (to force the sum-to-one).
We also meet one more symbol repeatedly, so let us earn it now:
The scenario matrix
Every situation this topic produces falls into one of these cells. The examples below are labelled with the cell(s) they hit, and together they fill the whole grid.
| Cell | What it stresses | Example |
|---|---|---|
| C1 — Basic single-head | one matrix, read a heatmap | Ex 1 |
| C2 — Softmax scaling / | why divide scores, what changing does | Ex 2 |
| C3 — Degenerate: uniform attention | all weights equal → says nothing | Ex 3 |
| C4 — Degenerate: value in null space | high weight, zero contribution | Ex 4 |
| C5 — Multi-head averaging trap | antagonistic heads cancel | Ex 5 |
| C6 — Attention rollout (limiting/deep) | multiply layers, add identity | Ex 6 |
| C7 — Attention vs gradient disagree | high attention, low gradient & vice-versa | Ex 7 |
| C8 — Adversarial rewrite | two different , same output | Ex 8 |
| C9 — Real-world word problem | sentiment sentence end-to-end | Ex 9 |
Forecast: Guess — will "not" (score 2.0) get roughly twice the weight of "good" (score 1.0)? Softmax is exponential, so the gap is bigger than "twice". Hold that thought.
- Exponentiate each score. , , . Why this step? Softmax forces positivity through ; large scores blow up faster than small ones — the exponential is what makes attention peaky.
- Sum them. . Why this step? This total is the denominator that turns scores into fractions summing to 1.
- Divide. . Why this step? Now each entry reads directly as "fraction of information from that token."
The figure below plots these three weights as bars, with each token's raw score printed underneath it. The point it makes visually is the gap-exaggeration: the "not" bar sits at while "good" sits at — a ratio of about 2.7×, even though the raw scores ( vs ) differ by only 2×. The extra stretch is the exponential at work; reading the raw scores alone would badly under-estimate how sharply the model concentrates on "not".

Verify: ✓ (a valid distribution — every row of an attention matrix must do this).
Forecast: Guess whether the unscaled version is more or less extreme. Bigger score gaps → more extreme softmax, so unscaled should nearly one-hot.
- Unscaled softmax of . The gap is ; dwarfs , so weight on the first token . Why this step? Shows the danger: in high dimensions raw dot products are huge, and softmax collapses to a single spike — the gradient through it dies.
- Scaled softmax of (, and ). Gap is : weight . Why this step? Dividing by keeps score magnitudes so softmax stays soft and trainable.
- Read the lesson. Same pattern (first > second), but scaling leaves a usable gradient (, not ). Why this step? is not cosmetic — it is why the model can learn.
Verify: (rounds to ) ✓.
Forecast: Guess: does uniform attention prove equal importance, or prove nothing?
- Compute the entropy (spread) of this distribution. With equal weights, entropy bits — the maximum possible. Why this step? Maximum entropy = the head is not selecting anything; it is the "I have no preference" output.
- Interpret. A maximally-uniform head has thrown away all discriminative signal. It could equally be (a) a genuinely useless head or (b) a head whose real work happens in the value vectors, not the weights. Why this step? Uniform weights are a degenerate input to any interpretation — the weights alone can't tell (a) from (b). You must inspect or gradients.
- Conclusion. Never read "importance" off a flat heatmap. Flatness = information absent, not "everything matters."
Verify: For 4 equal weights, entropy bits, and this equals ✓.
Forecast: Weight is — surely token dominates the output?
- The contribution the network sees is (weight) × (projection of the value onto what downstream reads): . Why this step? A weight scales the value, but the next layer only "feels" the part of the value along its own reading direction .
- Compute the projection. . Why this step? is orthogonal to — it lies in the null space of the downstream read. A dot product of zero means "invisible."
- Multiply. Contribution . Why this step? Despite the enormous weight, token contributes nothing to the output. This is the parent note's warning made concrete.
Verify: , so ✓. High attention ⇏ high influence.
Forecast: Will averaging reveal the signal, or erase it?
- Average element-wise. . Why this step? This is the naive "smooth out the noise" move the parent note warns against.
- Read the result. The average is perfectly uniform — exactly the degenerate Cell C3! The strong, opposite specializations cancelled. Why this step? Antagonistic heads produce a flat mean that hides both the signal and the confounder.
- The fix. Keep heads separate and use gradient attribution to find which head the classifier actually reads. Averaging destroyed the very structure we wanted.
Verify: and ✓ — a real signal averaged into meaningless uniformity.
Forecast: Layer 2 sends 0.5 of token-2's information back to token 1; layer 1 sends 0.2 of token-1 to token 2. Does the chain create influence token 1 → 2 that neither layer shows alone?
- Why multiply? If layer 1 routes and layer 2 routes , then influences through . Matrix product sums over every intermediate automatically: . Why this step? Composition of two routings = one matrix multiply; that's the whole idea of rollout.
- Compute row 2, column 1 of . Row 2 of is ; column 1 of is . Why this step? This traces the path: token 1 → (0.8, layer 1) → token 1's slot, then token 2 pulls 0.5 of it in layer 2.
- Interpret the limit. The cumulative influence is larger than the single-layer number and reveals a path no one layer's heatmap displays. Why this step? This is exactly why rollout exists — deep flow ≠ local attention.
Caveat (never skip): rollout assumes attention is the only pathway. Residuals (the ) and MLPs also carry information, so rollout is an upper-bound sketch, not truth.
Verify: ✓.
Forecast: Attention says "the"; gradient says "not". Who wins?
- What attention says. "the" is looked at 12× more than "not" ( vs ). Why this step? Establishes the correlation story — where the model's eyes lingered.
- What gradient says. Perturbing "not" moves the loss more than perturbing "the" ( vs ). Why this step? Gradient is the causal test: it measures whether changing the input changes the outcome.
- Resolve. "not" is the causal driver despite tiny attention (its influence is indirect, through other tokens); "the" was examined but unused. Trust the gradient for importance claims. Why this step? This is the parent note's slogan made numeric: attention shows correlation, gradients show causation.
Verify: Attention ranks "the" above "not" (, ratio ); gradient ranks "not" above "the" (, ratio ). The two rankings are opposite ✓.
Forecast: If two value vectors are identical, does the split between them matter?
- Compute the original output. . Why this step? Establishes the target output we must reproduce.
- Try a wildly different . Because , only their sum of weights matters: . Why this step? Identical (or collinear) value vectors make the attention split non-identifiable — many weightings yield one output.
- Conclusion. The "explanation" heatmap flipped from favouring (weight ) to favouring (weight ), yet the model's output vector is byte-for-byte unchanged at . Any observer reading the heatmap would tell two contradictory stories about "what the model focused on," while the network behaves identically. This is the Jain & Wallace adversarial-attention phenomenon in miniature: because attention weights are not uniquely determined by the output, they cannot be the explanation. Why this step? It closes the promised adversarial scenario — different heatmap, same prediction — and points to Adversarial Attention for the full construction on real models.
Verify: and — identical outputs from different attention ✓.
Forecast: Will "not" get the largest weight, and is that enough to justify calling it the explanation?
- Scale by . Scaled scores . Why this step? Same discipline as Example 2 — divide by to keep magnitudes tame before softmax.
- Exponentiate and sum. , , , ; sum . Why this step? Standard softmax machinery — positivity via , then a denominator to force the sum to 1.
- Divide to get the weights. Why this step? Now we can read the heatmap: "not" () and "great" () dominate — a plausible story that the model looked at the negation and the sentiment word.
- The honest caveat. Plausible ≠ proven. To claim "not" caused the negative label, ablate it: rerun with "not" removed and check the label flips, or measure (cf. Integrated Gradients / SHAP for Deep Learning). As Example 8 showed, a different heatmap could give the same output — so the heatmap is a hypothesis, not a verdict. Why this step? Closes the loop back to the whole page's thesis.
The figure below shows the four weights as bars. Notice "not" and "great" together carry — nearly three-quarters of all the attention mass — while "movie" and "was" split the rest. The visual makes the dominance obvious and reminds us (red note) that dominance in the heatmap is not the same as causal proof.

Verify: ✓, and "not" is the arg-max weight ✓.
Which tool answers which question?
Recall Self-check
Uniform attention over 4 tokens means all four are equally important. ::: False — it means the head expresses no preference; importance is undetermined (Cell C3). A token with attention weight 0.9 must dominate the output. ::: False — if its value vector is orthogonal to what downstream reads, its contribution is 0 (Cell C4). What does the symbol stand for? ::: The dimension (number of entries) of each query/key vector; dot-product scores grow with it, which is why we divide by . Why divide raw scores by before softmax? ::: Raw dot products grow with dimension; without scaling softmax saturates to near one-hot and the gradient dies. What does entry of represent? ::: Cumulative (rolled-up) information flow from token to token summed over all intermediate tokens. Averaging two antagonistic heads and gives? ::: — flat, hiding both the signal and confounder. Attention shows correlation; gradients show causation.
Related tools that measure causal importance where attention cannot: Integrated Gradients, SHAP for Deep Learning, Layer-wise Relevance Propagation, Probing Classifiers. To inspect the raw heatmaps: BertViz Tool. Foundations: Attention Mechanisms, Transformer Architecture.