如何构建堆叠的序列到序列自动编码器?

时间:2018-01-17 11:17:02

标签: python keras

在keras博客中:“在Keras建立自动编码器” 提供以下代码来构建单个序列以序列自动编码器

from keras.layers import Input, LSTM, RepeatVector
from keras.models import Model

inputs = Input(shape=(timesteps, input_dim))
encoded = LSTM(latent_dim)(inputs)

decoded = RepeatVector(timesteps)(encoded)
decoded = LSTM(input_dim, return_sequences=True)(decoded)

sequence_autoencoder = Model(inputs, decoded)
encoder = Model(inputs, encoded)

我想构建堆叠自动编码器,如何更新此代码以构建堆叠自动编码器?

我自己尝试,这是我的代码:

timesteps = 3
input_dim = 1
inputs = Input(shape=(timesteps, input_dim))
encoded = LSTM(4)(inputs)
encoded = RepeatVector(timesteps)(encoded)
encoded = LSTM(2)(encoded)
encoded = RepeatVector(timesteps)(encoded)
decoded = LSTM(4,return_sequences = True)(encoded)
decoded = LSTM(input_dim,return_sequences = True)(decoded)

sequence_autoencoder = Model(inputs, decoded)

sequence_autoencoder.compile(loss='mean_squared_error', optimizer='Adam')

sequence_autoencoder.fit(x_train, x_train,
epochs=100,
batch_size=1,
shuffle=True,
)

我想知道,这段代码是正确的还是我错过了什么?

1 个答案:

答案 0 :(得分:1)

您创建的代码尝试遵循相同的原则遵循您找到的示例。但是你过早地摧毁了这个序列(最后因为这个原因你最后使用了额外的RepeatVector。)

为避免这种情况,您可以在所有编码器层中使用return_sequences=True,但最后一个除外。这使序列成为一个序列,允许更大的解释能力,因为您不会过早地折叠您的数据。

#add return_sequences=True to all layers except for the last
encoded = LSTM(4, return_sequences=True)(inputs) 

#do not use RepeatVector, you've got your sequences preserved with their length

#the last encoder layer is the only one that collapses the sequence
encoded = LSTM(2)(encoded) 

#this RepeatVector is the only that is needed, to restore the sequence length
decoded = RepeatVector(timesteps)(encoded)

#the rest is the same
decoded = LSTM(4,return_sequences = True)(encoded)
decoded = LSTM(input_dim,return_sequences = True)(decoded)