0

I want to build up an embedding version seq2seq model by modifying the example on keras github. https://github.com/keras-team/keras/blob/master/examples/lstm_seq2seq.py

I've tried the np.reshape but it won't work.

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')

However, I got the following error messages, can anyone help me ? Thank a lot!

ValueError: Error when checking input: expected Encoder_input to have 2 dimensions, but got array with shape (4999, 53, 3132)

jjlin
  • 39
  • 5
  • What is the shape of `encoder_input_data`? It should be a 2D array. – today Oct 21 '18 at 13:38
  • I've reshaped encoder_input_data into 2D (4999, 3132) but I'm encountering another issue. '''Error when checking target: expected Dense_layer to have 3 dimensions, but got array with shape (4999, 3132)'' – jjlin Oct 21 '18 at 13:56
  • `decoder_target_data` must be a 3D array. It seems you are passing it as a 2D array. – today Oct 21 '18 at 14:04
  • I've changed the shape of decoder_target_data into (4999, 3132,1), but when I start to train, it shows that InvalidArgumentError: Incompatible shapes: [64,3132,10000] vs. [1,10000,1]. – jjlin Oct 21 '18 at 14:46

0 Answers0