LSTM解码器中的启动令牌

时间:2019-01-14 09:44:27

标签: tensorflow keras lstm mencoder

我了解编码器-解码器模型,以及编码器的输出将如何成为解码器的输入。假定这里我只有解码器模型,我有解码器的initial_state(即提供了encoder_states_inputs)。

我想给“ decoder_inputs”作为开始标记(例如)...但是我不知道如何以及以什么格式?!

decoder_lstm = LSTM(n_units, return_sequences=True, return_state=True)    
decoder_outputs, state_h, state_c = decoder_lstm(decoder_inputs, initial_state=decoder_states_inputs)

还,我必须将启动令牌添加到原始序列中吗?即:

 <start> statemnt1
 <start> statemnt2
 ....

1 个答案:

答案 0 :(得分:2)

如何添加<start><end>符号实际上取决于您如何实现模型的其余部分,但是在大多数情况下,结果是相同的。例如,在official tensorflow example中,它将这些符号添加到每个句子中。

def preprocess_sentence(w):
    # other preprocessing

    w = w.rstrip().strip()

    # adding a start and an end token to the sentence
    # so that the model know when to start and stop predicting.
    w = '<start> ' + w + ' <end>'
    return w

# rest of the code
# ... word2idx is a dictionary that map words into unique ids

然后,在标记化部分,<start><end>符号分别映射到4和5。但是,如您在图片中所见,它仅将<start>馈入解码器的输入中,并将<end>馈入解码器的输出中。这意味着我们的数据类似于:

decoder_inp = raw_decoder_input[:, 0:-1]
decoder_out = raw_decoder_input[:, 1:]

enter image description here