One-Line Summary: Bahdanau attention (2014) let decoders dynamically focus on different parts of the input sequence, solving the fixed-length bottleneck of Seq2Seq and laying the conceptual foundation for the Transformer's self-attention.

Prerequisites: 03-sequence-to-sequence-models.md, 02-recurrent-neural-networks-and-lstms.md

What Is Attention?

Imagine you're translating a long German sentence into English. Instead of first memorizing the entire German sentence and then writing the English version from memory, you keep the German text in front of you and glance back at different parts as you write each English word. When translating the verb, you look at the German verb. When translating a noun phrase, you focus on the corresponding German noun phrase. You dynamically attend to the relevant parts of the source for each word you produce.

This is exactly what neural attention does. In a standard Seq2Seq model (03-sequence-to-sequence-models.md), the encoder compressed the entire input into a single fixed-length vector — a bottleneck that lost information for long sequences. Attention replaced this single vector with a dynamic, weighted combination of all encoder hidden states, computed fresh for each decoder step. The decoder could now "look at" the entire input at every generation step, focusing on the most relevant parts.

The idea was published almost simultaneously by two groups in late 2014: Bahdanau, Cho, and Bengio submitted their paper in September 2014, and it became one of the most influential papers in NLP history. Attention didn't just fix a technical limitation — it introduced a computational primitive that would, three years later, become the sole building block of the Transformer.

How It Works

  Attention Mechanism: Dynamic Focus on Source Sequence
 
  Encoder Hidden States:      h₁     h₂     h₃     h₄     h₅
                              │      │      │      │      │
                              │      │      │      │      │
  Attention Weights:        0.05   0.10   0.70   0.10   0.05
  (for current decoder      ──┬──  ──┬──  ──┬──  ──┬──  ──┬──
   step, sum to 1.0)          │      │      │      │      │
                              ▼      ▼      ▼      ▼      ▼
  Weighted Sum:          ┌─────────────────────────────────────┐
  (Context Vector c_t)   │   c_t = Σ α_i · h_i                │
                         │   (dynamically computed each step)  │
                         └──────────────────┬──────────────────┘


                                      ┌───────────┐
                                      │  Decoder   │ ──▶ Output token
                                      │  step t    │
                                      └───────────┘
 
  Without Attention:  Encoder ──▶ [single vector] ──▶ Decoder
  With Attention:     Encoder ──▶ [ALL hidden states, dynamically weighted] ──▶ Decoder

Figure: Attention allows the decoder to dynamically focus on different encoder positions at each generation step. The attention weights (alpha) form a soft alignment between source and target.

Bahdanau Attention (Additive Attention, 2014)

Dzmitry Bahdanau, Kyunghyun Cho, and Yoshua Bengio at Universite de Montreal proposed what is now called additive or concatenation attention. At each decoder timestep t, the mechanism computes:

  1. Alignment scores: e_{t,i} = v^T * tanh(W_a * s_{t-1} + U_a * h_i), where s_{t-1} is the decoder's previous hidden state and h_i is the encoder's hidden state at position i. This is a small feedforward network that scores how relevant each source position is to the current decoding step.

  2. Attention weights: alpha_{t,i} = softmax(e_{t,i}) over all source positions i. These weights sum to 1, forming a probability distribution over the input sequence.

  3. Context vector: c_t = sum(alpha_{t,i} * h_i). A weighted average of all encoder hidden states, where the weights reflect relevance to the current decoder step.

  4. Decoder update: The context vector c_t is concatenated with the decoder input and fed into the decoder RNN to produce the next hidden state.

The critical insight: the alignment function is learned, not hand-designed. The network discovers that when generating an English article, it should attend to the corresponding French noun region. The attention weights form an implicit alignment between source and target — and when visualized as a heatmap, they often closely match the alignments that human linguists would produce.

Luong Attention (Multiplicative Attention, 2015)

Minh-Thang Luong, Hieu Pham, and Christopher Manning at Stanford proposed a family of attention mechanisms in August 2015 that were simpler and more computationally efficient:

Dot-product attention: score(s_t, h_i) = s_t^T * h_i. No learned parameters at all — just a dot product between decoder and encoder states. This is the simplest form and works when both vectors have the same dimensionality.

General attention: score(s_t, h_i) = s_t^T * W_a * h_i. A single learned matrix mediates the comparison. More expressive than dot-product while still avoiding the two-matrix feedforward network of Bahdanau.

Local vs global attention: Global attention computes weights over all source positions (like Bahdanau). Local attention first predicts a single aligned position p_t, then only attends to a window around it — reducing computation for long sequences.

Luong attention was faster because dot products can be computed as a single matrix multiplication, while Bahdanau's additive attention requires evaluating a small neural network for each source position. This efficiency advantage would prove decisive: the Transformer's scaled dot-product attention is essentially Luong's dot-product variant with a scaling factor.

The Scaling Factor

