3.5.10 · D3Sequence Models

Worked examples — Bahdanau and Luong attention

2,582 words12 min readBack to topic

Before starting, one reminder of what each symbol means in plain words:

The scenario matrix

Every attention computation you will ever face falls into one of these cells. The rest of the page fills each one.

# Cell (scenario class) What makes it tricky Covered by
A Dot-product scoring, aligned spaces () Softmax on hand-computed dot products Ex 1
B General/bilinear scoring () The matrix bridges two sizes Ex 2
C Bahdanau additive scoring non-linearity + read-out Ex 3
D All scores equal (degenerate) Uniform attention — the "no-info" case Ex 4
E One score (limiting) Attention collapses to one word (hard attention) Ex 5
F Negative & zero scores, sign handling of negatives stays positive Ex 6
G Real-world word problem (translation) Reading the matrix as an alignment Ex 7
H Exam twist: Bahdanau vs Luong timing Which decoder state feeds the score? Ex 8

(All of , , are defined in the callout just above.)


Forecast: points purely along the first axis. Which do you think wins the attention — and by how much? Guess before computing.

  1. Compute the raw scores. Why this step? The dot product measures how much two vectors point the same way — that is exactly "relevance" when both live in the same space.
  2. Exponentiate. Why? Softmax needs positive, order-preserving numbers; makes negatives positive and amplifies gaps.
  3. Normalise. Why? Dividing by the sum forces the weights to add to — a genuine probability split of "where my attention goes".
  4. Blend the memories. Why? The context vector is the mix the decoder actually reads. Add coordinate-by-coordinate:

Verify: had the biggest score () and wins the most weight () — matches the forecast. Weights sum: . ✓


Forecast: reshapes into the 3-D encoder space. Will the two words end up sharing attention roughly equally, or lopsidedly?

  1. Map the decoder into encoder space. Why? is a -vector that lives where the live — now a dot product is legal.
  2. Dot with each memory. Why? Same relevance idea as Ex 1, now in the shared 3-D space.
  3. Softmax. Why? Turn scores into a probability split.

Verify: The two scores came out equal, so attention splits — the bilinear form correctly bridged the size gap and the symmetry is preserved. Note has parameters, matching the parent's count. ✓


Forecast: Both inputs get pushed through , which saturates near . Do you expect the weights to be extreme or gentle?

  1. Pre-activation for each. Why? Bahdanau projects decoder () and encoder () into the shared space and adds them — the "additive" name.
  2. Apply . Why and not just ? The non-linearity lets the network learn alignments that a plain sum cannot; it also bounds values in so no score can explode.
  3. Read out with (here ). So .
  4. Softmax.

Verify: so — order preserved. Because squashed the raw gap ( vs became vs ), the weights are gentle, not extreme — exactly the saturation effect we forecast. ✓


Forecast: If nothing is more relevant than anything else, where should attention land?

  1. Softmax of equal scores. Why? of the same number gives the same value; the sum just multiplies by the count.
  2. Context vector. Why? With uniform weights the blend is a plain average.

Verify: Weights sum to . ✓ Interpretation: attention has learned nothing selective — it degrades gracefully to the old fixed-vector average, which is the seq2seq bottleneck the parent note warned about. Notice the score value never mattered: softmax is shift-invariant. ✓


Forecast: One word gets shouted louder and louder. Where does attention concentrate?

  1. Write the weight. Why? Track the winning word's share as grows.
  2. Take the limit. Why? as .

Verify: At : — already near . This is hard attention: the soft blend collapses to picking one word. The opposite limit () drives , handing all attention to the others. ✓


Forecast: Negatives look dangerous. Can a probability come out negative? (No — but see why.)

  1. Exponentiate — signs vanish here. Why? maps every real number, negative or positive, to a positive value. That is precisely why softmax can eat any score.
  2. Normalise.

Verify: All three weights are positive despite a negative score. Sum . ✓ The most-negative score got the tiniest (but non-zero!) weight — attention never fully ignores anything unless a score is . ✓


Forecast: French says the noun first. Which English word should attention pick for output word 1?

  1. Softmax the scores. Why? Convert relevance into a readable percentage split.
  2. Read the alignment. Why? The largest tells us which source word the decoder is "flipping back to."

The model puts of its attention on = "car" while emitting "voiture" (= car). ✓

Verify: French voiture rouge reorders adjective and noun; attention has correctly aligned output-1 "voiture" to input-2 "car" across the reordering — the exact skill a fixed bottleneck could not do (see the attention overview). Weights sum: . ✓

Now do the second output word "rouge" (= red) the same way. Suppose its scores are : So output-2 "rouge" puts of its attention on = "red" — the other diagonal. Together the two rows form the alignment picture below.

Figure — Bahdanau and Luong attention

Forecast: Same formula shape, different decoder state. Can the timing alone flip which word wins? Guess before computing.

  1. Bahdanau uses the PREVIOUS state . Why this step? Bahdanau computes attention before the RNN step (parent note, "input feeding"), so at scoring time only exists yet. So Bahdanau's blend leans on .
  2. Luong uses the CURRENT state . Why this step? Luong runs the RNN first, then scores with the freshly-updated (parent note, "attention after the step"). So Luong's blend leans on instead.
  3. Name each recipe. Why? The whole point of the twist is to attach the right label. The recipe that scores with the older is Bahdanau (additive origins, attention before the step); the one that scores with the current is Luong (multiplicative, attention after the step).

Verify: The winning word flips under Bahdanau, under Luong — purely from which decoder state feeds the score, which is the single most-tested distinction. Both weight-pairs sum to . ✓ See 3.5.11-Self-Attention-and-Transformers for where this timing question disappears entirely (queries and keys are computed in parallel).


Recall Self-check

Softmax is invariant to adding a constant to every score. True or false? ::: True — that is why the score value was irrelevant in Ex 4, and why the max-subtraction stability trick is safe. Why can't Luong dot-product scoring be used when ? ::: The dot product needs both vectors the same length; the bilinear form inserts to bridge the sizes. In Ex 7, which input word did output-1 "voiture" align to, and with what weight? ::: "car" (), with . As one score , soft attention becomes _____ attention. ::: hard (one-hot) attention. What is ? ::: The attention hidden size — the length of the shared space Bahdanau projects both states into before .


See also: 3.5.8-Encoder-Decoder-Architecture · 5.1.2-Word-Embeddings · 3.3.5-Vanishing-and-Exploding-Gradients (why attention beats long-range recurrence).