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

查看:26
本文介绍了使用 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)

我的第一个想法是使用 Keras over TensorFlow 开发多对多 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 个输出来训练算法,但是在测试期间,我将如何预测整个 2015 年时间序列而不查看1 个输入"?测试数据的形状与训练数据不同.

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:,:]

使用这些训练模型.

预测未来:

现在,为了预测未来,由于我们需要使用预测元素作为更多预测元素的输入,我们将使用循环并使模型 stateful=True.

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天全站免登陆