如何使用 TimeDistributed 层预测动态长度的序列?蟒蛇3 [英] How to use TimeDistributed layer for predicting sequences of dynamic length? PYTHON 3

查看:24
本文介绍了如何使用 TimeDistributed 层预测动态长度的序列?蟒蛇3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在尝试构建一个基于 LSTM 的自动编码器,我想将其用于时间序列数据.这些被分成不同长度的序列.因此,模型的输入具有 [None, None, n_features] 形状,其中第一个 None 代表样本数,第二个代表序列的 time_steps.序列由 LSTM 处理,参数 return_sequences = False,然后由函数 RepeatVector 重新创建编码维度并再次运行 LSTM.最后我想使用TimeDistributed层,但是如何告诉python time_steps维度是动态的?看我的代码:

So I am trying to build an LSTM based autoencoder, which I want to use for the time series data. These are spitted up to sequences of different lengths. Input to the model has thus shape [None, None, n_features], where the first None stands for number of samples and the second for time_steps of the sequence. The sequences are processed by LSTM with argument return_sequences = False, coded dimension is then recreated by function RepeatVector and ran through LSTM again. In the end I would like to use the TimeDistributed layer, but how to tell python that the time_steps dimension is dynamic? See my code:

from keras import backend as K  
.... other dependencies .....
input_ae = Input(shape=(None, 2))  # shape: time_steps, n_features
LSTM1 = LSTM(units=128, return_sequences=False)(input_ae)
code = RepeatVector(n=K.shape(input_ae)[1])(LSTM1) # bottleneck layer
LSTM2 = LSTM(units=128, return_sequences=True)(code)
output = TimeDistributed(Dense(units=2))(LSTM2) # ???????  HOW TO ????

# no problem here so far: 
model = Model(input_ae, outputs=output) 
model.compile(optimizer='adam', loss='mse')

推荐答案

这个函数似乎可以解决问题

this function seems to do the trick

def repeat(x_inp):

    x, inp = x_inp
    x = tf.expand_dims(x, 1)
    x = tf.repeat(x, [tf.shape(inp)[1]], axis=1)

    return x

示例

input_ae = Input(shape=(None, 2))
LSTM1 = LSTM(units=128, return_sequences=False)(input_ae)
code = Lambda(repeat)([LSTM1, input_ae])
LSTM2 = LSTM(units=128, return_sequences=True)(code)
output = TimeDistributed(Dense(units=2))(LSTM2)

model = Model(input_ae, output) 
model.compile(optimizer='adam', loss='mse')

X = np.random.uniform(0,1, (100,30,2))
model.fit(X, X, epochs=5)

我在 TF 2.2 中使用 tf.keras

I'm using tf.keras with TF 2.2

这篇关于如何使用 TimeDistributed 层预测动态长度的序列?蟒蛇3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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