Skip to content

Commit d6886ab

Browse files
[Edit] AI: BEAM Search (#7339)
* [Edit] AI: BEAM Search * added keywords * Update content/ai/concepts/search-algorithms/terms/beam-search/beam-search.md * Format ---------
1 parent fca044f commit d6886ab

File tree

1 file changed

+63
-5
lines changed

1 file changed

+63
-5
lines changed

content/ai/concepts/search-algorithms/terms/beam-search/beam-search.md

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22
Title: 'BEAM Search'
33
Description: 'A graph searching algorithm that is a variant of breadth-first search.'
44
Subjects:
5-
- 'Data Science'
65
- 'AI'
6+
- 'Data Science'
77
Tags:
8-
- 'Search Algorithms'
98
- 'AI'
9+
- 'Algorithms'
10+
- 'Search Algorithms'
1011
CatalogContent:
1112
- 'learn-python-3'
12-
- 'paths/machine-learning-ai-engineering-foundations'
13+
- 'paths/computer-science'
1314
---
1415

1516
**BEAM Search**, a variant of [breadth-first Search](https://www.codecademy.com/resources/docs/ai/search-algorithms/breadth-first-search), is an algorithm that searches a weighted graph for an optimal path from some start node **S** to some goal node **G**. The difference between BEAM search and breadth-first search is that at every level of the search tree, only the top _β_ candidates are chosen for further exploration. Here, β is known as the _beam width_. The reasoning behind this is that a path from **S** to **G** is likely to pass through some top number of most promising nodes. This leads to an algorithm that is fast and memory-efficient because it can disregard many nodes that may appear to be too far from the goal. This, however, sometimes causes the algorithm to overlook a potential shortest path because a candidate node is greater than the beam width. This renders the algorithm as _incomplete_, meaning that finding the shortest path is not guaranteed. Since the algorithm can find a reasonably short path, trading completeness for efficiency is accepted. Much like [A\* search](https://www.codecademy.com/resources/docs/ai/search-algorithms/a-star-search) and [best-first search](https://www.codecademy.com/resources/docs/ai/search-algorithms/best-first-search), an evaluation function is used to evaluate the candidacy of nodes for further exploration.
1617

17-
## The Algorithm
18+
## How the Beam Search Algorithm Works
1819

1920
Similar to A\* search, the algorithm uses an **open list** and a **closed list** for the exploration of nodes. In this algorithm, the open list is an ordinary queue. The algorithm begins with a start node **S**, which also serves as the root node for the search tree.
2021

@@ -58,10 +59,67 @@ It is in this step that node **G** has been found:
5859
Trace the path from **S** to **G**:
5960
![Final-path](https://raw.githubusercontent.com/Codecademy/docs/main/media/BEAM-Search-final-path.png)
6061

61-
## Advantages and Disadvantages
62+
## Pseudocode for Beam Search Algorithm
63+
64+
```plaintext
65+
function BEAM_SEARCH(start_node, goal_test, beam_width, max_depth):
66+
current_level ← [start_node]
67+
depth ← 0
68+
69+
while current_level is not empty AND depth < max_depth:
70+
all_successors ← []
71+
72+
// Generate all successors from current level
73+
for each node in current_level:
74+
if goal_test(node):
75+
return SUCCESS (node)
76+
77+
successors ← get_successors(node)
78+
for each successor in successors:
79+
successor.score ← evaluation_function(successor)
80+
all_successors.append(successor)
81+
82+
if all_successors is empty:
83+
return FAILURE
84+
85+
// Sort all successors by their scores (best first)
86+
sorted_successors ← sort(all_successors, by=score, descending=True)
87+
88+
// Keep only top beam_width candidates for next level
89+
current_level ← sorted_successors[0 : beam_width]
90+
depth ← depth + 1
91+
92+
// Check final level for goal
93+
for each node in current_level:
94+
if goal_test(node):
95+
return SUCCESS (node)
96+
97+
return FAILURE
98+
```
99+
100+
## Advantages and Disadvantages of Beam Search
62101

63102
As stated before, the advantage of BEAM search is that it is efficient both in time and memory. The disadvantage can be seen from the example in that a possible shortest path from **S** to **G** was overlooked. This can be somewhat corrected by varying the beam width to include more nodes. This, however, would reduce the efficiency of the algorithm. Selecting the optimal beam width is problem-specific and usually requires further insight into the problem.
64103

65104
## Applications of BEAM Search
66105

67106
The BEAM search algorithm is commonly used in natural language processing and machine translation. An encoder is used to process the text from the source language and BEAM search selects the most probable words (the beam width) in the target language. The evaluation function in this context is the conditional probability of a word in the target language.
107+
108+
## Frequently Asked Questions
109+
110+
### 1. Is ChatGPT using beam search?
111+
112+
No, ChatGPT (and most large language models from OpenAI) doesn't use beam search during inference. It uses sampling-based decoding methods like top-k sampling, top-p (nucleus) sampling, or temperature-controlled sampling to generate text.
113+
114+
### 2. Where is beam search used?
115+
116+
Beam search is widely used in natural language processing (NLP) tasks where sequential predictions are needed:
117+
118+
- Machine translation (e.g., translating English to French)
119+
- Speech recognition
120+
- Text generation (in some models)
121+
- Optical character recognition (OCR)
122+
123+
### 3. What are the benefits of beam search?
124+
125+
Beam search is faster and more memory-efficient than exhaustive search. It explores only the most promising paths, giving better results than greedy search while still being customizable through the beam width. However, it might miss the optimal path.

0 commit comments

Comments
 (0)