I am implementing a Seq2Seq model in Keras. However, they have not provided the beam search option in the decoder. Hence, I considered pynlpl's BeamSearch but their documentation on search found here doesn't have any information about how to implement it. Could you please give an example of how beam search can be implemented?
There is a similar answer here: How to implement a custom beam search in TensorFlow? but, its not clear.
Generally speaking you can do like this:
tile the origin batch in the first dimension by beam_size times including the outputs(encode outputs for the attention and the final states of the encoder as the initial decoder state). Each beam sized repetitive samples are noted beam_i.
do one step of decoding and get the top beam_size * 2 indices and probabilities correspondingly from the vocabulary sized outputs for each beam_i.
keep the previous probabilities of each generated character and calculate the average probability with those probabilities and the last probability you get from the step 2, and get the top beam_size characters.
put those samples encountering a stop_decode symbol into a list and if the length of the list reaches beam size for each beam_i, the beam ends.
But it is a little too abstract and hence you can refer to this (official) example for help. It is implemented by Denny Britz from Google and this very simple one.