Exercises — Bahdanau and Luong attention
Before we start, one figure fixes every symbol we will reuse. Never guess a symbol — look here.

Level 1 — Recognition
L1.1
For each formula below, name the mechanism (Bahdanau or Luong) and the exact step it belongs to.
(a) (b) (c)
Recall Solution
(a) Bahdanau, the alignment score step. Tell-tales: the additive + inside tanh, three parameter groups , and it uses the previous state .
(b) Luong (general/bilinear), the alignment score step. A single matrix sandwiched between two vectors, uses the current state .
(c) Luong, the attentional hidden state step — it blends the decoder state with the context AFTER attention. Bahdanau has no such line.
L1.2
Which one of , , is (i) always a single number, (ii) always between 0 and 1, (iii) a vector of the same length as an encoder state?
Recall Solution
(i) — a raw score, a single real number, any sign. (ii) — a softmax output, so . (iii) — a weighted average of the , so it lives in , exactly the size of one encoder state.
Level 2 — Application
L2.1
Three raw scores come out of a Luong dot-product layer: . Compute the attention weights (round to 3 dp) and identify which input word wins.
Recall Solution
Softmax exponentiates then normalises. , , ; sum . The middle word wins with of the attention. Note the two equal scores get equal weight — softmax preserves ties.
L2.2
With those weights and encoder states , , (so ), compute the context vector .
Recall Solution
First coordinate: . Second coordinate: . It leans hard toward , exactly the dominant word — the picture below shows sitting near .

L2.3
A Luong dot-product layer requires . If but , what breaks, and which single Luong variant fixes it with the fewest changes?
Recall Solution
is a dot product: it needs both vectors to have the same length. With the multiplication is undefined. The general/bilinear variant fixes it: turns the 256-length into a 512-length vector so the dot product lines up. It's the minimal fix — just one matrix, no tanh, no concat.
Level 3 — Analysis
L3.1
Count the trainable parameters for a Bahdanau layer with , , . Then do the same for a Luong-general layer with the same (include its ). Which is bigger?
Recall Solution
Bahdanau uses , , : Luong-general uses and : Here Luong is bigger (327,680 vs 98,432) — because , the attentional-hidden-state matrix, is large. The common claim "Luong has fewer parameters" refers only to the scoring matrix, not the whole layer. Read the tables carefully.
L3.2
Explain why Bahdanau uses but Luong uses — trace it to the ordering of operations, not to preference.
Recall Solution
The state that is available is dictated by when the RNN runs.
- Bahdanau computes attention before the RNN step, feeding into the RNN. At that moment the only decoder state that exists is the previous one, — the current hasn't been produced yet.
- Luong runs the RNN first (), so by scoring time the fresh is already available and is the natural query. Cause → effect: the timing fixes the state, not the other way around.
L3.3
The tanh appears in Bahdanau's score and in Luong's . In each case, what specific job does it do, and what tool would fail without it?
Recall Solution
- Bahdanau score:
tanhis the non-linearity of a one-hidden-layer network. Without it, collapses to a single linear map — that is just an additive dot-product-style score with no capacity to learn interactions.tanhbuys expressive alignment. - Luong :
tanhbounds the blended vector into per coordinate, so concatenating and (which can be large) doesn't hand an exploding vector to the output softmax. This ties to 3.3.5-Vanishing-and-Exploding-Gradients.
Level 4 — Synthesis
L4.1
Design a hybrid attention: use Bahdanau-style timing (attention before the RNN, input-feeding) but a Luong-general score. Write the full recurrence for step , and state the one consistency check you must satisfy for the score to be defined.
Recall Solution
Consistency check: must be so that has length and dots cleanly with . Because we score with , no step is needed — context already entered through the RNN. This is essentially how many modern seq2seq toolkits actually wire attention.
L4.2
You have a 3-word input and want the model to attend uniformly (no word favoured). What must be true of the three scores ? Then give one concrete score triple achieving and prove it.
Recall Solution
Softmax is invariant to adding a constant and strictly monotone, so equal weights require equal scores: . Take : Any constant triple works identically, because cancels top and bottom. Uniform attention = "the model found no reason to prefer any word."
Level 5 — Mastery
L5.1
A translation model attends almost entirely on the last encoder word () for every output step , regardless of meaning. This is exactly the failure attention was meant to cure. (a) Explain what the model has degenerated into. (b) Name a plausible root cause in the score computation. (c) Propose one architectural fix.
Recall Solution
(a) If always, the decoder is effectively reading only the encoder's final state — the classic fixed-length bottleneck of the plain 3.5.8-Encoder-Decoder-Architecture that attention was invented to escape. Attention is present but dead.
(b) Likely the scores are near-constant across except for , or the score magnitudes are so large that softmax saturates (one score dominates and its gradient vanishes) — a saturation cousin of the issues in 3.3.5-Vanishing-and-Exploding-Gradients. A common trigger: the query carries little positional signal, and (having seen the whole sentence) always scores highest under a dot product.
(c) Fixes: (i) scale the scores, e.g. divide by to de-saturate softmax — the move that later defines scaled dot-product attention in 3.5.11-Self-Attention-and-Transformers; (ii) add positional information to encoder states so differ meaningfully; (iii) use Bahdanau's richer tanh score to break the last-state dominance.
L5.2
Prove that the context vector is always a convex combination of the encoder states — i.e. can never lie outside the region spanned by the . Why is this a desirable safety property?
Recall Solution
By construction with (softmax outputs are positive) and (softmax normalises). A sum of vectors with non-negative coefficients summing to 1 is by definition a convex combination — it lies inside the convex hull of . Why desirable: can never invent a direction absent from the encoder — no hallucinated features, no magnitude blow-up. The summary is always a "blend of things the encoder actually saw," which stabilises training. Illustrated in the s02 figure: sits between the arrows, never beyond them.
Recall Self-test cloze
Bahdanau scores with the ::: previous decoder state , before the RNN step
Luong scores with the ::: current decoder state , after the RNN step
The context vector is always a ::: convex combination of encoder states (weights , sum to 1)
Removing tanh from Bahdanau's score collapses it into ::: a single linear map with no interaction capacity
See also: 3.5.10 Bahdanau and Luong attention (Hinglish) · 3.5.9-AttentionMechanism-Overview · 3.5.11-Self-Attention-and-Transformers · 2.4.7-Softmax-Function