【问题标题】:Input Data preparation for LSTM in Keras/TensorflowKeras/Tensorflow 中 LSTM 的输入数据准备
【发布时间】:2019-06-09 15:56:09
【问题描述】:

LSTM 层的输入形状是 (batch_size, timesteps, features)。我目前有一个如下所示的输入:

[0,1,2,3,4,5,6,7,8,9,10]

我使用我的代码重塑数据,使其看起来像这样

[
[0,1,2,3],
[1,2,3,4],
[2,3,4,5],
[3,4,5,6],
[4,5,6,7],
[5,6,7,8],
[6,7,8,9],
[5,7,8,10]
]

但是,在 Python 中重塑这些数据需要花费大量时间。 Keras/Tensorflow 中的 LSTM 模型有什么方法可以纯粹从 [0,1,2,3,4,5,6,7,8,9,10] 我在 Keras API 中将时间步长定义为 4。我试图寻找这样的选择,但没有找到。

这是我一直在使用的:

numberOfTimesteps = 240
i = 0
lstmFeatures = pd.DataFrame()
while i < features.transpose().shape[0] - numberOfTimesteps:
    temp = features.transpose().iloc[i:i+numberOfTimesteps,:]
    lstmFeatures = lstmFeatures.append(temp)
    if i%100 == 0:
        print(i,end=',')
    i = i + 1        

有没有人对如何重塑或如何使用 Keras 有更好的想法?

【问题讨论】:

标签: tensorflow input keras lstm timestep


【解决方案1】:

您可以使用tf.gather

import tensorflow as tf

my_data = tf.constant([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
to_take_inds = tf.range(4)[None, :]
to_take_inds = to_take_inds + tf.range(7)[:, None]

reshaped = tf.gather(my_data, to_take_inds)
with tf.Session() as sess:
    print(sess.run(reshaped))

打印

[[ 1  2  3  4]
 [ 2  3  4  5]
 [ 3  4  5  6]
 [ 4  5  6  7]
 [ 5  6  7  8]
 [ 6  7  8  9]
 [ 7  8  9 10]]

【讨论】:

    猜你喜欢
    • 2016-07-05
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多