How does Beam Search operate on the output of The Transformer?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Beam search is a popular decoding algorithm frequently used in conjunction with sequence-to-sequence models, such as Transformers, for tasks like machine translation, text generation, and more. The purpose of beam search is to maintain multiple hypotheses instead of just one as it progresses through generating the sequence, thereby improving the chances of finding the optimal sequence with higher probability. In this article, we delve into how beam search operates on the output of the Transformer model, explaining its workings with technical insights and examples.
Understanding Beam Search
Beam search is an algorithm that systematically explores a graph by expanding the most promising nodes in a limited set. It uses a breadth-first search approach but restricts the number of potential sequences it considers at each step to a fixed number, known as the beam width, `k`. This makes the search process efficient and manageable, avoiding the exhaustive computation associated with trying to explore all possible sequences (as in a naive approach).
The Transformer Output
Transformers consist of stacked self-attention and fully connected layers. When a Transformer model, such as BERT or GPT, is used for generating text, it outputs a probability distribution over the vocabulary for each token position. This output serves as the basis on which the beam search operates.
Steps in Beam Search on Transformer Output
- Initialization: Start with a single hypothesis, often containing only the start token. The beam width `k` defines how many hypotheses are carried forward to the next step.
- Expansion: At each time step, for each hypothesis, compute the probabilities for every word in the vocabulary using the Transformer’s output. Each hypothesis can generate new candidate sequences for every word from the vocabulary.
- Scoring: `Score` these candidate sequences based on their cumulative log-probability. The cumulative log-probability for a sequence is the sum of the log-probabilities of each token predicted so far.
- Pruning: Sort all candidate sequences by their scores and keep the top `k` sequences. This step ensures that the search space remains within the beam width's limits, focusing only on the most promising hypothesis.
- Repetition: Continue expanding and pruning hypotheses until a termination condition is met, such as generating the end token or reaching a maximum sequence length.
- Termination: Choose the highest scoring sequence from the final list of hypotheses as the output.
Example
Consider a simple example where our vocabulary is {A, B, C} and beam width, `k`, is 2. Assume a simplified Transformer output at each step provides the following probabilities:
| Step | Hypotheses | Cumulative Probability Log (Base 10) |
| 1 | [Start] | 0 |
| 2 | [Start, A] | -0.6 |
| [Start, B] | -0.4 | |
| 3 | [Start, A, A] | -1.3 |
| [Start, A, B] | -0.8 | |
| [Start, B, A] | -0.9 |
After step 1, we keep two sequences: [Start, A] and [Start, B] since those have the highest scores. At step 3, the sequences [Start, A, B] and [Start, B, A] are chosen due to their higher probabilities than [Start, A, A].
Considerations and Trade-offs
• Beam Width: The selection of beam width `k` is crucial as it influences both the computational cost and the performance of the algorithm. A larger `k` means better exploration of potential sequences but increased computation.
• Trade-off Between Exploration and Exploitation: With a small beam width, the search is more focused (exploitation), while a wider beam promotes exploration.
• Sacrificing Optimality for Practicality: Beam search is not guaranteed to find the globally optimal sequence due to its limited scope, especially with small `k`. However, it balances complexity and optimal decoding quite effectively.
Conclusion
Beam search is a powerful decoding technique when employed with models like Transformers. Its strategic balance between exploration and efficiency allows for improved sequence generation results in various NLP applications. Understanding the tuning and trade-offs of beam width is essential in harnessing the full potential of beam search for generating accurate and coherent text.
Key Points Summary
| Feature | Description |
Beam Width (k) | Number of sequences maintained at each step. |
Initialization | Start with basic hypotheses, typically starting with a start token. |
Expansion | Generate candidate hypotheses by extending current hypotheses at each step. |
Scoring | Evaluate new sequences based on cumulative log-probabilities. |
Pruning | Retain the top k sequences, discarding others to manage complexity. |
Termination | Final output is the highest scoring sequence after completing all steps or reaching a stopping point. |
Beam search, coupled with Transformer models, serves as an efficient method for exploring the vast space of possible sequences and achieving high-quality text generation. Understanding its mechanics and tuning parameters ensures optimal performance across diverse NLP tasks.
Related reading
- How does choosing between pre and post zero padding of sequences impact results
- How does data normalization work in keras during prediction?
- How does data normalization work in keras during prediction?
- How does Keras define accuracy and loss?
- How does CountVectorizer deal with new words in test data?
- How does Fine-tuning Word Embeddings work?
- How does binary cross entropy loss work on autoencoders?
- How does distributed tensorflow work ? Issue with tf.train.Server
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.