【问题标题】:How to apply LSTM-autoencoder to variant-length time-series data?如何将 LSTM 自动编码器应用于变长时间序列数据?
【发布时间】:2018-03-11 17:11:21
【问题描述】:

我在本教程中阅读了LSTM-autoencoder:https://blog.keras.io/building-autoencoders-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 可以处理可变长度的时间序列数据,我想知道是否可以以某种方式修改上面的代码以接受任何长度的数据?

谢谢!

【问题讨论】:

    标签: neural-network deep-learning keras lstm autoencoder


    【解决方案1】:

    你可以使用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)
    

    【讨论】:

    • 谢谢!你知道如何将不同长度的数据传递给自动编码器吗?我试图将 var-length 数组的列表转换为数组,但失败了。我尝试将 var-length 数组列表直接传递给它,但我收到错误信息说 Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 arrays but instead got the following list of 3773 arrays: [array([[ 0.300544 , 0.251966 ],
    • 还可以选择用虚拟值填充数组,使它们都具有相同的大小,并使用masking。 (我没用过,但是你可以google一下如何在keras上使用masking)。
    • 我注意到另一个问题,每次将一组新形状的数据输入自编码器(在for循环中)时,误差会突然增加然后逐渐减小。这是否意味着不同长度的数据的参数应该不同?有没有更好的方法来正确“混合”不同长度的数据,使训练结果不太依赖于不同长度数据的使用顺序?
    • 要混合它们,您可以使用填充 + 掩码和随机播放(在 fit 中是自动的,但您可以使用 shuffle=True 保证)。不同序列有不同的损失并不罕见。这不一定是由于长度。但是你不应该多次训练一个序列,而是循环序列。每个序列只有一个时期(我建议train_on_batch),然后你重复所有序列。
    猜你喜欢
    • 2019-12-26
    • 1970-01-01
    • 2018-10-01
    • 2018-07-16
    • 2019-04-29
    • 2021-01-07
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多