如何处理keras中多元LSTM中的多步时间序列预测 [英] How to deal with multi step time series forecasting in multivariate LSTM in keras

查看:144
本文介绍了如何处理keras中多元LSTM中的多步时间序列预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Keras 中使用多元 LSTM 进行多步时间序列预测.具体来说,我最初为每个时间步长有两个变量(var1 和 var2).跟着在线教程

解决方案

问题一:

从您的表格中,我看到您在单个序列上有一个滑动窗口,用 2 个步骤制作了许多较小的序列.

  • 为了预测 t,您将表格的第一行作为输入
  • 为了预测 t+1,您将第二行作为输入.

如果您不使用表格:请参阅问题 3

问题2:

假设您使用该表作为输入,这显然是一个滑动窗口案例,需要两个时间步作为输入,您的 timeSteps 为 2.

您可能应该将 var1var2 视为相同顺序的特征:

  • input_shape = (2,2) - 两个时间步长和两个特征/变量.

问题3:

我们不需要制作这样的桌子或构建滑动窗口案例.这是一种可能的方法.

您的模型实际上能够学习并决定此窗口本身的大小.

如果一方面您的模型能够学习长时间依赖,允许您不使用窗口,另一方面,它可以学习识别序列开头和中间的不同行为.在这种情况下,如果您想使用从中间开始的序列(不包括开头)进行预测,您的模型可能会像开头一样工作并预测不同的行为.使用窗户可以消除这种长期的影响.我猜哪个更好可能取决于测试.

不使用窗口:

如果您的数据有 800 个步骤,请一次性输入所有 800 个步骤以进行训练.

在这里,我们需要分离两个模型,一个用于训练,另一个用于预测.在训练中,我们将利用参数 return_sequences=True.这意味着对于每个输入步骤,我们都会得到一个输出步骤.

为了以后的预测,我们只需要一个输出,然后我们将使用 return_sequences=False.如果我们要将预测输出用作后续步骤的输入,我们将使用 stateful=True 层.

培训:

将您的输入数据设置为 (1, 799, 2),1 个序列,从 1 到 799 执行步骤.两个变量的序列相同(2 个特征).

将目标数据 (Y) 的形状也设置为 (1, 799, 2),采用相同的步骤,从 2 移动到 800.

使用 return_sequences=True 构建模型.您可以使用 timeSteps=799,但也可以使用 None(允许可变的步数).

model.add(LSTM(units, input_shape=(None,2), return_sequences=True))model.add(LSTM(2, return_sequences=True)) #也可以是 Dense 2....……模型.fit(X, Y, ....)

预测:

为了预测,创建一个类似的模型,现在使用 return_sequences=False.

复制权重:

newModel.set_weights(model.get_weights())

您可以输入长度为 800 的输入,例如(形状:(1,800,2))并预测下一步:

step801 = newModel.predict(X)

如果您想预测更多,我们将使用 stateful=True 层.再次使用相同的模型,现在使用 return_sequences=False(仅在最后一个 LSTM 中,其他保持 True)和 stateful=True(所有这些).将 input_shape 更改为 batch_input_shape=(1,None,2).

#with stateful=True,你的模型永远不会认为序列结束#每个新批次将被视为新步骤而不是新序列#因此,当我们想要一个从零开始的序列时,我们需要调用它:statefulModel.reset_states()#预测X = step1to800 #输入step801 = statefulModel.predict(X).reshape(1,1,2)step802 = statefulModel.predict(step801).reshape(1,1,2)step803 = statefulModel.predict(step802).reshape(1,1,2)#reshape是因为return_sequences=True消除了step维度

实际上,您可以使用单个 stateful=Truereturn_sequences=True 模型来做所有事情,需要处理两件事:

  • 训练时,reset_states() 用于每个 epoch.(使用手动循环和 epochs=1 进行训练)
  • 当从多个步骤进行预测时,仅将输出的最后一步作为预期结果.

I am trying to do multi-step time series forecasting using multivariate LSTM in Keras. Specifically, I have two variables (var1 and var2) for each time step originally. Having followed the online tutorial here, I decided to use data at time (t-2) and (t-1) to predict the value of var2 at time step t. As sample data table shows, I am using the first 4 columns as input, Y as output. The code I have developed can be seen here, but I have got three questions.

   var1(t-2)  var2(t-2)  var1(t-1)  var2(t-1)  var2(t)
