我有一个这样定义的自动编码器
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)
我希望将编码器连接到这样的LSTM层
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])
但这给了我尺寸错误。
Input 0 is incompatible with layer lstm_3: expected ndim=3, found ndim=2
如何正确执行此操作??
答案 0 :(得分:2)
我认为主要问题是由于最后一个encoded
不是重复向量这一事实引起的。为了将编码器输出馈送到LSTM,它需要通过RepeatVector
层发送。换句话说,编码器的最后一个输出需要具有[batch_size, time_steps, dim]
的形状才能被输入到LSTM中。这可能是您要找的东西吗?
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])
我已将您的第一个decoded
重命名为encode_repeat
答案 1 :(得分:1)
您的代码已经给出了答案。
encoder
在其最后一层具有二维(number_batch,number_features)而不是(number_batches,number_timesteps,number_features)的lstm。
这是因为您没有设置return_sequences = True
(这是您的预期行为)。
但是,您要执行的操作与使用解码器的操作相同:您可以应用RepeatVector图层使输入形状为3维,从而可以将其输入到LSTM图层中。