简体   繁体   English

如何重塑keras LSTM的输入?

[英]How to reshape input for keras LSTM?

I have a numpy array of some 5000 rows and 4 columns (temp, pressure, speed, cost). 我有一个约5000行和4列(温度,压力,速度,成本)的numpy数组。 So this is of the shape (5000, 4). 因此,它的形状为(5000,4)。 Each row is an observation at a regular interval this is the first time i'm doing time series prediction and I'm stuck on input shape. 每行都是按固定间隔进行的观察,这是我第一次进行时间序列预测,并且卡在输入形状上。 I'm trying to predict a value 1 timestep from the last data point. 我正在尝试从最后一个数据点开始预测1个时间步长的值。 How do I reshape it into the 3D form for LSTM model in keras? 如何在keras中将其重塑为LSTM模型的3D形式?

Also It will be much more helpful if a small sample program is written. 如果编写一个小的示例程序,它也将更加有用。 There doesn't seem to be any example/tutorial where the input has more than one feature (and also not NLP). 似乎没有任何示例/教程中的输入具有多个功能(也没有NLP)。

The first question you should ask yourself is : 您应该问自己的第一个问题是:

  • What is the timescale in which the input features encode relevant information for the value you want to predict? 输入要素对要预测的值的相关信息进行编码的时间范围是什么?

Let's call this timescale prediction_context . 让我们把这个时间表prediction_context

You can now create your dataset : 您现在可以创建数据集:

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)

At the end : 在末尾 :
X_train.shape = (recording_length - prediction_context, prediction_context, n_features)
So you will need to make a trade-off between the length of your prediction context and the number of examples you will have to train your network. 因此,您将需要在预测上下文的长度和训练网络的示例数量之间进行权衡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM