gpt4 book ai didi

python - 将编码器从 AutoEncoder 连接到 LSTM

转载 作者:太空狗 更新时间:2023-10-30 02:52:45 25 4
gpt4 key购买 nike

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

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

我怎样才能正确地做到这一点?

最佳答案

我认为主要问题源于最后一个 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

关于python - 将编码器从 AutoEncoder 连接到 LSTM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52201643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com