如何将LSTM自动编码器应用于变长时间序列数据? [英] How to apply LSTM-autoencoder to variant-length time-series data?
问题描述
我在本教程中阅读了LSTM-autoencoder: https://blog. keras.io/building-autoencoders-in-keras.html ,并在下面粘贴相应的keras实现:
I read LSTM-autoencoder in this tutorial: https://blog.keras.io/building-autoencoders-in-keras.html, and paste the corresponding keras implementation below:
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)
在此实现中,他们将输入固定为形状(时间步长,input_dim),这意味着时间序列数据的长度固定为timesteps
.如果我没记错的话,RNN/LSTM可以处理可变长度的时间序列数据,并且我想知道是否可以通过某种方式修改代码以接受任何长度的数据?
In this implementation, they fixed the input to be of shape (timesteps, input_dim), which means length of time-series data is fixed to be timesteps
. If I remember correctly RNN/LSTM can handle time-series data of variable lengths and I am wondering if it is possible to modify the code above somehow to accept data of any length?
谢谢!
推荐答案
您可以使用shape=(None, input_dim)
但是RepeatVector
将需要一些技巧,直接从输入张量获取尺寸. (代码适用于tensorflow,不确定theano)
But the RepeatVector
will need some hacking taking dimensions directly from the input tensor. (The code works with tensorflow, not sure about 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)
这篇关于如何将LSTM自动编码器应用于变长时间序列数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!