问题
I have an auto encoder defined like this
inputs = Input(batch_shape=(1,timesteps, input_dim))
encoded = LSTM(4,return_sequences = True)(inputs)
encoded = LSTM(3,return_sequences = True)(encoded)
encoded = LSTM(2)(encoded)
decoded = RepeatVector(timesteps)(encoded)
decoded = LSTM(3,return_sequences = True)(decoded)
decoded = LSTM(4,return_sequences = True)(decoded)
decoded = LSTM(input_dim,return_sequences = True)(decoded)
sequence_autoencoder = Model(inputs, decoded)
encoder = Model(inputs,encoded)
I want the encoder to be connected to a LSTM layer like this
f_input = Input(batch_shape=(1, timesteps, input_dim))
encoder_input = encoder(inputs=f_input)
single_lstm_layer = LSTM(50, kernel_initializer=RandomUniform(minval=-0.05, maxval=0.05))(encoder_input)
drop_1 = Dropout(0.33)(single_lstm_layer)
output_layer = Dense(12, name="Output_Layer"
)(drop_1)
final_model = Model(inputs=[f_input], outputs=[output_layer])
But it gives me a dimension error.
Input 0 is incompatible with layer lstm_3: expected ndim=3, found ndim=2
How can I do this properly.?
回答1:
I think the main issue rises up from the fact that the very last encoded
is not a repeat vector. To feed the encoder output to the LSTM, it needs to be sent through a RepeatVector
layer. In other words, the last output of the encoder needs to have [batch_size, time_steps, dim]
shape to be able to be fed into a LSTM. This is probably what you're looking for?
inputs = Input(batch_shape=(1,timesteps, input_dim))
encoded = LSTM(4,return_sequences = True)(inputs)
encoded = LSTM(3,return_sequences = True)(encoded)
encoded = LSTM(2)(encoded)
encoded_repeat = RepeatVector(timesteps)(encoded)
decoded = LSTM(3,return_sequences = True)(encoded_repeat)
decoded = LSTM(4,return_sequences = True)(decoded)
decoded = LSTM(input_dim,return_sequences = True)(decoded)
sequence_autoencoder = Model(inputs, decoded)
encoder = Model(inputs,encoded_repeat)
f_input = Input(batch_shape=(1, timesteps, input_dim))
encoder_input = encoder(inputs=f_input)
single_lstm_layer = LSTM(50, kernel_initializer=RandomUniform(minval=-0.05, maxval=0.05))(encoder_input)
drop_1 = Dropout(0.33)(single_lstm_layer)
output_layer = Dense(12, name="Output_Layer"
)(drop_1)
final_model = Model(inputs=[f_input], outputs=[output_layer])
I have renamed your first decoded
to encode_repeat
回答2:
Your code already gives the answer.
encoder
has in its last layer lstm with two dimension (number_batch, number_features) instead of (number_batches, number_timesteps, number_features).
This is because you did not set return_sequences = True
(this is your intended behaviour).
But what you want to do is the same, as what you do with your decoder: You apply the RepeatVector layer to make the input shape 3 dimensional and therefore able to be feeded into a LSTM Layer.
来源:https://stackoverflow.com/questions/52201643/connect-encoder-from-autoencoder-to-lstm