Sequence-to-sequence models and the attention mechanism

Sequence-to-Sequence Models and Attention

The encoder–decoder architecture for translation and summarization, training it with teacher forcing, the bottleneck problem, then additive (Bahdanau) and multiplicative (Luong) attention with their equations, beam-search decoding, and a direct lead-in to transformers.

Lesson 21 of 38Intermediate45 minutes

Lesson objectives

  • Describe the encoder–decoder architecture and how training and inference work.
  • Compute the attention weights and the context vector for a small example.
  • Compare greedy search with beam search.
Page contents
  1. The encoder–decoder
  2. The bottleneck
  3. Additive (Bahdanau) attention
  4. Multiplicative (Luong) attention
  5. Decoding: greedy versus beam
  6. The road to the transformer

The encoder–decoder

In tasks such as translation and summarization, the input is a sequence and the output is a sequence of a different length. Sutskever et al. (2014) and Cho et al. (2014) proposed the encoder–decoder (Encoder–Decoder) architecture:

  1. The encoder: an LSTM that reads the source sentence x_{1:n} and produces a final state \mathbf{h}_n (the context vector).
  2. The decoder: another LSTM that starts from \mathbf{h}_n and generates the target sentence token by token until the end token:
P(y_{1:m} \mid x_{1:n}) = \prod_{t=1}^{m} P(y_t \mid y_{<t}, \mathbf{c})

One of the tricks Sutskever found was reversing the order of the source sentence, because it brings the start of the source closer to the start of the target and eases learning, a trick that pointed to the long-distance problem.

Training with teacher forcing

During training, at step t we feed the decoder the correct token y_{t-1} from the reference, not what it predicted (Teacher Forcing). This speeds up convergence but creates exposure bias: at inference the model sees its own predictions (which may be wrong), not the reference, so errors accumulate.

The bottleneck

The whole source sentence, however long, is compressed into a single vector \mathbf{c}. Cho et al. noticed that translation quality drops clearly with sentence length.

Additive (Bahdanau) attention

Bahdanau, Cho and Bengio (2015) introduced a simple, revolutionary idea: instead of a single vector, the encoder (a BiRNN) keeps a representation for every word \mathbf{h}_1, \dots, \mathbf{h}_n, and at each step t the decoder computes its own context vector as a weighted average of them:

  1. The score: how relevant each source word is to the previous decoder state \mathbf{s}_{t-1}:
e_{t,j} = \mathbf{v}^\top \tanh(W_s\, \mathbf{s}_{t-1} + W_h\, \mathbf{h}_j)
  1. The weights, with softmax:
\alpha_{t,j} = \frac{\exp(e_{t,j})}{\sum_{k=1}^{n} \exp(e_{t,k})}
  1. The context vector:
\mathbf{c}_t = \sum_{j=1}^{n} \alpha_{t,j}\, \mathbf{h}_j

\mathbf{c}_t enters, together with the previous token, into computing the decoder state and the next-word distribution. The \alpha weights are a learned soft alignment between source and target words, and can be drawn as a heat map: when translating "the cat" into “القطة”, attention concentrates on "cat".

Encoder and decoder with attention Seq2Seq with attention
EncoderDecoderx1h1α1x2h2α2x3h3α3x4h4α4context c_t= Σ α_i h_is1y1s2y2s3y3step t=2

The encoder turns the input into hidden states, and at every generation step attention computes weights over these states to form a context vector specific to that step.

A worked example

Three source words with scores e = (2.0,\ 1.0,\ 0.0):

\alpha = \text{softmax}(e) = \frac{(e^{2}, e^{1}, e^{0})}{e^{2} + e^{1} + e^{0}} = \frac{(7.39,\ 2.72,\ 1.00)}{11.11} \approx (0.665,\ 0.245,\ 0.090)

And if \mathbf{h}_1 = (1, 0), \mathbf{h}_2 = (0, 1), \mathbf{h}_3 = (1, 1):

\mathbf{c} = 0.665(1,0) + 0.245(0,1) + 0.090(1,1) = (0.755,\ 0.335)

Multiplicative (Luong) attention

Luong et al. (2015) proposed simpler, faster score functions:

  • Dot: e_{t,j} = \mathbf{s}_t^\top \mathbf{h}_j
  • General: e_{t,j} = \mathbf{s}_t^\top W_a \mathbf{h}_j

Scaled dot-product attention is what the transformer would adopt.

Decoding: greedy versus beam

At inference we want \arg\max_{y} P(y \mid x), and exhaustive search is impossible.

  • Greedy: we choose the most likely token at each step. Fast, but it may miss a better overall translation.
  • Beam search: at each step we keep the best k partial hypotheses (the beam width, usually 4–10), expand each of them, and keep the best k. Because probability decreases with length, the log sum is normalized by length to avoid favoring short sentences:
\text{score}(y) = \frac{1}{|y|^{\alpha}} \sum_{t} \log P(y_t \mid y_{<t}, x)

This architecture (deep LSTM + attention + subword tokenization + beam search) peaked in Google's Neural Machine Translation system GNMT (Wu et al., 2016).

The road to the transformer

The next question was natural: if attention lets the decoder look directly at any word, why not let every word attend to every word within the same sentence, and drop recurrence altogether? This is the self-attention on which the transformer is built, in the next lesson.

Try it: in the Translation module, the lab shows dictionary-based translation with word alignment, and explains how it differs from neural translation, which only works with an external engine.

Lesson summary

  • The encoder reads the input, and the decoder generates the output token by token.
  • Attention lets the decoder look at all encoder states with weights that change at every step.
  • Attention is the building block on which the transformer is built entirely.

Check what you understood

Choose an answer for each question to see the correction and the reasoning. Your answers are not sent anywhere.

  1. What is the "bottleneck" in Seq2Seq without attention?

  2. What is "teacher forcing" (Teacher Forcing)?

References

  1. Sutskever, I., Vinyals, O. & Le, Q. V. (2014). Sequence to Sequence Learning with Neural Networks. NeurIPS arxiv.org/abs/1409.3215
  2. Bahdanau, D., Cho, K. & Bengio, Y. (2015). Neural Machine Translation by Jointly Learning to Align and Translate. ICLR arxiv.org/abs/1409.0473
  3. Luong, M.-T., Pham, H. & Manning, C. D. (2015). Effective Approaches to Attention-based Neural Machine Translation. EMNLP arxiv.org/abs/1508.04025
  4. Wu, Y. et al. (2016). Google's Neural Machine Translation System: Bridging the Gap between Human and Machine Translation arxiv.org/abs/1609.08144