嵌入版本seq2seq模型(Keras)

时间:2018-10-21 10:58:28

标签: keras embedding seq2seq

我想通过修改keras github上的示例来构建嵌入版本seq2seq模型。 https://github.com/keras-team/keras/blob/master/examples/lstm_seq2seq.py

我已经尝试过np.reshape,但是它不起作用。

from keras.layers.embeddings import Embedding

embedding = 100
vocab_size = 10000

encoder_inputs = Input(shape=(None,), name="Encoder_input")
encoder = LSTM(latent_dim, return_state=True, name='Encoder_lstm') 
Shared_Embedding = Embedding(output_dim=embedding, input_dim=vocab_size, name="Embedding") 
word_embedding_context = Shared_Embedding(encoder_inputs) 
encoder_outputs, state_h, state_c = encoder(word_embedding_context) 
encoder_states = [state_h, state_c] 

decoder_inputs = Input(shape=(None,))
decoder_lstm = LSTM(latent_dim, return_sequences=True, return_state=True, name="Decoder_lstm") 
word_embedding_answer = Shared_Embedding(decoder_inputs) 
decoder_outputs, _, _ = decoder_lstm(word_embedding_answer, initial_state=encoder_states) 


decoder_dense = Dense(vocab_size, activation='softmax', name="Dense_layer") 
decoder_outputs = decoder_dense(decoder_outputs) 


model = Model([encoder_inputs, decoder_inputs], decoder_outputs) 



model.compile(optimizer='rmsprop', loss='sparse_categorical_crossentropy')

# Note that `decoder_target_data` needs to be one-hot encoded,

# rather than sequences of integers like `decoder_input_data`!

model.fit([encoder_input_data, decoder_input_data], decoder_target_data,

          batch_size=batch_size,

          epochs=epochs,

          validation_split=0.2)
model.save('s2s.h5')

但是,我收到以下错误消息,有人可以帮助我吗? 非常感谢!

ValueError:检查输入时出错:预期Encoder_input具有2维,但是数组的形状为(4999,53,3132)

0 个答案:

没有答案