Exercises — The attention mechanism intuition
A quick reminder of the machinery you will use (all from the parent):
Here is a raw alignment score (a plain number saying "how relevant is input position right now"), is an attention weight (a fraction between and ), and is the context vector (a custom summary of the input for decoder step ). is the input length. One more symbol you will meet below: is the previously emitted output token (the word the decoder produced at the last step), fed back in so the decoder knows what it just said.
Level 1 — Recognition
Exercise 1.1 (L1)
Match each symbol to its plain-English meaning.
, , , , — pair each with: (a) weighted summary fed to the decoder, (b) raw relevance number, (c) previous decoder memory, (d) encoder memory at input position , (e) normalized focus fraction.
Recall Solution
- → (c) previous decoder memory (the state we align from, because doesn't exist yet).
- → (d) encoder memory at input position .
- → (b) raw relevance number (unbounded, not yet a probability).
- → (e) normalized focus fraction (in , sums to ).
- → (a) weighted summary fed to the decoder.
Exercise 1.2 (L1)
True or false: Vanilla seq2seq uses all encoder hidden states to build its context, while attention uses only the last one.
Recall Solution
False — it's the reverse. Vanilla seq2seq squeezes everything into the last state (the bottleneck). Attention uses all of , weighted by the . See Seq2seq models for the vanilla baseline.
Exercise 1.3 (L1)
Which quantity must sum to exactly across all input positions , for a fixed decoder step ? The scores , the weights , or the states ?
Recall Solution
The weights . That is precisely what the softmax in Step 2 guarantees: . The scores are unbounded and generally do not sum to ; the are vectors, not fractions.
Level 2 — Application
Exercise 2.1 (L2)
Decoder step , input length . Raw scores are , , . Compute all three attention weights .
Recall Solution
Exponentiate: , , . Sum (denominator): . Check: . ✓ The middle word grabs of the focus.
Exercise 2.2 (L2)
Using the weights from 2.1, and encoder states , , , compute the context vector .
Recall Solution
Why a weighted sum and not a max? Before the arithmetic, ask why attention blends states with weights instead of just picking the single highest-scoring state (a "max"/hard pick). Two reasons. (1) Differentiability: a hard argmax has zero gradient almost everywhere — you couldn't train it by backprop. The weighted sum is smooth, so gradients flow into every and the alignment can be learned. (2) Blending genuinely helps: a French article may need both gender info from one word and number info from another; a hard pick throws one away, the weighted sum keeps a mix. So the sum is a soft, trainable lookup — the best of "focus" (via large weights) and "flexibility" (via keeping everything).
Now the arithmetic:
First component: .
Second component: .
Notice leans hard toward 's direction (the up axis) because dominates — a soft pick of the middle word.

Exercise 2.3 (L2)
Same scores as 2.1, but now add to every score: . Do the weights change?
Recall Solution
No — the weights are identical to 2.1. Softmax is invariant to adding the same constant to every score: The factor cancels top and bottom. So again. Only the differences between scores matter, never their absolute size.
Level 3 — Analysis
Exercise 3.1 (L3)
An attention distribution over positions is perfectly uniform: for all . Compute the attention entropy (natural log). Then compute for a spiked distribution . Interpret.
Recall Solution
Uniform: . This equals — the maximum possible entropy for 4 options. Spiked: . Interpretation: high entropy () = diffuse, "looking everywhere"; low entropy () = sharp, "focused on one word." As the parent note warns, both can be correct — summarization wants diffuse, word-alignment wants sharp.

Exercise 3.2 (L3)
Why does Bahdanau (additive) attention use in its score, while Luong (multiplicative) uses ? Explain the ordering, not just the names.
Recall Solution
It comes down to when the context vector is needed in the update equation. (Recall = the previously emitted output token, defined at the top of this page.)
- Bahdanau: the context is an input to the RNN update . To build you need scores, but doesn't exist yet — it's what you're about to compute. So you must align from the already-available .
- Luong: the RNN update runs first to produce (context-free), then attention is applied to refine the output. By that point already exists, so the score can legally use it. The lesson: the available state depends on whether attention feeds the RNN (Bahdanau) or follows it (Luong). See Encoder-decoder architecture for the surrounding wiring.
Exercise 3.3 (L3)
You permute the encoder outputs: swap and . Assuming the scores are computed purely from content (no positional info added), what happens to (a) each weight and (b) the final context vector ?
Recall Solution
(a) The weights permute along with the states: whatever weight was attached to now attaches to and vice versa. The score function depends only on the content , so swapping contents swaps the scores identically. (b) The context vector is a sum, and addition is commutative, so is unchanged. This is the "permutation invariance" property from the parent note — a warning that pure content attention has no built-in notion of order, which is exactly why Transformers add positional encodings.
Level 4 — Synthesis
Exercise 4.1 (L4)
A decoder emits words; the input has words. How many alignment scores are computed over the whole translation? Then explain in one sentence why this is the cost the fixed-vector bottleneck was trading away.
Recall Solution
One score per (decoder step, encoder position) pair: scores. Trade-off: vanilla seq2seq computes essentially alignment scores by forcing all information through one vector (cheap, lossy); attention pays score computations to avoid that loss — spending computation to buy back information. This is the seed of the quadratic cost that self-attention later scales up.
Exercise 4.2 (L4)
Sketch (describe) the attention heatmap for translating "neural machine translation" → "traduction automatique neuronale" (French reverses the order). Rows are the 3 French output words, columns are the 3 English input words. Where are the hot cells?
Recall Solution
French reverses the noun-modifier order, so the heatmap has a anti-diagonal (bottom-left to top-right of a top-down reading) hot streak:
| output ↓ / input → | neural(1) | machine(2) | translation(3) |
|---|---|---|---|
| traduction (1) | · | · | hot |
| automatique (2) | · | hot | · |
| neuronale (3) | hot | · | · |
"traduction"↔"translation" (col 3), "automatique"↔"machine" (col 2), "neuronale"↔"neural" (col 1). A reordering pattern, not a diagonal — contrast with speech/OCR where alignment is monotonic (main diagonal). See Neural machine translation.

Exercise 4.3 (L4)
Argue why attention improves gradient flow compared to a long RNN encoder chain.
Recall Solution
In a plain RNN, the gradient from output to an early input must pass through every intermediate state — a long chain of multiplications that shrinks (vanishing gradient). Attention creates a direct, weighted shortcut: connects straight to each through a single weight . During backprop, the gradient reaches in essentially one hop, skipping the long recurrent chain. Fewer multiplicative steps → less vanishing → early words stay learnable even at step .
Level 5 — Mastery
Exercise 5.1 (L5, degenerate case)
Input length (a one-word source). Show that attention becomes trivial, and state what equals for every decoder step.
Recall Solution
With one position, softmax over a single score is forced: The score value is irrelevant — the denominator is the same single term. So for every decoder step. Attention degenerates exactly into vanilla seq2seq's single fixed context. Takeaway: attention only earns its keep when ; with one source token there is nothing to choose between.
Exercise 5.2 (L5, limiting behaviour)
Take two scores and . As , what do and approach? As ? This shows softmax interpolating between "hard pick" and "uniform."
Recall Solution
- : — a hard one-hot pick of position 2 (this is the "argmax" limit).
- : — perfectly uniform, no focus.
- : mirror image, position 1 wins. So the score gap is a continuous knob from diffuse to razor-sharp attention. Why this matters in practice: when scores are computed by a dot product over vectors of dimension (here = the length of the query/key vectors, i.e. how many numbers each such vector holds), the dot product's typical size grows with , so the gaps blow up and softmax saturates into a near one-hot hard pick — killing the gradient into every non-winning position. To prevent that saturation, scaled-dot-product attention divides each score by , keeping the gaps moderate and the softmax soft. This is the detail explored in Transformers and self-attention.
Exercise 5.3 (L5, numeric synthesis)
Scores over . Compute the weights and the entropy . Then a single score jumps: . Recompute weights and . Confirm entropy dropped.
Recall Solution
Uniform case: all four scores equal ⇒ each. Entropy (maximum, as in 3.1). Spiked case: (three times), . Denominator . Check sum: . ✓ Entropy: . ✓ — the spike sharpened attention, entropy fell as predicted.
Recall Self-test checklist
Can you, from memory: Restate the 3-step pipeline (score → softmax → blend)? ::: Yes — , then , then . Explain why softmax ignores a shared additive constant? ::: The factor cancels in numerator and denominator; only score differences matter. Say what happens to attention when ? ::: always, — degenerates to vanilla seq2seq. Explain low vs high entropy? ::: Low = focused/sharp, high = diffuse/broad; both can be task-correct. Distinguish Bahdanau () from Luong () ordering? ::: Context feeds the RNN in Bahdanau (state not yet built), context follows the RNN in Luong (state already built).