我在本教程中读取LSTM-AUTOENCODER:https://blog.keras.io/building-autoencoders-iencoders-in-keras.html,并粘贴以下相应的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,input_dim),这意味着时间序列数据的长度固定为timesteps
。如果我记得正确的RNN/LSTM可以处理可变长度的时间序列数据,我想知道是否可以以某种方式修改上述代码以接受任何长度的数据?
谢谢!
您可以使用shape=(None, input_dim)
但是RepeatVector
将需要直接从输入张量进行一些黑客攻击。(该代码可与TensorFlow一起使用,不确定Theano)
import keras.backend as K
def repeat(x):
stepMatrix = K.ones_like(x[0][:,:,:1]) #matrix with ones, shaped as (batch, steps, 1)
latentMatrix = K.expand_dims(x[1],axis=1) #latent vars, shaped as (batch, 1, latent_dim)
return K.batch_dot(stepMatrix,latentMatrix)
decoded = Lambda(repeat)([inputs,encoded])
decoded = LSTM(input_dim, return_sequences=True)(decoded)