Chapter 228
Beam Search
Beam Search
In sec_seq2seq, we predicted the output sequence token by token until the special end-of-sequence "<eos>" token is predicted. In this section, we will begin with formalizing this greedy search strategy and exploring issues with it, then compare this strategy with other alternatives: exhaustive search and beam search.
Before a formal introduction to greedy search, let us formalize the search problem using the same mathematical notation from sec_seq2seq. At any time step , the probability of the decoder output is conditional on the output subsequence before and the context variable that encodes the information of the input sequence. To quantify computational cost, denote by (it contains "<eos>") the output vocabulary. So the cardinality of this vocabulary set is the vocabulary size. Let us also specify the maximum number of tokens of an output sequence as . As a result, our goal is to search for an ideal output from all the possible output sequences. Of course, for all these output sequences, portions including and after "<eos>" will be discarded in the actual output.
Greedy Search
First, let us take a look at a simple strategy: greedy search. This strategy has been used to predict sequences in sec_seq2seq. In greedy search, at any time step of the output sequence, we search for the token with the highest conditional probability from , i.e.,
as the output. Once "<eos>" is outputted or the output sequence has reached its maximum length , the output sequence is completed.
So what can go wrong with greedy search? In fact, the optimal sequence should be the output sequence with the maximum , which is the conditional probability of generating an output sequence based on the input sequence. Unfortunately, there is no guarantee that the optimal sequence will be obtained by greedy search.
Let us illustrate it with an example.
Suppose that there are four tokens
"A", "B", "C", and "<eos>" in the output dictionary.
In fig_s2s-prob1,
the four numbers under each time step represent the conditional probabilities of generating "A", "B", "C", and "<eos>" at that time step, respectively.
At each time step,
greedy search selects the token with the highest conditional probability.
Therefore, the output sequence "A", "B", "C", and "<eos>" will be predicted
in fig_s2s-prob1.
The conditional probability of this output sequence is .
Next, let us look at another example in fig_s2s-prob2. Unlike in fig_s2s-prob1, at time step 2 we select the token "C" in fig_s2s-prob2, which has the second highest conditional probability. Since the output subsequences at time steps 1 and 2, on which time step 3 is based, have changed from "A" and "B" in fig_s2s-prob1 to "A" and "C" in fig_s2s-prob2, the conditional probability of each token at time step 3 has also changed in fig_s2s-prob2. Suppose that we choose the token "B" at time step 3. Now time step 4 is conditional on the output subsequence at the first three time steps "A", "C", and "B", which is different from "A", "B", and "C" in fig_s2s-prob1. Therefore, the conditional probability of generating each token at time step 4 in fig_s2s-prob2 is also different from that in fig_s2s-prob1. As a result, the conditional probability of the output sequence "A", "C", "B", and "<eos>" in fig_s2s-prob2 is , which is greater than that of greedy search in fig_s2s-prob1. In this example, the output sequence "A", "B", "C", and "<eos>" obtained by the greedy search is not an optimal sequence.
Exhaustive Search
If the goal is to obtain the optimal sequence, we may consider using exhaustive search: exhaustively enumerate all the possible output sequences with their conditional probabilities, then output the one with the highest conditional probability.
Although we can use exhaustive search to obtain the optimal sequence, its computational cost is likely to be excessively high. For example, when and , we will need to evaluate sequences. This is next to impossible! On the other hand, the computational cost of greedy search is : it is usually significantly smaller than that of exhaustive search. For example, when and , we only need to evaluate sequences.
Beam Search
Decisions about sequence searching strategies lie on a spectrum, with easy questions at either extreme. What if only accuracy matters? Obviously, exhaustive search. What if only computational cost matters? Clearly, greedy search. A real-world application usually asks a complicated question, somewhere in between those two extremes.
Beam search is an improved version of greedy search. It has a hyperparameter named beam size, . At time step 1, we select tokens with the highest conditional probabilities. Each of them will be the first token of candidate output sequences, respectively. At each subsequent time step, based on the candidate output sequences at the previous time step, we continue to select candidate output sequences with the highest conditional probabilities from possible choices.
fig_beam-search demonstrates the process of beam search with an example. Suppose that the output vocabulary contains only five elements: , where one of them is “<eos>”. Let the beam size be 2 and the maximum length of an output sequence be 3. At time step 1, suppose that the tokens with the highest conditional probabilities are and . At time step 2, for all we compute
and pick the largest two among these ten values, say and . Then at time step 3, for all , we compute
and pick the largest two among these ten values, say and As a result, we get six candidates output sequences: (i) ; (ii) ; (iii) , ; (iv) , ; (v) , , ; and (vi) , , .
In the end, we obtain the set of final candidate output sequences based on these six sequences (e.g., discard portions including and after “<eos>”). Then we choose the sequence with the highest of the following score as the output sequence:
where is the length of the final candidate sequence and is usually set to 0.75. Since a longer sequence has more logarithmic terms in the summation of eq_beam-search-score, the term in the denominator penalizes long sequences.
The computational cost of beam search is . This result is in between that of greedy search and that of exhaustive search. In fact, greedy search can be treated as a special type of beam search with a beam size of 1. With a flexible choice of the beam size, beam search provides a tradeoff between accuracy versus computational cost.
Summary
- Sequence searching strategies include greedy search, exhaustive search, and beam search.
- Beam search provides a tradeoff between accuracy versus computational cost via its flexible choice of the beam size.
Exercises
- Can we treat exhaustive search as a special type of beam search? Why or why not?
- Apply beam search in the machine translation problem in sec_seq2seq. How does the beam size affect the translation results and the prediction speed?
- We used language modeling for generating text following user-provided prefixes in sec_rnn_scratch. Which kind of search strategy does it use? Can you improve it?
