如何使用 Keras RNN 模型预测未来的日期或事件? [英] How to use a Keras RNN model to forecast for future dates or events?

查看:48
本文介绍了如何使用 Keras RNN 模型预测未来的日期或事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我训练完整模型并保存它的代码:

Here is my code fore training the complete model and saving it:

num_units = 2
activation_function = 'sigmoid'
optimizer = 'adam'
loss_function = 'mean_squared_error'
batch_size = 10
num_epochs = 100

# Initialize the RNN
regressor = Sequential()

# Adding the input layer and the LSTM layer
regressor.add(LSTM(units = num_units, activation = activation_function, input_shape=(None, 1)))

# Adding the output layer
regressor.add(Dense(units = 1))

# Compiling the RNN
regressor.compile(optimizer = optimizer, loss = loss_function)

# Using the training set to train the model
regressor.fit(x_train, y_train, batch_size = batch_size, epochs = num_epochs)
regressor.save('model.h5')

在那之后,我看到大多数时候人们建议使用测试数据集来检查我也尝试过的预测并获得了良好的结果.

After that I have seen that most of the time people our suggesting the test dataset for checking the prediction which I have attempted as well and got good result.

但问题在于我创建的模型的使用.我想对接下来的 30 天或每一分钟进行预测.现在我有了训练有素的模型,但我没有得到我能做什么或我使用什么代码来使用模型并预测未来 30 天或一分钟的价格.

But the problem is in the usage of the model that I have created. I want to have a forecast for next 30 days or every minute whatsoever. Now I have the trained model but I am not getting what I can do or what code do I use to use the model and forecast the prices for next 30 days or one minute.

请给我建议出路.一周以来,我一直被困在这个问题上,无法进行任何成功的尝试.

Please suggest me the way out. I am stuck at this problem since a week and not able to make any successful attempts.

这是存储库的链接,您可以在其中找到完整的可运行代码、模型和数据集:我的仓库链接

Here is the link of the repository where one can find the complete runnable code, the model, and the dataset: My repository link

推荐答案

嗯,你需要一个 stateful=True 模型,这样你就可以一个接一个地提供预测以获得下一个并保持模型认为每个输入不是一个新的序列,而是前一个的续集.

Well, you need a stateful=True model, so you can feed it one prediction after another to get the next and keep the model thinking that each input is not a new sequence, but a sequel to the previous.

修复代码和训练

我在代码中看到有人试图让你的 y 成为一个 shifte x(预测下一步的好选择).但是这里的预处理也有一个很大的问题:

I see in the code that there is an attempt to make your y be a shifte x (a good option for predicting the next steps). But there is also a big problem in the preprocessing here:

training_set = df_train.values
training_set = min_max_scaler.fit_transform(training_set)

x_train = training_set[0:len(training_set)-1]
y_train = training_set[1:len(training_set)]
x_train = np.reshape(x_train, (len(x_train), 1, 1))

LSTM 层的数据必须是 (number_of_sequences, number_of_steps,features).

因此,您显然只创建了 1 个步骤的序列,这意味着您的 LSTM 根本没有学习序列.(没有序列只有一步).

So, you're clearly creating sequences of 1 step only, meaning that your LSTM is not learning sequences at all. (There is no sequence with only one step).

假设您的数据是具有 1 个特征的单个唯一序列,它肯定应该被塑造为 (1, len(x_train), 1).

Assuming that your data is a single unique sequence with 1 feature, it should definitely be shaped as (1, len(x_train), 1).

自然,y_train 也应该具有相同的形状.

Naturally, y_train should also have the same shape.

反过来,这将要求您的 LSTM 层为 return_sequences=True - 使 y 具有步进长度的唯一方法.此外,为了进行良好的预测,您可能需要一个更复杂的模型(因为现在它将是真正的学习).

This, in its turn, will require that your LSTM layers be return_sequences=True - The only way to make y have a length in steps. Also, for having a good prediction, you may need a more complex model (because now it will be trully learning).

完成后,您可以训练模型,直到获得满意的结果.

This done, you train your model until you get a satisfactory result.

预测未来

为了预测未来,您将需要 stateful=True LSTM 层.

For predicting the future, you will need stateful=True LSTM layers.

首先,您重置模型的状态:model.reset_states() - 每次将新序列输入有状态模型时都需要这样做.

Before anything, you reset the model's states: model.reset_states() - Necessary every time you're inputting a new sequence into a stateful model.

然后,首先您预测整个 X_train(这是模型理解序列的哪个点所必需的,用技术术语来说:创建状态).

Then, first you predict the entire X_train (this is needed for the model to understand at which point of the sequence it is, in technical words: to create a state).

predictions = model.predict(`X_train`) #this creates states

最后创建一个循环,从上一个预测的最后一步开始:

And finally you create a loop where you start with the last step of the previous prediction:

future = []
currentStep = predictions[:,-1:,:] #last step from the previous prediction

for i in range(future_pred_count):
    currentStep = model.predict(currentStep) #get the next step
    future.append(currentStep) #store the future steps    

#after processing a sequence, reset the states for safety
model.reset_states()

<小时>

示例

此代码使用 2 特征序列、移位的未来步骤预测以及与此答案略有不同但基于相同原理的方法来执行此操作.

This code does this with a 2-feature sequence, a shifted future step prediction, and a method that is a little different from this answer, but based on the same principle.

我创建了两个模型(一个 stateful=False,用于训练而无需每次都重置状态 - 在开始新序列时永远不要忘记重置状态 - 另一个 stateful=True,从训练好的模型中复制权重,用于预测未来)

I created two models (one stateful=False, for training without needing to reset states every time - never forget to reset states when you're starting a new sequence - and the other stateful=True, copying the weights from the trained model, for predicting the future)

https://github.com/danmoller/TestRepo/blob/master/TestBookLSTM.ipynb

这篇关于如何使用 Keras RNN 模型预测未来的日期或事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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