When the dimensionality d of the vectors is large, dot products can grow large in magnitude, pushing softmax into regions with very small gradients. The fix, introduced formally in 01-attention-is-all-you-need.md, is to scale by 1/sqrt(d): score = (Q * K^T) / sqrt(d_k). This seemingly minor detail was crucial for stable training of deep attention-based models.

Attention as Soft Alignment

Traditional statistical machine translation used hard alignment — each target word was aligned to exactly one (or zero) source words. Attention introduced soft alignment: each target word attends to all source words with different weights. This gracefully handles one-to-many, many-to-one, and reordering relationships. A French adjective that corresponds to two English words naturally receives attention from both.

Why It Matters

Solving the Information Bottleneck

On WMT'14 English-to-French, Bahdanau attention improved BLEU by 5+ points over standard Seq2Seq for long sentences (30+ words). More importantly, performance no longer degraded with sentence length — the decoder could always access the full source. This made neural machine translation viable for real-world text, where sentences frequently exceed 20-30 words.

Launching a New Computational Paradigm

Attention proved to be far more general than machine translation. Within two years, it was applied to image captioning (attend to spatial regions of an image), speech recognition (attend to audio frames), reading comprehension (attend to relevant passage sections), and text summarization. The mechanism of dynamically weighting a set of values based on a query became a universal tool.

The Path to the Transformer

Bahdanau and Luong attention were still bolted onto sequential LSTM architectures. The encoder and decoder remained recurrent — attention merely helped bridge them. The radical insight of 01-attention-is-all-you-need.md was: what if attention was all you needed? What if you replaced recurrence entirely with attention, applied not just between encoder and decoder but within each layer? Self-attention was born from this question, and it transformed NLP forever. The scaled dot-product attention of the Transformer is a direct descendant of Luong's multiplicative attention.

Key Technical Details

  • Bahdanau attention paper: September 2014 (arXiv:1409.0473), published at ICLR 2015; over 30,000 citations
  • Luong attention paper: August 2015 (arXiv:1508.04025), published at EMNLP 2015
  • BLEU improvement: Bahdanau attention: ~26 BLEU vs ~19 for non-attention Seq2Seq on EN-FR (comparable data setup), with the gap widening dramatically for longer sentences
  • Computational cost: Bahdanau (additive) is O(d) per score computation; Luong (dot-product) is O(d) but with smaller constants and better GPU utilization via batched matrix multiply
  • Attention weight interpretability: Attention heatmaps closely correlate with linguistic alignment, making the mechanism somewhat interpretable — a rare property in deep learning
  • Adoption timeline: By 2016, attention was standard in all competitive neural MT systems; by 2017, Google Translate switched to attention-based neural MT (GNMT)

Common Misconceptions

  • "Attention tells you what the model is 'thinking about.'" Attention weights show where the model focuses, but they are not faithful explanations of the model's reasoning. Multiple attention heads can contribute differently, and downstream layers can override attention patterns. Jain & Wallace (2019) showed attention weights often don't correlate with feature importance.

  • "Bahdanau attention is self-attention." Bahdanau attention is cross-attention — the decoder attends to the encoder. Self-attention, where a sequence attends to itself, was introduced in the Transformer. The mathematical machinery is similar, but the application is fundamentally different.

  • "Attention eliminated the need for RNNs." Not in 2014-2016 — attention was added on top of RNN-based encoders and decoders. The RNN still did the sequential processing; attention just improved how information flowed between encoder and decoder. Eliminating RNNs entirely required the Transformer in 2017.

  • "Dot-product and additive attention are equivalent." They have the same expressive power in theory, but behave differently in practice. Additive attention tends to work better with smaller dimensions; dot-product attention scales better to high dimensions (with the scaling factor). The Transformer chose dot-product for its computational efficiency.

Connections to Other Concepts

  • Directly addressed the fixed-length bottleneck identified in 03-sequence-to-sequence-models.md
  • Augmented the LSTM architectures from 02-recurrent-neural-networks-and-lstms.md
  • Scaled dot-product attention became the core of 01-attention-is-all-you-need.md
  • The limitations of attention-augmented RNNs are covered in 07-the-bottlenecks-that-motivated-transformers.md
  • For the full technical treatment of modern attention, see llm-concepts/attention-mechanisms.md
  • Cross-attention reappears in encoder-decoder models like 05-t5-text-to-text-framework.md

Further Reading

  • Bahdanau, Cho, Bengio, "Neural Machine Translation by Jointly Learning to Align and Translate" (2014, arXiv:1409.0473) — the paper that started it all
  • Luong, Pham, Manning, "Effective Approaches to Attention-based Neural Machine Translation" (2015, arXiv:1508.04025) — introduced multiplicative attention and local attention
  • Xu et al., "Show, Attend and Tell: Neural Image Caption Generation with Visual Attention" (2015, arXiv:1502.03044) — attention applied to image captioning, demonstrating generality
  • Jain & Wallace, "Attention is not Explanation" (2019, arXiv:1902.10186) — challenged the interpretability of attention weights
  • Vaswani et al., "Attention Is All You Need" (2017, arXiv:1706.03762) — the culmination of attention research, eliminating recurrence entirely