Exercises — Attention visualization and limitations
These problems build from reading an attention matrix to reasoning about why it can mislead you. Every symbol used here is built in the parent note and in Attention Mechanisms. Work each one before opening the solution.
Reminders of the two objects we compute with:
The row is a probability distribution: it says where query position looks. That row-sums-to-one fact is the workhorse of almost every problem below.
Level 1 — Recognition
Exercise 1.1 — Read a single row
An attention matrix over tokens ["The", "cat", "sat"] has query-row for "sat" equal to
(a) Which token does "sat" attend to most? (b) What must equal, and why?
Recall Solution
(a) The largest entry is , in the "cat" column, so "sat" attends most to =="cat"==.
(b) The entries must sum to . Why: each row is a softmax output — softmax exponentiates then divides by the total, so a row is a probability distribution that always sums to .
Exercise 1.2 — Softmax by hand
Two raw attention scores (pre-softmax) are . Compute the attention weights.
Recall Solution
Softmax exponentiates each score then normalizes: Sum . Weights: The bigger score gets the bigger share, but the small one is never zero — softmax gives strictly positive weights.
Level 2 — Application
Exercise 2.1 — Scaling changes the sharpness
Query , keys , , dimension . Compute the two attention weights with the scaling.
Recall Solution
Dot products: , . Scale by : scores . Softmax: , , sum . Why the scaling? Without dividing by the score would be giving (Exercise 1.2) — sharper. Scaling flattens the distribution so gradients through softmax don't vanish in high dimensions.
Exercise 2.2 — Averaging heads
Three heads give the "it"→"animal" weight as . Compute the mean-attention value .
Recall Solution
Note how one confident head () got diluted to by two quiet heads.
Level 3 — Analysis
Exercise 3.1 — Attention rollout, two layers
Two single-head layers, no residual term, over 2 tokens: Compute the rollout and read off how much token 1's output depends on input token 1.
Recall Solution
Matrix product :
- Row 1:
- Row 2:
Entry : why the product? Layer 1 routes inputmid, layer 2 routes midoutput. The product's entry sums over the intermediate token : — the transitive information path. See figure

Exercise 3.2 — Rollout with identity (residual)
Same as above. Form , then row-normalize it (this is the residual-stream trick from the parent note).
Recall Solution
Row sums: and . Divide each row by its sum: Why add ? The residual connection copies each token's own value straight through, so a token attends partly to itself. Adding then renormalizing bakes that self-pass into the routing matrix, keeping rows summing to .
Level 4 — Synthesis
Exercise 4.1 — Attention high, contribution zero
Token gets attention but its value vector is and the downstream layer projects onto direction (its sensitivity). Compute the effective contribution and interpret.
Recall Solution
.
Despite attention, this token contributes nothing: its value vector lies in the null space of the downstream direction. This is the headline limitation — attention weight measured the look, not the effect. See figure 
Exercise 4.2 — Attention vs gradient disagreement
For output and two tokens, attention weights are but loss gradients are . Rank the tokens by attention and by gradient. What does the disagreement tell you?
Recall Solution
By attention: token 1 () token 2 (). By gradient: token 2 () token 1 (). They disagree. Token 1 was looked at heavily but changing it barely moves the loss (examined, not used). Token 2 was barely attended but the loss is very sensitive to it (indirect influence). Attention shows correlation; the gradient shows causation — see SHAP for Deep Learning and Probing Classifiers for causal probes.
Level 5 — Mastery
Exercise 5.1 — Design an adversarial attention pair
You have output with values , , and original weights . Find a different weight vector (a valid distribution) that produces the same output , demonstrating attention is not identifiable.
Recall Solution
Original output: .
Since , any split between positions 1 and 2 keeps position 3 at weight gives the same . Choose
Check: ✓
Meaning: two visibly different heatmaps ( vs ) produce an identical result. When value vectors are duplicated (or degenerate), attention weights are not uniquely determined — the core Jain–Wallace critique, formalized. See figure 
Exercise 5.2 — Full contribution pipeline
Combine everything. Token : , , downstream sensitivity direction (unit vector). Compute (a) , (b) the aligned magnitude , (c) the effective contribution , and compare against the naive "attention only" score .
Recall Solution
(a) . (b) , so . (c) Contribution . Naive attention score would report . The true routed magnitude is — nearly triple, because this value vector is both large and well-aligned with what the next layer reads. Attention alone under-reported this token. Lesson: always fold in and alignment, never rank by in isolation.
Recall Self-test cloze
Attention rows always sum to ::: (they are softmax / probability distributions) Attention shows correlation, gradients show ::: causation Two attention maps can give the same output when value vectors are ::: duplicated or degenerate (non-identifiability) True contribution times ::: times downstream sensitivity
Related tools: BertViz Tool for interactive heatmaps · Transformer Architecture for where these matrices come from · Hinglish version.