3.5.10 · D5Sequence Models

Question bank — Bahdanau and Luong attention

2,419 words11 min readBack to topic

This page is a trap-hunter. Every line below is a claim, a broken statement, or a "why" that a student who half-learned Bahdanau and Luong attention would stumble on. Cover each one, then reveal. If a reveal surprises you, re-read the matching section of the parent note before moving on.

Before you start, make sure the symbols we lean on constantly actually mean something concrete to you.

The two score functions this page quizzes you on are worth pinning down before the traps, so each item reads on its own.

Two pictures anchor everything below — glance at them before the questions, and we point back to them by name inside the reveals.

Figure — Bahdanau and Luong attention

The heatmap above is the attention weights : one row per decoder step, one column per encoder word, each cell's brightness is how much that word was attended to. The softmax curve on the right shows why a small lead in score becomes a big lead in weight.

Figure — Bahdanau and Luong attention

The alignment picture shows the encoder states along the bottom, the decoder walking left-to-right on top, and the bright links whose thickness is — the decoder "flipping to the relevant page" at each step.


True or false — justify

TF1. "Attention removes the encoder's final hidden state entirely."
False. Attention removes the reliance on a single fixed vector, but the encoder still produces all states ; the decoder is often still initialised from the last one. It stops being the only channel of information, not the only vector.
TF2. "Because attention weights sum to 1, the context vector can never point somewhere no encoder state reached."
True, and the proof is coordinate-wise. Fix any coordinate : is a convex combination (weights summing to 1) of the numbers , so — every coordinate lies inside the range the encoder states span. (The Euclidean norm is then bounded too: by the triangle inequality.) This is exactly why the bright blend in figure s02 always lands between the pink encoder nodes, never outside them.
TF3. "Bahdanau uses the current decoder state to score."
False. In the score the decoder term is the previous state , because attention is computed before the RNN step — the new state does not exist yet when scoring happens.
TF4. "Luong's dot-product score works for any encoder/decoder dimensions."
False. Plain dot product requires , because you can only dot two vectors of equal length. The general variant was invented precisely to lift that restriction.
TF5. "Softmax makes attention weights positive, so a low-relevance encoder state gets weight exactly 0."
False. is always strictly positive, so every weight is strictly greater than 0 — the model can shrink an irrelevant state's weight toward zero but never reaches exactly zero (soft attention, not hard). In figure s01 even the dimmest heatmap cell is a small positive number, never pure black.
TF6. "Additive and multiplicative attention differ mainly in whether they add or multiply the context into the output."
False. The "additive/multiplicative" names describe the score function — Bahdanau adds projected states inside a tanh (), Luong multiplies them via a dot product (). It has nothing to do with how the context is later merged.
TF7. "Removing the tanh from Bahdanau's score would leave the mechanism unchanged in expressive power."
False. Without tanh, collapses to a single linear function of and ; the tanh non-linearity is what lets it learn alignments that aren't just linear similarity.
TF8. "Luong's general score is symmetric: swapping and gives the same value."
False. is generally not symmetric, so in general. Encoder and decoder play different roles, which is the point.

Spot the error

