Lesson 6.6core2 h 30 min

Seq2seq, encoder–decoder and beam search

Reading a whole sentence into a vector, then writing a new one out

The encoder–decoder pattern is the grammar of modern AI: an encoder turns an input into representations, a decoder generates an output step by step. You will meet the same two words in transformers (7.1 Attention Is All You Need: the big picture, 7.7 Encoder–decoder: T5, BART and choosing an architecture), autoencoders (4.10 Autoencoders, embeddings and representation learning), diffusion U-Nets (8.9 Generative models II: Diffusion and flow matching) and speech models (8.10 Multimodal models). Teacher forcing, exposure bias and beam search — all introduced here — are still how LLMs are trained and decoded (7.6 Decoder-only: GPT and autoregressive generation, 8.5 Decoding and sampling), and the fixed-vector bottleneck is the exact problem attention was invented to solve (6.7 Attention is born).

You'll be able to

  • Describe the encoder–decoder architecture and its training objective with teacher forcing
  • Explain exposure bias and the difference between training-time and inference-time inputs
  • Run beam search by hand with length normalization and contrast it with greedy decoding
  • Compute modified n-gram precision and BLEU, and state why BLEU is an imperfect metric

Key ideas

  1. Seq2seq = encoder RNN compressing the source into a context vector + decoder RNN generating the target as a conditional language model.
  2. Training maximizes Σₜ log P(yₜ | y<ₜ, x) with teacher forcing (true previous token as input); inference feeds back the model's own predictions.
  3. Exposure bias: the decoder never saw its own errors in training; scheduled sampling and RL-based training are mitigations.
Show 4 more key ideas
  1. Beam search keeps the B best partial hypotheses by cumulative log-probability (length-normalized by T^α); it can beat greedy but huge beams favor short, generic outputs.
  2. BLEU = brevity penalty × geometric mean of clipped n-gram precisions; useful at corpus level, blind to synonyms and paraphrase.
  3. The fixed-size context vector is a bottleneck that degrades with sentence length — the problem attention solves.
  4. Encoder/decoder vocabulary recurs everywhere: BERT (encoder-only), GPT (decoder-only), T5/Whisper (encoder–decoder), autoencoders/VAEs.

Suggested path through this lesson