你如何使用 LSTM 模型预测未来的预测?

Posted

技术标签:

【中文标题】你如何使用 LSTM 模型预测未来的预测?【英文标题】:How do you predict future predictions with an LSTM model? 【发布时间】:2020-08-24 14:28:33 【问题描述】:

您如何使用此模型预测未来价值?我尝试将时间步长窗口更改为比股票数据库更大的值,但我只得到一个错误,说元组索引超出范围。如何预测未来值,而不是在现有数据上测试模型?这是我的代码:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

dataset_train = pd.read_csv(r'/path', error_bad_lines = False)
training_set = dataset_train.iloc[:, 1:2].values

from sklearn.preprocessing import MinMaxScaler
sc = MinMaxScaler(feature_range = (0, 1))
sc_training_set = sc.fit_transform(training_set)

X_train = []
y_train = []
for i in range (1, 220):
    X_train.append(sc_training_set[i-1:i, 0])
    y_train.append(sc_training_set[i, 0])

X_train, y_train = np.array(X_train), np.array(y_train)

X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))

from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout

regressor = Sequential()

regressor.add(LSTM(units = 64, return_sequences = True, input_shape = (X_train.shape[1], 1)))
regressor.add(Dropout(0.2))

regressor.add(LSTM(units = 128, return_sequences = True))
regressor.add(Dropout(0.2))

regressor.add(LSTM(units = 256, return_sequences = True))
regressor.add(Dropout(0.2))

regressor.add(LSTM(units = 512, return_sequences = True))
regressor.add(Dropout(0.2))

regressor.add(LSTM(units = 256, return_sequences = True))
regressor.add(Dropout(0.2))

regressor.add(LSTM(units = 128, return_sequences = True))
regressor.add(Dropout(0.2))

regressor.add(LSTM(units = 64))
regressor.add(Dropout(0.2))

regressor.add(Dense(units = 1))

regressor.compile(optimizer = 'adam', loss = 'mean_squared_error', metrics = ['accuracy'])

regressor.fit(X_train, y_train, epochs = 10, batch_size = 32)

dataset_test = []
X_test = []

for i in range(220, 500):
    X_test.append(sc_training_set[i-1:i, 0])
X_test = np.array(X_test)
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))

pred_stock = regressor.predict(X_test)
pred_stock = sc.inverse_transform(pred_stock)

【问题讨论】:

【参考方案1】:

这里有一些用于未来预测的伪代码。本质上,您需要不断地将最近的预测添加到您的时间序列中。

您不能只增加时间步长,否则您最终会尝试访问超出范围的索引。

predictions = []
last_x = (the last x value in your data)
while len(predictions) < #_of_predictions_you_want:
    p = model.predict(last_x)
    predictions.append(p)
    last_x = np.roll(x, -1)
    last_x[-1] = p

【讨论】:

谢谢!我现在就试试! 我试过了,它给了我一个错误:ValueError: cannot reshape array of size 1 into shape (746,1,1)。我该如何解决?【参考方案2】:

也许你可以把这个添加到肖恩的回答中

last_x=np.reshape(len(last_x),1,1)

为了完成,

 predictions = []
    last_x = (the last x value in your data)
    last_x=np.reshape(len(last_x),1,1)
    while len(predictions) < #_of_predictions_you_want:
        p = model.predict(last_x)
        predictions.append(p)
        last_x = np.roll(x, -1)
        last_x[-1] = p
        last_x=np.reshape(len(last_x),1,1)

【讨论】:

以上是关于你如何使用 LSTM 模型预测未来的预测?的主要内容,如果未能解决你的问题,请参考以下文章

测试经过训练的 LSTM 模型后如何预测实际的未来值?

如何让我的 LSTM 模型在训练后进行预测

如何在 LSTM 模型中将未来预测用作时间序列的输入变量?

如何使用 LSTM 单元训练 RNN 以进行时间序列预测

使用 LSTM 进行预测

python如何预测下一年的数据