When to Finish? Optimal Beam Search for Neural Text Generation (modulo beam size)

Liang Huang, Kai Zhao, Mingbo Ma

TL;DR

This paper introduces a provably optimal stopping criterion for beam search in neural text generation, ensuring that the algorithm always finds the highest-scoring hypothesis within the beam. By combining this with a novel "bounded length reward," the authors solve the common problem of neural models producing overly short sentences while maintaining a mathematical guarantee of optimality and improving both translation quality (BLEU) and decoding speed.

What Problem Does This Paper Address?

In neural text generation tasks like machine translation or summarization, beam search is the standard way to find the best output sequence. However, unlike traditional methods where all possible answers have the same length, neural models generate sequences of varying lengths, ending whenever a special "end-of-sentence" (</s>) token is produced.

This creates a dilemma: When do you stop searching?

  1. If you stop as soon as the top-ranked path finishes (the OpenNMT approach), you might miss a slightly lower-ranked path that could have finished with a much better score a few steps later.
  2. If you keep searching until the beam is empty (the RNNsearch "shrinking beam" approach), you waste computation and still lack a guarantee that you've found the best possible result.

Furthermore, neural models are notorious for a "brevity bias"—they tend to prefer shorter sentences because they involve fewer probability multiplications. While researchers often use "length rewards" to encourage longer outputs, these rewards usually break the mathematical properties needed to guarantee that a search algorithm is optimal.

Methods

The authors propose two key innovations:

1. An Optimal Stopping Criterion

The core idea is simple but powerful: stop only when you have an "optimality certificate." Let $best_{\le i}$ be the highest score of any completed hypothesis found so far. Let $B_{i,1}$ be the score of the very best active (incomplete) hypothesis in the current beam. Because probabilities are always $\le 1$, any future sentence grown from the current beam can only have a score equal to or worse than $B_{i,1}$.

Therefore, if $best_{\le i} \ge B_{i,1}$, it is mathematically impossible for any active path to eventually beat the one we already finished. We can stop immediately.

2. Bounded Length Reward

To fix the brevity bias without breaking the search, the authors introduce a bounded length reward. Instead of rewarding every single word indefinitely, they reward words only up to a target length $l$ (calculated as a ratio of the source sentence length).

Because the reward is "bounded," the authors can derive a modified stopping criterion that accounts for the maximum possible future reward an active path could receive. This allows the search to remain provably optimal while producing more natural-length sentences.

Datasets

The researchers evaluated their approach on Chinese-to-English neural machine translation (NMT):

  • Training: 1 million sentence pairs, processed using Byte-Pair Encoding (BPE) to handle large vocabularies.
  • Development: NIST 06 newswire (616 sentences) used for tuning the length reward $r$ and target length ratio.
  • Testing: NIST 08 newswire (691 sentences) used for final evaluation.

Key Insights and Ideas

  • Optimality is Faster: Surprisingly, being "optimal" doesn't mean being slow. The authors found their search finishes 3–5 steps earlier than standard baselines because it knows exactly when it can stop safely.
  • The "Modulo Beam Size" Caveat: The term "modulo beam size" in the title is a crucial technicality. It means the algorithm is optimal given the constraints of the beam width. It doesn't guarantee the global optimum (which would require an infinite beam), but it ensures you aren't leaving any quality on the table within your chosen beam size.
  • Model Scores vs. BLEU: As seen in Figure 1, the optimal search consistently finds higher model scores (log-probabilities) than the default OpenNMT search, especially as the beam size increases.
Comparison of model scores between optimal search and OpenNMT default.

Key Results

The proposed method outperformed both the standard OpenNMT baseline and the popular "shrinking beam" heuristic used in RNNsearch.

  • Translation Quality: On the NIST 08 test set, the optimal beam search with bounded reward achieved a 30.61 BLEU score, compared to 29.75 for the OpenNMT default—a significant gain of nearly 0.9 BLEU points.
  • Sentence Length: The bounded reward successfully moved the length ratio from a truncated 0.88 (too short) to a much more natural 0.97, as shown in Figure 2(b).
  • Efficiency: The algorithm terminates no later than OpenNMT's criteria and often earlier, providing a "free" boost in both quality and speed.
Figure 2: BLEU score and length ratio against beam size (on dev) of various beam search algorithms for neural machine translation.

Limitations and Future Work

  • Hyperparameter Tuning: The "optimal length ratio" and the reward value $r$ must be tuned on a development set. While the authors found $r=1.2$ worked well for Chinese-English, this might vary for other language pairs or tasks like summarization.
  • Scope: The paper focuses heavily on NMT. While the theory applies to any sequence-to-sequence task (like image captioning), empirical results for those fields are not provided in this specific paper.
  • Beam Size Limits: While the search is optimal modulo beam size, very small beam sizes still suffer from search errors that no stopping criterion can fix. The benefits of this method are most pronounced at beam sizes between 5 and 20.

Summary updated

Abstract

In neural text generation such as neural machine translation, summarization, and image captioning, beam search is widely used to improve the output text quality. However, in the neural generation setting, hypotheses can finish in different steps, which makes it difficult to decide when to end beam search to ensure optimality. We propose a provably optimal beam search algorithm that will always return the optimal-score complete hypothesis (modulo beam size), and finish as soon as the optimality is established (finishing no later than the baseline). To counter neural generation's tendency for shorter hypotheses, we also introduce a bounded length reward mechanism which allows a modified version of our beam search algorithm to remain optimal. Experiments on neural machine translation demonstrate that our principled beam search algorithm leads to improvement in BLEU score over previously proposed alternatives.
BibTeX
@inproceedings{huang2017when,
  author = {Liang Huang and Kai Zhao and Mingbo Ma},
  title = {{When to Finish? Optimal Beam Search for Neural Text Generation (modulo beam size)}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2017},
  url = {https://openreview.net/forum?id=HJbtiGfdZS},
}
Figure