【问题标题】:How do you predict future predictions with an LSTM model?你如何使用 LSTM 模型预测未来的预测?
【发布时间】: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)

【问题讨论】:

    标签: python machine-learning deep-learning lstm recurrent-neural-network


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 2019-12-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-02
      • 2019-02-23
      • 1970-01-01
      • 2021-12-22
      • 2021-12-15
      • 2022-01-21
      相关资源
      最近更新 更多