您的位置:首页 > 编程语言 > Python开发

愉快的学习就从翻译开始吧_7-Time Series Forecasting with the Long Short-Term Memory Network in Python

2018-06-12 18:16 399 查看

LSTM Forecast/LSTM预测

Once the LSTM model is fit to the training data, it can be used to make forecasts.一旦LSTM模型适配训练数据,它就可以用来进行预测。
Again, we have some flexibility. We can decide to fit the model once on all of the training data, then predict each new time step one at a time from the test data (we’ll call this the fixed approach), or we can re-fit the model or update the model each time step of the test data as new observations from the test data are made available (we’ll call this the dynamic approach).再次,我们有一些灵活性。我们可以决定模型一次拟合在所有训练数据,然后根据测试数据一次一个的预测每个新的时间步(我们称之为固定方法),或者我们可以重新拟合模型或每当从测试数据中提供新观测值时就更新一次模型(我们称之为动态方法)(这表述也是服了,翻来覆去的搞脑子,大概意思应该就是测试的时候也更新模型的权重,每测试一个数据就更新一次权重,这个可以有,对于有些问题太久远的数据可能影响很小,但前面几步的影响可能很大,这时候动态更新权重就很重要了,刚看到一个名词注意力模型,可能就是这个东西,哈哈)。
In this tutorial, we will go with the fixed approach for its simplicity, although, we would expect the dynamic approach to result in better model skill.在本教程中,我们将使用固定的方法来简化它,但我们期望动态方法能够提高模型技巧(唉,又被坑了,动态方法才是有价值的,这个固定方法没有多大价值,也罢学了这个再看动态的应该会更好理解了,动态的肯定只是这个的稍微变化)。
To make a forecast, we can call the predict() function on the model. This requires a 3D NumPy array input as an argument. In this case, it will be an array of one value, the observation at the previous time step.为了做出预测,我们可以在模型中调用predict()函数。这需要3D NumPy数组输入作为参数。本例中,它将是一个值的数组,即前一时间步的观察值。
The predict() function returns an array of predictions, one for each input row provided. Because we are providing a single input, the output will be a 2D NumPy array with one value.predict()函数返回一个预测数组,每个输入行提供一个。因为我们提供了单个输入,所以输出将是一个具有一个值的2D NumPy数组。
We can capture this behavior in a function named forecast() listed below. Given a fit model, a batch-size used when fitting the model (e.g. 1), and a row from the test data, the function will separate out the input data from the test row, reshape it, and return the prediction as a single floating point value.我们可以在下面列出的名为forecast()的函数中捕获此行为。给定一个拟合模型(model),在拟合模型时使用的批量大小(batch_size)和测试数据中的一行(row),该函数将从测试行中分离出输入数据,重新塑造它,并返回单一浮点型预测值。

def forecast(model, batch_size, row):
X = row[0:-1]
X = X.reshape(1, 1, len(X))
yhat = model.predict(X, batch_size=batch_size)
return yhat[0,0]

During training, the internal state is reset after each epoch. While forecasting, we will not want to reset the internal state between forecasts. In fact, we would like the model to build up state as we forecast each time step in the test dataset.

在训练过程中,内部状态在每个纪元后重置(重置什么呀?)。 在进行预测时,我们不希望在预测之间重置内部状态。 事实上,我们希望在预测测试数据集中每个时间步时模型能够建立状态(这特么不成了动态方法了吗?真的是乱)。
This raises the question as to what would be a good initial state for the network prior to forecasting the test dataset.这引发了一个问题,即在预测测试数据集之前,什么才是网络的好的初始状态。
In this tutorial, we will seed the state by making a prediction on all samples in the training dataset. In theory, the internal state should be set up ready to forecast the next time step.在本教程中,我们将通过对训练数据集中的所有样本进行预测来为该状态播种。 理论上,内部状态应该已经准备就绪,可以预测下一个时间步骤。
We now have all of the pieces to fit an LSTM Network model for the Shampoo Sales dataset and evaluate its performance.我们现在拥有所有的部件来适配Shampoo Sales数据集的LSTM网络模型并评估其性能。
In the next section, we will put all of these pieces together.在下一节中,我们将把所有这些部分放在一起。

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