将编码器从自动编码器连接到LSTM [英] Connect Encoder from AutoEncoder to LSTM

查看:259
本文介绍了将编码器从自动编码器连接到LSTM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

我希望将编码器连接到这样的LSTM层

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.?

推荐答案

我认为主要问题是由于最后一个encoded不是重复向量而引起的.为了将编码器输出馈送到LSTM,它需要通过RepeatVector层发送.换句话说,编码器的最后一个输出必须具有[batch_size, time_steps, dim]形状,以便能够输入到LSTM中.这可能是您要寻找的东西吗?

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

我已将您的第一个decoded重命名为encode_repeat

I have renamed your first decoded to encode_repeat

这篇关于将编码器从自动编码器连接到LSTM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