Python 不同时间步长的数据形状和LSTM输入

Python 不同时间步长的数据形状和LSTM输入,python,tensorflow,keras,neural-network,lstm,Python,Tensorflow,Keras,Neural Network,Lstm,在我的硕士论文中,我想用LSTM模型预测未来一小时的股票价格。My X数据包含30000行,其中包含6个维度(=6个要素),My Y数据包含30000行,仅包含1个维度(=目标变量)。对于我的第一个LSTM模型,我将X数据重塑为(30.000x1x6),Y数据重塑为(30.000x1),并确定如下输入: 输入=输入(形状=(1,6)) 如果我想增加时间步长,我不确定如何重塑数据和确定模型的输入形状。我仍然想预测下一个小时的股价,但包括更多以前的时间步骤。 我是否必须在第二维度的X数据中添加以前时

在我的硕士论文中,我想用LSTM模型预测未来一小时的股票价格。My X数据包含30000行,其中包含6个维度(=6个要素),My Y数据包含30000行,仅包含1个维度(=目标变量)。对于我的第一个LSTM模型,我将X数据重塑为(30.000x1x6),Y数据重塑为(30.000x1),并确定如下输入: 输入=输入(形状=(1,6))

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


你能解释一下LSTM的单位数是指什么吗?在我的例子中,它是否应该与时间步数相同?

您的思路是正确的,但将单位数与时间步数混淆了。
单位
是一个超参数,用于控制LSTM的输出尺寸。它是LSTM输出向量的维数,因此如果输入是
(1,6)
,并且您有32个单位,您将得到
(32,)
,因为LSTM将遍历单个时间步并生成大小为32的向量

时间步长指的是你可以考虑的历史的大小。所以它和单位根本不一样。Keras没有自己处理数据,而是有一个方便的工具,它将获取像您一样的2D数据,并使用一些时间步长大小的滑动窗口来生成timeseries数据。从文件中:

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]]))

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

使用比上一个快大约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:]

你能描述一下LSTM中的其他单位是做什么的吗?据我所知,随着时间的推移,神经网络可以通过额外的LSTM层检测到更复杂的行为,但单位指的是什么?可以将其视为一层中的神经元数量,更多的单位/神经元也有更大的能力学习更复杂的中间行为。层数与神经元数之间的相互作用是一个棘手的问题,没有一个正确的答案。我明白了,谢谢你,努里奇,你帮了我很多。我被TimeseriesGenerator搞糊涂了,并这样解决了它: