如何使用Keras RNN模型预测未来的日期或事件? [英] How to use a Keras RNN model to forecast for future dates or events?
问题描述
这是我训练完整模型并保存它的代码:
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
转变为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)
.
Data for LSTM
layers must be shaped as (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屋!