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:
- The encoder: an LSTM that reads the source sentence x_{1:n} and produces a final state \mathbf{h}_n (the context vector).
- The decoder: another LSTM that starts from \mathbf{h}_n and generates the target sentence token by token until the end token:
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:
- The score: how relevant each source word is to the previous decoder state \mathbf{s}_{t-1}:
- The weights, with softmax:
- The context vector:
\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".
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):
And if \mathbf{h}_1 = (1, 0), \mathbf{h}_2 = (0, 1), \mathbf{h}_3 = (1, 1):
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:
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.
