使用 Keras 进行多输出、多时间步长序列预测

数据挖掘 喀拉斯 张量流 时间序列 序列到序列 多输出
2022-02-20 12:40:38

我已经搜索了大约三个小时,但找不到一个非常简单问题的答案。

我有一个时间序列预测问题。我正在尝试使用 Keras LSTM 模型(最后是 Dense)使用多个输入和一个移动窗口来预测多个时间步长多个输出。我想做序列到序列的预测,我的模型在每个时间步的输出上进行训练,而不仅仅是最后一个。

我的目标应该是什么形状?我的输入是一个形状数组(number_of_moving_windows、input_window_length、number_of_features)。我的输出应该是(number_of_moving_windows、output_window_length、number_of_series_to_predict)吗?或者也许(number_of_moving_windows,output_window_length*number_of_series_to_predict)?要不然是啥?


Aurelien Geron 的教科书“Hands-on Machine Learning with Scikit-Learn, Keras, and TensorFlow (2nd edition)”提供了以下代码,用于创建单输出、10 时间步、序列到序列的目标数组:

# series is a (batch_size, time_steps, 1) NumPy array of random time series 
# where batch_size=10000 and time_steps=n_steps+10
Y = np.empty((10000, n_steps, 10)) # each target is a sequence of 10D vectors 
for step_ahead in range(1, 10 + 1): 
    Y[:, :, step_ahead - 1] = series[:, step_ahead:step_ahead + n_steps, 0] # This zero will drop a dimension
Y_train = Y[:7000] 
Y_valid = Y[7000:9000] 
Y_test = Y[9000:]

如何将其更改为具有 10 个时间步长、3 个输出的目标?我的目标形状应该是 (batch_size, 10, 3) 还是 (batch_size, 30) 还是什么?

另外,我是否将网络中的最后一个 Dense 层设为 Dense(30)?


编辑:

例如,假设我的数据是这个数据框:

import pandas as pd
import numpy as np

dummy_data = np.concatenate([np.arange(100, 113).reshape(-1, 1), 
                        np.arange(200, 213).reshape(-1, 1), 
                        np.arange(300, 313).reshape(-1, 1)], 
                       axis=1)

dummy_data = pd.DataFrame(dummy_data, columns=["A", "B", "C"])

     A       B       C
0   100     200     300
1   101     201     301
2   102     202     302
3   103     203     303
4   104     204     304
5   105     205     305
6   106     206     306
7   107     207     307
8   108     208     308
9   109     209     309
10  110     210     310
11  111     211     311
12  112     212     312

我想预测,对于四个输入时间步(t-3、t-2、t-1、t)的每个窗口,对于所有 A、B 和 C 一起,在以下三个时间步(t+1, t+2,t+3)。所以 input_window_length 是 4,number_of_series_to_predict 等于 number_of_features 等于 3,而 output_window_length 是 3。这意味着 number_of_moving_windows 是 4。

然后我的训练集的窗口输入是:

np.array([[[100, 200, 300],
         [101, 201, 301],
         [102, 202, 302],
         [103, 203, 303]],
 
        [[101, 201, 301],
         [102, 202, 302],
         [103, 203, 303],
         [104, 204, 304]],
 
        [[102, 202, 302],
         [103, 203, 303],
         [104, 204, 304],
         [105, 205, 305]],
 
        [[103, 203, 303],
         [104, 204, 304],
         [105, 205, 305],
         [106, 206, 306]]])

我在问相应目标的形状。我是否创建一个形状数组 (number_of_moving_windows, output_window_length*number_of_series_to_predict),即 (4, 9)?像这个:

np.array([[104, 204, 304, 105, 205, 305, 106, 206, 306],
        [105, 205, 305, 106, 206, 306, 107, 207, 307],
        [106, 206, 306, 107, 207, 307, 108, 208, 308],
        [107, 207, 307, 108, 208, 308, 109, 209, 309]])

还是我让它(number_of_moving_windows、output_window_length、number_of_series_to_predict),即(4、3、3)?像这个:

np.array([[[104, 105, 106],
        [105, 106, 107],
        [106, 107, 108],
        [107, 108, 109]],

       [[204, 205, 206],
        [205, 206, 207],
        [206, 207, 208],
        [207, 208, 209]],

       [[304, 305, 306],
        [305, 306, 307],
        [306, 307, 308],
        [307, 308, 309]]])

或者 (number_of_moving_windows, number_of_series_to_predict, output_window_length), 即 (4, 3, 3) 再次,但最后两个维度交换了?像这个:

np.array([[[104, 204, 304],
        [105, 205, 305],
        [106, 206, 306],
        [107, 207, 307]],

       [[105, 205, 305],
        [106, 206, 306],
        [107, 207, 307],
        [108, 208, 308]],

       [[106, 206, 306],
        [107, 207, 307],
        [108, 208, 308],
        [109, 209, 309]]])

感谢您的帮助。

1个回答
  • 要获得每一步的输出,您必须return_sequence=True为所有 LSTM 层
  • 最后一个 Dense 层应反映输出大小,即此处为 3
  • 因此, Y = (total instances, n_steps, output size)的形状