Beam Search Algorithm
Definition:
Beam Search is a heuristic search algorithm that is widely used in sequence prediction tasks to find the most likely sequence of outputs by considering multiple candidates at each step. Beam Search is particularly useful in applications like language translation, speech recognition, and image captioning where the goal is to generate coherent, accurate sequences based on probabilities.
Video Explanation

Characteristics:
- Heuristic Search:
Beam Search uses a probabilistic approach, evaluating multiple partial sequences at each time step and discarding less likely candidates. - Controlled Exploration:
Beam Search keeps a fixed number of candidates at each step, called the "beam width," balancing between search breadth and computational efficiency. - Greedy Yet Flexible:
Unlike pure greedy search, Beam Search maintains multiple candidates, allowing it to recover from locally optimal but globally suboptimal choices.
Components of Beam Search:
- Beam Width (k):
The beam width determines the number of candidate sequences kept at each time step. A higher beam width allows more paths to be considered but requires more computational resources. - Score Calculation:
Beam Search calculates scores for each candidate sequence by combining the probability of the sequence with additional metrics like length normalization. - Pruning:
At each time step, Beam Search keeps only the topksequences with the highest scores, discarding the rest to manage computational efficiency. - Termination Condition:
The algorithm stops when all selected sequences reach an end condition, such as a specified token (<end>token in NLP tasks) or a maximum length.
Beam Search Architecture:
- Input Sequence:
The model is provided with an initial input sequence, usually a<start>token for text generation tasks, to begin predicting the output sequence. - Candidate Expansion:
For each candidate sequence in the beam, the model generates probabilities for the next token in the sequence. - Pruning:
The algorithm keeps only thekmost likely sequences at each time step, maintaining only the highest-scoring candidates. - Output Selection:
When the search reaches the termination condition, Beam Search outputs the sequence with the highest score among the final candidates.
Problem Statement:
Given an initial input sequence and a model that can predict probabilities for the next token in a sequence, the goal of Beam Search is to generate the most probable sequence of tokens. This is achieved by exploring multiple possible paths and discarding less probable ones to optimize search efficiency while maintaining high-quality results.
Key Concepts:
- Sequence Probability:
The overall probability of a sequence is calculated as the product of the probabilities of each token in the sequence. Beam Search aims to maximize this probability. - Beam Width (k):
Beam width controls the number of candidates explored at each step. Higher beam widths can improve output quality but also increase computation. - Normalization:
Length normalization can be applied to avoid bias toward shorter or longer sequences, depending on the task. - Pruning Mechanism:
Pruning reduces the search space by keeping only the topkcandidates, discarding sequences with lower probabilities to manage computation.