microsoft / CNTK

Microsoft Cognitive Toolkit (CNTK), an open source deep-learning toolkit
https://docs.microsoft.com/cognitive-toolkit/
Other
17.52k stars 4.28k forks source link

Beam Search on Seq2Seq #2696

Open tomasek opened 6 years ago

tomasek commented 6 years ago

Hello,

I am trying to implement beam search on top the CNTK_204 tutorial. I have reached this following code that works, but i am not sure it is totally correct:

def create_beam_search(s2smodel): @Function def sampling(x): perturbed_x = x + C.random.gumbel_like(x) return hardmax(perturbed_x) @Function def model_beam_search(input:InputSequence[Tensor[input_vocab_dim]]): unfold = UnfoldFrom(lambda history: s2smodel(history, input) >> sampling, until_predicate=lambda w: w[...,sentence_end_index], length_increase=1.5) return unfold(initial_state=sentence_start, dynamic_axes_like=input) return model_beam_search

n17s commented 6 years ago

The code above is performing random sampling of words proportionally to their probabilities according to the model. That's not beam search. In beam search, you keep track of the best beam_size hypotheses (prefixes), expand and score all of them (i.e. temporarily creating beam_size * vocab_size possibilities) and keep the best beam_size of them. No randomness is involved.

To do beam search efficiently a top_k operation is necessary. I'm currently working on an efficient top_k operation and will try to create a beam search example based on it.

lahirufano commented 6 years ago

@ebarsoumMS @n17s Do CNTK have a beam search decoder for CTC? I am looking for something like the Tensorflow implementation as given below: https://www.tensorflow.org/api_docs/python/tf/nn/ctc_beam_search_decoder