【问题标题】:Shape of data and LSTM Input for varying timesteps不同时间步长的数据形状和 LSTM 输入
【发布时间】:2023-03-14 02:30:01
【问题描述】:

对于我的硕士论文,我想使用 LSTM 模型来预测未来一小时的股票价格。我的 X 数据包含 30.000 行,有 6 个维度(= 6 个特征),我的 Y 数据包含 30.000 行,只有 1 个维度(=目标变量)。对于我的第一个 LSTM 模型,我将 X 数据重新整形为 (30.000x1x6),将 Y 数据重新整形为 (30.000x1),并确定如下输入: input_nn = 输入(shape=(1, 6))

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

你能解释一下 LSTM 的单元数究竟指的是什么吗?它应该与我的情况下的时间步数相同吗?

【问题讨论】:

    标签: python tensorflow keras neural-network lstm


    【解决方案1】:

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

    Timesteps 是指您的 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,...) 中的目录,让您可以选择尝试不同的采样率、时间步长等。您可能应该调查这些参数以及它们如何影响您的论文结果。

    【讨论】:

    • 你能描述一下 LSTM 中的其他单元“做什么”吗?据我了解,随着时间的推移,神经网络可以通过额外的 LSTM 层检测更复杂的行为,但这些单元指的是什么?
    • 可以把它想象成一层中神经元的数量,更多的单元/神经元也有更多的能力来学习更复杂的中间行为。层数与神经元数之间的相互作用是一个棘手的问题,并且没有一个正确的答案。
    • 我明白了,谢谢你,你帮了我很多。我对 TimeseriesGenerator 感到困惑并像这样解决了它:
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-22
      • 1970-01-01
      • 2019-01-22
      • 1970-01-01
      • 2022-01-14
      相关资源
      最近更新 更多