Foundation reference

Beam Search & Structured Decoding

Beam search is the usual decoder for tasks with one intended output, such as machine translation; the sequence-to-sequence translation model of Sutskever et al. (2014) and Google's GNMT system (Wu et al., 2016) both used it.

Concept 51 of 100Core TrainingStudy phase 9
Key equation
score(y1:t)=∑i=1tlog⁡p(yi∣y<i,x)\text{score}(y_{1:t}) = \sum_{i=1}^{t} \log p(y_i \mid y_{<i}, x)
Reading map and next steps

Study guide

Where to start

Concept 51 of 100Beam SearchCore Training · Study phase 9: Advanced architectures & generation
Question

Beam search looks for the single most probable sequence instead of sampling from the model; why does that suit translation but produce generic, repetitive text in open-ended generation (Holtzman et al., 2020)?

How to study this pagePredict · Check · Generalize · Continue
PredictSay what you expect.

Pick a concrete input and say what the equation should give before you read the math.

CheckCheck against the math and the papers.

Work through the math, then read the cited papers for the original argument.

GeneralizeName what stays the same.

Identify the part of the idea that survives a change of notation or model, so you can recognize it in another paper or system.

ContinueFollow the connections.

Use the prerequisites and related concepts at the end of the page to fill the next gap.

Why It Matters for Modern Models

  • Beam search is the usual decoder for tasks with one intended output, such as machine translation; the sequence-to-sequence translation model of Sutskever et al. (2014) and Google's GNMT system (Wu et al., 2016) both used it.
  • It makes the trade-off between search and cost explicit: greedy decoding keeps one hypothesis and can miss a better sequence whose first token looked worse, while a beam of B hypotheses costs about B times as much computation per step.
  • Searching harder for the most probable sequence does not always give better text. In translation, quality eventually fell as the beam grew wider, mainly because the outputs became shorter (Koehn & Knowles, 2017); in open-ended generation, maximizing likelihood gives bland and repetitive text (Holtzman et al., 2020).

Questions to Think About

  • Why can a wider beam give worse translations, and how is that connected to models that assign high probability to short or even empty outputs (Koehn & Knowles, 2017; Stahlberg & Byrne, 2019)?
  • How does length normalization change which finished sequence wins, and what does the exponent α trade off?

Core Math

Key Equation
score(y1:t)=∑i=1tlog⁡p(yi∣y<i,x)\text{score}(y_{1:t}) = \sum_{i=1}^{t} \log p(y_i \mid y_{<i}, x)

Beam search looks for the most probable output sequence yy for an input xx while keeping only the BB best partial sequences (the beam) at each step. A partial sequence is scored by its log-probability:

score(y1:t)=∑i=1tlog⁡p(yi∣y<i,x)\text{score}(y_{1:t}) = \sum_{i=1}^{t} \log p(y_i \mid y_{<i}, x)

At step tt, every sequence in the beam is extended by every token in the vocabulary, and the BB highest-scoring extensions are kept. With B=1B = 1 this is greedy decoding; as BB grows it approaches an exact search for the most probable sequence.

Each extra token adds a negative log-probability, so the raw score favours short outputs. Length normalization divides by a power of the length TT when comparing finished sequences:

scorenorm(y1:T)=1Tα∑i=1Tlog⁡p(yi∣y<i,x),0≤α≤1\text{score}_{\text{norm}}(y_{1:T}) = \frac{1}{T^\alpha} \sum_{i=1}^{T} \log p(y_i \mid y_{<i}, x), \quad 0 \le \alpha \le 1

Google's GNMT system used a smoothed version, dividing by ((5+T)/6)α\big((5 + T)/6\big)^\alpha (Wu et al., 2016). Diverse beam search splits the beam into groups and penalizes a group for choosing tokens that earlier groups chose, so that the returned sequences differ from one another (Vijayakumar et al., 2018).

Key Papers

Sequence to Sequence Learning with Neural Networks

Sutskever, Vinyals & Le2014NeurIPS
Read paper →

Google's Neural Machine Translation System: Bridging the Gap between Human and Machine Translation

Wu et al.2016arXiv
Read paper →

Six Challenges for Neural Machine Translation

Koehn & Knowles2017First Workshop on Neural Machine Translation
Read paper →

The Curious Case of Neural Text Degeneration

Holtzman et al.2020ICLR
Read paper →

Diverse Beam Search: Decoding Diverse Solutions from Neural Sequence Models

Vijayakumar et al.2018AAAI
Read paper →

Connections