将编码器从AutoEncoder连接到LSTM

问题描述 投票:4回答:2

我有一个像这样定义的自动编码器

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

我该怎么做呢?

python keras deep-learning lstm
2个回答
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
投票

你的代码已经给出了答案。 encoder在其最后一层lstm中有两个维度(number_batch,number_features)而不是(number_batches,number_timesteps,number_features)。这是因为你没有设置return_sequences = True(这是你的预期行为)。

但是你想要做的是和你的解码器一样:你应用RepeatVector层使输入形状为3维,因此能够被装入LSTM层。

© www.soinside.com 2019 - 2024. All rights reserved.