在具有编码器和解码器的 seq2seq 模型中,在每个生成步骤中,softmax 层都会输出整个词汇表的分布。在 CNTK 中,可以使用 C.hardmax 函数轻松实现贪婪解码器。它看起来像这样。
def create_model_greedy(s2smodel):
# model used in (greedy) decoding (history is decoder's own output)
@C.Function
@C.layers.Signature(InputSequence[C.layers.Tensor[input_vocab_dim]])
def model_greedy(input): # (input*) --> (word_sequence*)
# Decoding is an unfold() operation starting from sentence_start.
# We must transform s2smodel (history*, input* -> word_logp*) into a generator (history* -> output*)
# which holds 'input' in its closure.
unfold = C.layers.UnfoldFrom(lambda history: s2smodel(history, input) >> **C.hardmax**,
# stop once sentence_end_index was max-scoring output
until_predicate=lambda w: w[...,sentence_end_index],
length_increase=length_increase)
return unfold(initial_state=sentence_start, dynamic_axes_like=input)
return model_greedy
但是,在每一步我都不想以最大概率输出令牌。相反,我想要一个随机解码器,它根据词汇的概率分布生成一个标记。
我怎样才能做到这一点?任何帮助表示赞赏。谢谢。