编码器为每个输入Keras seq2seq返回相同的状态

时间:2018-06-04 07:26:42

标签: machine-learning keras chatbot seq2seq encoder-decoder

我在Keras使用编码器解码器seq2seq架构, 我传递了一个单一的形状数组(num_samples,max_sentence_length,max_words)进行训练,并使用教师强制。

#Encoder
latent_dim = 256
encoder_inputs = Input(shape=(None, max_words))
encoder = LSTM(latent_dim, return_state = True)
encoder_outputs, state_h, state_c = encoder(encoder_inputs)
encoder_states = [state_h, state_c]
#Decoder
decoder_inputs = Input(shape=(None, max_words))
decoder_lstm = LSTM(latent_dim, return_state = True, return_sequences = 
True)
decoder_outputs, _, _ = decoder_lstm(decoder_inputs, initial_state= 
encoder_states)
decoder_dense = Dense(max_words, activation = 'softmax')
decoder_outputs = decoder_dense(decoder_outputs)

对于推理模型:

# Inference model
encoder_model = Model(encoder_inputs, encoder_states)

decoder_state_input_h = Input(shape=(latent_dim,))
decoder_state_input_c = Input(shape=(latent_dim,))
decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c]
decoder_outputs, state_h, state_c = decoder_lstm(
    decoder_inputs, initial_state=decoder_states_inputs)
decoder_states = [state_h, state_c]
decoder_outputs = decoder_dense(decoder_outputs)
decoder_model = Model(
    [decoder_inputs] + decoder_states_inputs,
    [decoder_outputs] + decoder_states)

我尝试打印出encoder_model状态,但它总是为任何输入返回相同的状态。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

这仅仅是Keras示例中的内容,对吗?

https://blog.keras.io/a-ten-minute-introduction-to-sequence-to-sequence-learning-in-keras.html

您训练过模型吗?您发布的所有内容都与Keras文档中的内容相同,所以我认为这可能不是问题。

这是一个有效的示例,该示例也基于Keras文档,外观与您所拥有的非常相似。也许尝试运行此功能,看看自己与众不同?

https://github.com/JEddy92/TimeSeries_Seq2Seq/blob/master/notebooks/TS_Seq2Seq_Intro.ipynb