繁体   English   中英

数据形状和LSTM输入适用于不同的时间步长

[英]Shape of data and LSTM Input for varying timesteps

对于我的硕士论文,我想使用LSTM模型预测下一小时的股票价格。 我的X数据包含30.000行和6维(= 6个要素),我的Y数据包含30.000行和仅1维(= target变量)。 对于我的第一个LSTM模型,我将X数据重塑为(30.000x1x6),将Y数据重塑为(30.000x1),并确定输入如下:input_nn = Input(shape =(1,6))

如果我想增加时间步长,我不确定如何重塑数据并确定模型的输入形状。 我仍然希望预测下一个小时的股价,但包括更多先前的时间步长。 我是否必须在第二维的X数据中添加先前时间步的数据?

您能解释一下LSTM的确切单位数是什么吗? 在我的情况下,时间步数应该相同吗?

使用比上一个快约5倍的代码进行更新:

x = np.load(nn_input + "/EOAN" + "/EOAN_X" + ".npy")
y = np.load(nn_input + "/EOAN" + "/EOAN_Y" + ".npy")
num_features = x.shape[1]
num_time_steps = 500

for train_index, test_index in tscv.split(x):
# Split into train and test set
print("Fold:", fold_counter, "\n" + "Train Index:", train_index, "Test Index:", test_index)
x_train_raw, y_train, x_test_raw, y_test = x[train_index], y[train_index], x[test_index], y[test_index]

# Scaling the data
scaler = StandardScaler()
scaler.fit(x_train_raw)
x_train_raw = scaler.transform(x_train_raw)
x_test_raw = scaler.transform(x_test_raw)

# Creating Input Data with variable timesteps
x_train = np.zeros((x_train_raw.shape[0] - num_time_steps + 1, num_time_steps, num_features), dtype="float32")
x_test = np.zeros((x_test_raw.shape[0] - num_time_steps + 1, num_time_steps, num_features), dtype="float32")

for row in range(len(x_train)):
    for timestep in range(num_time_steps):
        x_train[row][timestep] = x_train_raw[row + timestep]

for row in range(len(x_test)):
    for timestep in range(num_time_steps):
        x_test[row][timestep] = x_test_raw[row + timestep]

y_train = y_train[num_time_steps - 1:]
y_test = y_test[num_time_steps - 1:]

您处在正确的轨道上,但混淆了单位数量和时间步长。 units是控制LSTM输出尺寸的超参数。 它是LSTM输出向量的维度,因此,如果输入为(1,6)并且您有32个单位,则将获得(32,)因为LSTM中将遍历单个时间步长并生成大小为32的向量。

时间步长是指您的LSTM可以考虑的历史记录的大小。 因此,它与单位完全不同。 Keras拥有一个方便的TimeseriesGenerator ,而不是自己处理数据,它将像您一样获取2D数据,并使用某个时间步长的滑动窗口来生成时间序列数据。 从文档中:

from keras.preprocessing.sequence import TimeseriesGenerator
import numpy as np

data = np.array([[i] for i in range(50)])
targets = np.array([[i] for i in range(50)])

data_gen = TimeseriesGenerator(data, targets,
                               length=10, sampling_rate=2,
                               batch_size=2)
assert len(data_gen) == 20

batch_0 = data_gen[0]
x, y = batch_0
assert np.array_equal(x,
                      np.array([[[0], [2], [4], [6], [8]],
                                [[1], [3], [5], [7], [9]]]))
assert np.array_equal(y,
                      np.array([[10], [11]]))

您可以在model.fit_generator(data_gen,...)使用目录,让您可以选择尝试不同的采样率,时间步长等。您可能应该研究这些参数以及它们如何影响论文的结果。

暂无
暂无

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

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