How did the original attention mechanism for RNN-based machine translation compute a context vector at each decoding step?

Last updated 22 November 2026

In Bahdanau attention, the decoder builds a fresh context vector at every step. It scores each encoder hidden state against the previous decoder state, normalizes the scores with softmax into alignment weights, and takes the weighted sum of the encoder states.

  1. Encode: A bidirectional RNN produces an annotation h_j for each source token (forward and backward states concatenated). All of them are kept, not just the last.

  2. Score (alignment model): For decoder step i: e_ij = v^T tanh(W s_{i-1} + U h_j). This is a small feed-forward network, hence the name additive attention, trained jointly with the rest of the model.

  3. Normalize: α_ij = softmax_j(e_ij) gives a soft alignment over source positions.

  4. Combine: c_i = Σ_j α_ij h_j

  5. Decode: c_i, the previous output y_{i-1} and s_{i-1} produce the new state s_i and the next-token distribution.

  6. Variant: Luong attention (2015): Uses multiplicative scores (dot: s_t · h_j; general: s_t^T W h_j) computed from the current decoder state. This is the direct ancestor of Transformer dot-product attention.

  • https://machinelearningmastery.com/the-bahdanau-attention-mechanism/
  • https://d2l.ai/chapter_attention-mechanisms-and-transformers/bahdanau-attention.html
  • https://www.geeksforgeeks.org/artificial-intelligence/ml-attention-mechanism/
All Transformers interview questions