【问题标题】:How to reshape input for keras LSTM?如何重塑 keras LSTM 的输入?
【发布时间】:2018-06-05 08:15:19
【问题描述】:

我有一个大约 5000 行和 4 列(温度、压力、速度、成本)的 numpy 数组。所以这是形状 (5000, 4)。每一行都是定期观察,这是我第一次进行时间序列预测,我被困在输入形状上。我试图预测 距离最后一个数据点 1 个时间步长的值。如何将其重塑为 keras 中 LSTM 模型的 3D 形式?

如果编写一个小示例程序也会更有帮助。似乎没有任何示例/教程输入具有多个特征(也不是 NLP)。

【问题讨论】:

  • 四个观察值都是输入变量还是其中一个是输出?
  • 有 4 列(温度、压力、速度、成本),我想使用所有列的过去值预测其中一个的未来值(主要是第一列,温度),包括温度,如果有意义的话。
  • 先看看this post,看看它是否有助于理解如何为 LSTM 重塑数据,如果可以,我可以回答,但问题非常相似

标签: python numpy neural-network keras lstm


【解决方案1】:

你应该问自己的第一个问题是:

  • 输入要素对您要预测的值的相关信息进行编码的时间尺度是多少?

让我们将此时间刻度称为prediction_context

您现在可以创建数据集了:

import numpy as np

recording_length = 5000
n_features = 4
prediction_context = 10  # Change here
# The data you already have
X_data = np.random.random((recording_length, n_features))
to_predict = np.random.random((5000,1))
# Make lists of training examples
X_in = []
Y_out = []
# Append examples to the lists (input and expected output)
for i in range(recording_length - prediction_context):
    X_in.append(X_data[i:i+prediction_context,:])
    Y_out.append(to_predict[i+prediction_context])

# Convert them to numpy array
X_train = np.array(X_in)
Y_train = np.array(Y_out)

最后:
X_train.shape = (recording_length - prediction_context, prediction_context, n_features)
因此,您需要在预测上下文的长度和必须训练网络的示例数量之间进行权衡。

【讨论】:

  • 每隔 15 分钟观察一次数据点(实例),如果这就是您所说的时间尺度? y_data 基本上是数据集中的列之一。我有四列,我想使用该(温度)列和其他列(压力、速度、成本)的过去值来预测其中一列(例如温度)的未来值。
  • 我也想了解如何将 LSTM 的 2D 数组重塑为 3D。