Attention replaces the Seq2Seq fixed context vector with a dynamic weighted sum over all encoder states, recomputed at each decoding step. The weights \(\alpha_{t,s}\) — a soft alignment between target position \(t\) and source position \(s\) — are fully differentiable and learned end-to-end. Bahdanau et al. (2015) use a small MLP to score alignment (additive); Luong et al. (2015) replace it with a dot product (multiplicative). The dot product, scaled by \(1/\sqrt{d_k}\), is exactly the Transformer’s attention mechanism — applied here to encoder-decoder pairs, applied there to every position in a sequence attending to itself.
The Seq2Seq bottleneck is a single vector \(\mathbf{c} = h_T^{\text{enc}}\) that must represent an entire source sentence. Translation quality degrades sharply for sentences beyond ~20 tokens — not because the LSTM is weak, but because the fixed-size bottleneck cannot hold everything.
Attention removes the bottleneck by letting the decoder query the encoder directly at each step. Instead of one compressed representation, the decoder has access to the full sequence of encoder hidden states and learns which ones matter for each output token. The alignment weights \(\alpha_{t,s}\) are interpretable: plotting them produces a heatmap showing which source words influenced each target word.
This is the insight the Transformer generalises: if a decoder can attend to encoder states, why not let every position attend to every other position in the same sequence?
14.2 The mechanism
14.2.1 General formulation
At decoding step \(t\), attention computes a context vector\(\mathbf{c}_t\) as a soft, weighted combination of all encoder hidden states \(\{h_1^{\text{enc}}, \ldots, h_{T_x}^{\text{enc}}\}\):
where \(e_{t,s}\) is an alignment score measuring how well the decoder state at step \(t\) matches the encoder state at position \(s\). The score function is where Bahdanau and Luong differ.
\(\mathbf{W}_a\) and \(\mathbf{U}_a\) project decoder and encoder states to a common space; \(\mathbf{v}_a\) collapses to a scalar score.
The two states are combined by addition (hence “additive”) before the non-linearity — this handles the case where decoder and encoder hidden dimensions differ.
Parameters \(\mathbf{W}_a\), \(\mathbf{U}_a\), \(\mathbf{v}_a\) are learned jointly with the entire model.
The decoder update at step \(t\) uses the dynamic context:
The dot variant is the one that matters historically: it is exactly the Transformer’s attention score before scaling.
14.2.4 Alignment heatmap
Plotting \(\alpha_{t,s}\) for all \((t, s)\) pairs produces an alignment matrix. High weights fall on the diagonal for monotone language pairs (e.g. en→de); they cross diagonally for pairs with reordering. This was the first interpretable internals visualisation in neural MT.
14.2.5 Minimal sketch
The sketch computes additive and dot-product attention weights for a toy encoder-decoder pair, then prints the alignment matrix and context vector.
import numpy as nprng = np.random.default_rng(3)T_x, T_y, d_h, d_a =5, 4, 8, 16# src len, tgt len, hidden dim, attn dim# Toy encoder and decoder statesH_enc = rng.normal(size=(T_x, d_h)) # (T_x, d_h)H_dec = rng.normal(size=(T_y, d_h)) # (T_y, d_h) — one state per target stepdef softmax(x): e = np.exp(x - x.max())return e / e.sum()# ── Additive attention (Bahdanau) ──────────────────────────────────────────Wa = rng.normal(scale=0.1, size=(d_a, d_h)) # project decoder stateUa = rng.normal(scale=0.1, size=(d_a, d_h)) # project encoder stateva = rng.normal(scale=0.1, size=d_a) # collapse to scalardef additive_score(dec_h, enc_h):return va @ np.tanh(Wa @ dec_h + Ua @ enc_h)# ── Multiplicative attention (Luong dot) ───────────────────────────────────def dot_score(dec_h, enc_h):return dec_h @ enc_h # plain dot product# ── Compute context vectors for each target step ──────────────────────────print("Alignment weights (additive):")for t inrange(T_y): scores = np.array([additive_score(H_dec[t], H_enc[s]) for s inrange(T_x)]) alpha = softmax(scores) ctx = alpha @ H_enc # (d_h,) context vectorprint(f" t={t+1}: α = {np.round(alpha, 2)} ‖c_t‖ = {np.linalg.norm(ctx):.3f}")
In encoder-decoder attention, the query comes from the decoder and the keys/values from the encoder. Self-attention asks: what if query, key, and value all come from the same sequence? That single change — covering in Self-Attention & Multi-Head Attention — produces the Transformer’s core mechanism.
14.3 Application & impact
Concept here
What it becomes
Where
Dynamic context \(\mathbf{c}_t\)
Cross-attention
Transformer encoder-decoder
Alignment weights \(\alpha_{t,s}\)
Attention map / head visualisation
Interpretability tools, BERTViz
Additive score
Rarely used post-Transformer
Some efficient-attention variants
Dot-product score
Scaled dot-product attention (\(/ \sqrt{d_k}\))
Every Transformer, every LLM
Soft alignment
Hard alignment (argmax)
CTC, monotonic attention
Machine translation (2015–2017): additive attention became the standard component in RNN-based NMT systems, enabling competitive quality on long sentences.
The dot-product step: Luong’s dot score needs only one change to become the Transformer — divide by \(\sqrt{d_k}\) to prevent softmax saturation in high dimensions. Vaswani et al. (2017) note this explicitly.
Beyond MT: attention was quickly adopted in summarisation, reading comprehension (Bahdanau-style alignment over a document), and speech recognition (CTC + attention hybrid systems).
NoteKey takeaway
Attention breaks the fixed-context bottleneck by making the decoder’s query a first-class citizen: at each step, it asks which encoder states are relevant and receives a weighted answer. The score function is the only architectural choice — MLP (additive) vs. dot product (multiplicative). The dot product wins on speed and scales directly to the Transformer. The Transformer’s novelty is not inventing attention but asking: what if everything attends to everything, with no recurrence at all?
Bahdanau, D., Cho, K., & Bengio, Y. (2015). Neural machine translation by jointly learning to align and translate. Proceedings of the 3rd International Conference on Learning Representations (ICLR). https://arxiv.org/abs/1409.0473
Luong, M.-T., Pham, H., & Manning, C. D. (2015). Effective approaches to attention-based neural machine translation. Proceedings of the 2015 Conference on Empirical Methods in Natural Language Processing (EMNLP), 1412–1421. https://doi.org/10.18653/v1/D15-1166