Core

Bahdanau attention: the idea that changed everything

Additive attention lets each decoder step softly address all encoder states, replacing one fixed source summary with a learned alignment.

Updated

1

Concept

Classic encoder-decoder models force one fixed vector to carry the entire source. Bahdanau attention replaces that interface with a collection of encoder states and a learned addressing operation. At every target step, the decoder asks which source positions are useful now. The answer is a differentiable distribution, so alignment and translation can be learned jointly from the translation objective.

Let the encoder produce states h1,,hSh_1,\ldots,h_S. Before target step tt, the decoder has previous state st1s_{t-1}. Additive attention computes one energy for every source position:

eti=vtanh(Wsst1+Whhi+b).e_{ti}=v^\top\tanh(W_s s_{t-1}+W_h h_i+b).

The learned scorer can compare decoder needs and source content after projecting them into a shared alignment space.

Softmax across source positions turns energies into weights:

αti=exp(eti)j=1Sexp(etj).\alpha_{ti}=\frac{\exp(e_{ti})}{\sum_{j=1}^{S}\exp(e_{tj})}.

For a fixed target step, weights are non-negative and sum to one. A mask gives padded source positions effectively zero weight. Temperature is not normally an extra free decoding parameter here; the scale is learned through scorer parameters.

The context vector is

ct=i=1Sαtihi.c_t=\sum_{i=1}^{S}\alpha_{ti}h_i.

The decoder uses ctc_t with its state and target input to update itself and predict the next token. At the next target position, it computes new energies and a new context. A name, verb, or agreement marker can therefore retrieve different source evidence without demanding that one final encoder state preserve all details equally.

The full matrix of αti\alpha_{ti} values forms a soft alignment map: target positions on one axis, source positions on the other. Translation often produces roughly diagonal patterns with jumps or spreads for reordered phrases and multiword expressions. The alignment is learned without word-level alignment labels. “Soft” means the model can mix positions and gradients flow through every nonzero weight.

This design shortens paths for both information and learning. A source state can influence a late target directly through one attention edge rather than only through the encoder’s final state and many decoder transitions. The recurrent decoder remains sequential, and computing scores for all source positions at every target step adds work, but the fixed bottleneck is removed.

Attention weights are not complete explanations. A high weight shows that a stored value contributed strongly under one normalized routing operation. Encoder states already mix context; downstream decoder dynamics can amplify or cancel the contribution; alternate weights can sometimes yield similar outputs. Alignment plots are useful diagnostics and historical evidence, but causal claims require interventions rather than visual inspection alone.

Bahdanau attention changed the architecture’s central question from “how can the encoder compress everything?” to “how can the decoder retrieve what it needs?” Later dot-product attention altered the scoring rule, and the Transformer removed recurrence by using attention for representation mixing throughout. Queries, keys, and values generalize the same address-and-retrieve pattern. The enduring breakthrough is not a heatmap: it is a learned, differentiable memory interface whose address depends on the current computation.

When implementing the alignment, assert that every unmasked row sums to one and every padded position receives zero probability within numerical tolerance. These invariants catch a softmax applied over the wrong axis.

2

Explain it like I am five

A simultaneous interpreter keeps a row of source-language note cards on the desk. Before speaking each target word, she compares her current unfinished sentence with every card, puts a weight on each, and reads a weighted blend. Translating a name concentrates weight on its card; producing grammar may spread weight over several. The weights are soft and recomputed at every step. They expose an alignment, but do not prove which card causally determined the final wording.

3

Teach it back

Derive the steps of Bahdanau additive attention from encoder states and decoder state to energies, softmax weights, context vector, and next-token prediction.

Minimum: 80 characters and 15 words. Your text stays only in this browser.

Saved only on this device.

Show a model answer

For decoder step t, a learned feed-forward scorer computes e_ti=vᵀtanh(W_s s_{t−1}+W_h h_i) for every encoder state h_i. Softmax across source positions yields α_ti values that are non-negative and sum to one. The context c_t=Σ_i α_ti h_i is combined with decoder state/input to predict the next target and update the decoder. Recomputing c_t removes the requirement that one fixed final encoder vector serve every output position.

4

Check your understanding

1. Across which dimension is softmax applied in Bahdanau alignment for one decoder step?
Answer and explanation

Across source positions — The decoder distributes attention mass over the stored encoder states.

2. What is the context vector?
Answer and explanation

A weighted sum of encoder states — Soft weights create a differentiable summary tailored to the current decoding step.

Complete the teach-back and answer the quiz correctly to finish this lesson.

Sources

  1. Dzmitry Bahdanau, Kyunghyun Cho, and Yoshua Bengio (2015). Neural Machine Translation by Jointly Learning to Align and Translate.