2        1.5       -0.8        0.9       -0.5     -0.2
3        0.9       -0.5       -0.1       -0.2      0.2
4       -0.1       -0.2       -0.3        0.2      0.4
5       -0.3        0.2       -0.7        0.4      0.6
6       -0.7        0.4        0.2        0.6      0.7

  1. Q1: I have trained an LSTM model with the data above. This model does well in predicting the value of var2 at time step t. However, what if I want to predict var2 at time step t+1. I feel it is hard because the model cannot tell me the value of var1 at time step t. If I want to do it, how should I modify the code to build the model?
  2. Q2: I have seen this question asked a lot, but I am still confused. In my example, what should be the correct time step in [samples, time steps, features] 1 or 2?
  3. Q3: I just started studying LSTMs. I have read here that one of the biggest advantages of LSTM is that it learns the temporal dependence/sliding window size by itself, then why must we always covert time series data into format like the table above?

Update: LSTM result (blue line is the training seq, orange line is the ground truth, green is the prediction)

解决方案

Question 1:

From your table, I see you have a sliding window over a single sequence, making many smaller sequences with 2 steps.

  • For predicting t, you take first line of your table as input
  • For predicting t+1, you take the second line as input.

If you're not using the table: see question 3

Question 2:

Assuming you're using that table as input, where it's clearly a sliding window case taking two time steps as input, your timeSteps is 2.

You should probably work as if var1 and var2 were features in the same sequence:

  • input_shape = (2,2) - Two time steps and two features/vars.

Question 3:

We do not need to make tables like that or build a sliding window case. That is one possible approach.

Your model is actually capable of learning things and deciding the size of this window itself.

If on one hand your model is capable of learning long time dependencies, allowing you not to use windows, on the other hand, it may learn to identify different behaviors at the beginning and at the middle of a sequence. In this case, if you want to predict using sequences that start from the middle (not including the beginning), your model may work as if it were the beginning and predict a different behavior. Using windows eliminate this very long influence. Which is better may depend on testing, I guess.

Not using windows:

If your data has 800 steps, feed all the 800 steps at once for training.

Here, we will need to separate two models, one for training, another for predicting. In training, we will take advantage of the parameter return_sequences=True. This means that for each input step, we will get an output step.

For predicting later, we will want only one output, then we will use return_sequences= False. And in case we are going to use the predicted outputs as inputs for following steps, we are going to use a stateful=True layer.

Training:

Have your input data shaped as (1, 799, 2), 1 sequence, taking the steps from 1 to 799. Both vars in the same sequence (2 features).

Have your target data (Y) shaped also as (1, 799, 2), taking the same steps shifted, from 2 to 800.

Build a model with return_sequences=True. You may use timeSteps=799, but you may also use None (allowing variable amount of steps).

model.add(LSTM(units, input_shape=(None,2), return_sequences=True))
model.add(LSTM(2, return_sequences=True)) #it could be a Dense 2 too....
....
model.fit(X, Y, ....)

Predicting:

For predicting, create a similar model, now with return_sequences=False.

Copy the weights:

newModel.set_weights(model.get_weights())

You can make an input with length 800, for instance (shape: (1,800,2)) and predict just the next step:

step801 = newModel.predict(X)

If you want to predict more, we are going to use the stateful=True layers. Use the same model again, now with return_sequences=False (only in the last LSTM, the others keep True) and stateful=True (all of them). Change the input_shape by batch_input_shape=(1,None,2).

#with stateful=True, your model will never think that the sequence ended  
#each new batch will be seen as new steps instead of new sequences
#because of this, we need to call this when we want a sequence starting from zero:
statefulModel.reset_states()

#predicting
X = steps1to800 #input
step801 = statefulModel.predict(X).reshape(1,1,2)
step802 = statefulModel.predict(step801).reshape(1,1,2)
step803 = statefulModel.predict(step802).reshape(1,1,2)
    #the reshape is because return_sequences=True eliminates the step dimension   

Actually, you could do everything with a single stateful=True and return_sequences=True model, taking care of two things:

  • When training, reset_states() for every epoch. (Train with a manual loop and epochs=1)
  • When predicting from more than one step, take only the last step of the output as the desired result.

这篇关于如何处理keras中多元LSTM中的多步时间序列预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