SE1. "Bahdanau: ."
The context is a weighted sum of encoder states , not of the scalar scores . Correct: .
SE2. "."
The normalising sum runs over the same decoder step across all encoder positions : . Writing sums over the wrong axis (different decoder steps), which is meaningless. In figure s01 this is normalising along a row, not down a column.
SE3. "In Luong, we first compute the context, then run the RNN to get ."
Reversed. Luong runs the RNN first to get , then uses that to score and build . Doing it the Bahdanau way here breaks the definition.
SE4. "Luong output: ."
Luong feeds the attentional state (that's the combination matrix), not raw , into the output layer. Skipping the combine step throws away the context entirely.
SE5. "Dot-product attention has a weight matrix ."
The dot-product variant has no parameters at all. The matrix belongs to the general variant. Confusing them mis-counts the model's parameters.
SE6. "Bahdanau's parameters: , , ."
The shapes must all funnel into the attention dimension : and . Otherwise the sum wouldn't be a vector of the same length.
SE7. "Since produces a scalar, we can skip softmax and use directly as the weight."
The raw scores can be negative and don't sum to 1, so using them directly would let the context vector blow up or point in nonsensical directions. Softmax is what turns scores into a valid blend.
SE8. "To compute softmax we exponentiate the raw scores directly: ."
In practice you first subtract the max score, , which is mathematically identical (the constant cancels in numerator and denominator) but prevents from overflowing to infinity or underflowing to zero on large scores.

Why questions

WHY1. Why does Bahdanau project both states into a shared -space before comparing them?
The decoder space (length ) and encoder space (length ) may be different sizes and mean different things; the matrices and project both into a common -space so the sum is even well-defined, and the model learns the comparison rather than assuming the two spaces already line up.
WHY2. Why does softmax use the exponential rather than just dividing scores by their sum?
Exponentiation makes every weight positive (scores can be negative, so plain division would break) and it amplifies differences — a slightly higher score gets a disproportionately larger weight, sharpening focus. That amplification is exactly the softmax curve in figure s01. See 2.4.7-Softmax-Function.
WHY3. Why is Luong called "cheaper" than Bahdanau despite both using softmax and a context vector?
The difference is only in the score: Bahdanau runs a small feedforward net (, , tanh, ) per encoder position, while Luong's general score is a single matrix multiply . Fewer operations and often fewer parameters.
WHY4. Why does attention help with the vanishing-gradient problem of long sequences?
It creates direct weighted paths from every encoder state to the decoder (the links in figure s02), so gradient can flow back to early inputs without passing through every intermediate RNN step — shortening the effective path length that gradients must survive.
WHY5. Why does Bahdanau feed the context into the RNN (input feeding) while Luong combines it after via ?
Bahdanau lets the context shape the hidden-state evolution itself for tighter integration; Luong keeps the RNN attention-free and merges context afterward through for a cleaner, easier-to-implement separation. Neither is universally "correct".
WHY6. Why must the alignment scores be recomputed at every decoder step?
Because relevance changes as you generate: the word you need to attend to for output token 2 (e.g. the subject) differs from token 5 (e.g. the verb). You can see this in figure s01 — the bright cell moves across the row as increases. Freezing the weights would defeat the entire purpose.
WHY7. Why does the context vector have the same dimension as a single encoder state, not times bigger?
It is a weighted average (a blend), not a concatenation, of the states — averaging preserves the shape of one state regardless of how many you blend. That is what keeps the decoder input a fixed size no matter the sequence length.
WHY8. Why does Luong wrap the combined state in a ()?
The concatenation-and--multiply step can produce large values; tanh bounds the output to per component, stabilising the signal fed to the final softmax and letting the layer learn how much to trust context versus state.
WHY9. Why is attention a stepping stone to self-attention?
Both compute weighted blends via learned similarity; self-attention just makes a sequence attend to itself (queries, keys, values all from the same sequence) and drops the RNN, so the scoring intuition here transfers directly.

Edge cases

EC1. What are the attention weights when all alignment scores are equal?
Softmax of equal scores gives the uniform distribution for every , so the context becomes a plain average of all encoder states — the model is "undecided" and looks everywhere equally (a heatmap row of identical mid-tone cells in figure s01).
EC2. What happens to the context vector when the sequence has length ?
With one encoder state, softmax over a single score always gives , so exactly — attention degenerates to just copying that one state, and the whole mechanism adds nothing.
EC3. What about the truly empty source, (no encoder states at all)?
There is nothing to score, the softmax denominator , and is an empty sum — attention is undefined. In practice you never let this happen: an empty input is rejected or a special start/end token is inserted so .
EC4. If one score is enormously larger than the rest (say ), what does attention become?
Softmax saturates toward a one-hot vector: , all others , so . This is the "hard attention" limit — soft attention can approach but never exactly hit it. Numerically this is where the subtract-the-max trick matters, or overflows.
EC5. What does a negative alignment score mean for a state's weight?
A negative score simply means "less relevant than the others", but after it still yields a positive weight (just a small one). Negativity is relative, not a veto.
EC6. Two encoder states are identical — what happens?
They receive identical scores and therefore identical weights; their combined contribution is . The model can't distinguish them, which is fine — duplicate information is just pooled.
EC7. In Luong dot-product attention with , what happens if you run it anyway?
The dot product is undefined — you cannot multiply mismatched-length vectors. The model won't even build; you must switch to the general variant (whose bridges the gap) or reshape. This is a hard error, not a soft degradation.
EC8. What if every embedding/encoder state is the zero vector?
All scores become equal (dot products and projections of zero vanish), so weights are uniform and — the context carries no information, and the decoder falls back to whatever the RNN state alone provides.
Recall Rebuild the framework from memory

Name the four shared steps of both mechanisms, in order. Answer ::: (1) alignment scoring — compute a scalar for each encoder state; (2) softmax to weights — turn scores into a probability distribution summing to 1; (3) context vector — blend the encoder states, ; (4) output generation — use the context (Bahdanau feeds it into the RNN; Luong combines it via ) to predict .