After the word goes through the encoding network, its final activation goes to the decoding network. This generates the first output prediction . We try to evaluate the probability of the first word, give the input sentence. Here we can consider multiple alternatives.

The Beam Search algorithm has a parameter called , which is called the beam width. So the algorithm not just considers one possibility but considers at the time. This words are stored in the computer memory for further calculations. Now beam search considers all this alternatives while calculating the second output prediction . For each of the alternative we then calculate the probability of the first two words with respect to the input .

We calculate this probability for all the words in the vocab and pick the top three of them. This is done until we get the sentence with highest probability.

Refinements

Length normalization

In beam search at each step we calculate the conditional probability of the words predicted so far with the input sequence. Suppose we have words in the output sequence then :

which also can be written as :

The issue with this calculation is that sometimes the probabilities are very small number this might lead too rounding off errors during multiplication. So in practice we calculate the following :

One more issue with both this calculation is the way longer sequences are treated. As the output sequence becomes bigger probabilities get multiplied which generally leads to a very small number. Same goes with the summation formula since of small numbers is negative or small, the sum of longer sequences makes it more negative.

This gives a undesirable effect of the model choosing only smaller outputs even though they are not that good. So we normalize the summation with length so we get good results.

Here is the hyper-parameter which can be tuned to get good results.

How to choose ??

If we use a large we get better results but the algorithm is slower. If we choose a small we might get worse results but it will be fast. Depending on the application we can use . In some research application where we want every bit of performance is also used.

As increase the increase in quality decreases. Unlike exact search algorithms like BFS or DFS, Beam Search runs faster but is not guaranteed to find exact maximum.

Beam search is an approximate search algorithm, ​also called a heuristic search algorithm. ​And so it doesn’t always output the most likely sentence. ​It’s only keeping track of B equals 3 or 10 or 100 top possibilities.