使用LSTM预测时间序列的多个正向时间步长 [英] Predicting a multiple forward time step of a time series using LSTM

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

问题描述

我想预测某些每周可预测的值(低SNR).我需要预测由一年的几周形成的一年的整个时间序列(52个值-图1)

I want to predict certain values that are weekly predictable (low SNR). I need to predict the whole time series of a year formed by the weeks of the year (52 values - Figure 1)

我的第一个想法是在TensorFlow上使用Keras开发多对多LSTM模型(图2).我正在用52个输入层(上一年的给定时间序列)和52个预测输出层(下一个时间序列)训练模型. train_X的形状为(X_examples,52,1),换句话说,X_examples进行训练,其中52个时间步长各1个特征.我了解Keras会将52个输入视为同一域的时间序列. train_Y的形状相同(y_examples,52、1). 我添加了一个TimeDistributed层.我的想法是,该算法会将值预测为时间序列,而不是孤立的值(我正确吗?)

My first idea was to develop a many-to-many LSTM model (Figure 2) using Keras over TensorFlow. I'm training the model with a 52 input layer (the given time series of previous year) and 52 predicted output layer (the time series of next year). The shape of train_X is (X_examples, 52, 1), in other words, X_examples to train, 52 timesteps of 1 feature each. I understand that Keras will consider the 52 inputs as a time series of the same domain. The shape of the train_Y are the same (y_examples, 52, 1). I added a TimeDistributed layer. My thought was that the algorithm will predict the values as a time series instead of isolated values (am I correct?)

在Keras中,模型的代码为:

The model's code in Keras is:

y = y.reshape(y.shape[0], 52, 1)
X = X.reshape(X.shape[0], 52, 1)
# design network
model = Sequential()
model.add(LSTM(n_neurons, input_shape=(X.shape[1], X.shape[2]), return_sequences=True))
model.add(TimeDistributed(Dense(1)))
model.compile(loss='mean_squared_error', optimizer='adam')
# fit network
model.fit(X, y, epochs=n_epochs, batch_size=n_batch, verbose=2)

问题在于算法没有学习示例.它预测的值与属性的值非常相似.我可以正确地建模问题吗?

The problem is that the algorithm is not learning the example. It is predicting values very similar to the attributes' values. Am I modeling the problem correctly?

第二个问题: 另一个想法是训练具有1个输入和1个输出的算法,但是在测试期间,如何在不考虑"1个输入"的情况下预测整个2015年时间序列?测试数据的形状将与训练数据不同.

Second question: Another idea is to train the algorithm with 1 input and 1 output, but then during the test how will I predict the whole 2015 time series without looking to the '1 input'? The test data will have a different shape than the training data.

推荐答案

对数据太少也有同样的担忧,您可以这样做.

Sharing the same concerns about having too little data, you can do that like this.

首先,将您的值保持在-1和+1之间是一个好主意,因此我首先将它们标准化.

First, it's a good idea to keep your values between -1 and +1, so I'd normalize them first.

对于LSTM模型,必须确保使用的是return_sequences=True.
您的模型没有什么错误",但是可能需要或多或少的层或单元来实现您想要的. (但是,对此没有明确的答案).

For the LSTM model, you must make sure you're using return_sequences=True.
There is nothing "wrong" with your model, but it may need more or less layers or units to achieve what you desire. (There is no clear answer to this, though).

训练模型以预测下一步:

您需要做的就是将Y转换为X:

All you need is to pass Y as a shifted X:

entireData = arrayWithShape((samples,52,1))
X = entireData[:,:-1,:]
y = entireData[:,1:,:]

使用这些训练模型.

预测未来:

现在,为了预测未来,由于我们需要将预测元素用作更多预测元素的输入,因此我们将使用循环并将模型制作为<​​c1>.

Now, for predicting the future, since we need to use predicted elements as input for more predicted elements, we are going to use a loop and make the model stateful=True.

创建与上一个模型相同的模型,并进行以下更改:

Create a model equal to the previous one, with these changes:

  • 所有LSTM层都必须具有stateful=True
  • 批处理输入形状必须为(batch_size,None, 1)-这允许长度可变
  • All LSTM layers must have stateful=True
  • The batch input shape must be (batch_size,None, 1) - This allows variable lengths

复制先前训练过的模型的权重:

Copy the weights of the previously trained model:

newModel.set_weights(oldModel.get_weights())

一次只预测一个样本,并且永远不要忘记在开始任何序列之前调用model.reset_states().

Predict only one sample at a time and never forget to call model.reset_states() before starting any sequence.

首先使用您已经知道的顺序进行预测(这将确保模型正确准备其状态以用于预测未来)

First predict with the sequence you already know (this will make sure the model prepares its states properly for predicting the future)

model.reset_states()
predictions = model.predict(entireData)

按照我们训练的方式,预测的最后一步将是未来的第一个要素:

By the way we trained, the last step in predictions will be the first future element:

futureElement = predictions[:,-1:,:]

futureElements = []
futureElements.append(futureElement)

现在,我们进行循环,以该元素为输入. (由于有状态,模型将理解它是先前序列的新输入步骤,而不是新序列)

Now we make a loop where this element is the input. (Because of stateful, the model will understand it's a new input step of the previous sequence instead of a new sequence)

for i in range(howManyPredictions):
    futureElement = model.predict(futureElement)
    futureElements.append(futureElement)


此链接包含预测两个功能的未来的完整示例: https://github.com/danmoller/TestRepo/blob/master/TestBookLSTM.ipynb

这篇关于使用LSTM预测时间序列的多个正向时间步长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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