【问题标题】:Predicted values does not fit test values LSTM Keras预测值不适合测试值 LSTM Keras
【发布时间】:2020-09-03 21:24:18
【问题描述】:

我正在尝试拟合 LSTM 模型,并且在进行预测时,预测值似乎与测试数据不太吻合。可以在这里看到。

这是我的代码:

依赖关系

import pandas as pd
from pandas_datareader import data as pdr
import numpy as np
import matplotlib.pyplot as plt
import yfinance as yf
%matplotlib inline

#ML stuff
from sklearn.preprocessing import MinMaxScaler
from keras.preprocessing.sequence import TimeseriesGenerator
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout

导入数据:

yf.pdr_override()
df = pdr.get_data_yahoo("0118.KL",start="2018-01-01",end="2020-04-30")

df.reset_index(inplace=True,drop=False)

df.columns = [str(x).lower().replace(' ','_') for x in df.columns]

df = df.drop(['open','high','low','close','volume'],axis=1)

df = df.set_index('date')
df.tail()

数据准备:

n_input = 10 #because I want to forecast 10 days into the future, use past 10 days to derive future value
n_features=1
train,test=df[:-n_input],df[-n_input:]

scaler = MinMaxScaler(feature_range=(0,1))
scaler.fit(train)
train = scaler.transform(train) #returns 2d array, of [inputs/time_steps,features] which is 1, because univariate timeseries
test = scaler.transform(test)

火车模型:

generator = TimeseriesGenerator(train,train,length=n_input,batch_size=128) #batch size means the number of samples shown before updating weight of NN
model=Sequential()
model.add(LSTM(200,activation='relu',input_shape=(n_input,n_features)))
model.add(Dropout(0.2))
model.add(Dense(1))
model.compile(optimizer='adam',loss='mse')
model.fit_generator(generator,epochs=100)

预测:

pred_list = [] #list of arrays, in which each array has one value
batch = train[-n_input:].reshape(1,n_input,n_features)
#it takes the prediction,appends it to the next batch, then use it to predict the next value, like a moving window that updates
# with the predicted value
for i in range(n_input):
    pred_list.append(model.predict(batch)[0]) #one value,returns a list of value, which has only one value actually
    batch = np.append(batch[:,1:,:],[[pred_list[i]]],axis=1)#append to the end of the batch list,axis=1 column

df_predict = pd.DataFrame(scaler.inverse_transform(pred_list),index = df[-n_input:].index,columns=['Predictions'])

df_test = pd.concat([df,df_predict],axis=1)

显示上面可以看到的图表:

plt.figure(figsize=(12,4))
plt.plot(df_test['adj_close'][-n_input:])
plt.plot(df_test['Predictions'],color="r")
plt.show()

有什么线索可以解释为什么会这样吗?感谢您的阅读,欢迎提出任何意见。

【问题讨论】:

    标签: python tensorflow keras lstm


    【解决方案1】:

    您的模型正在预测小动作,只是它不够灵敏。

    你可以研究一些事情,比如, 1. 神经网络权重矩阵的初始化。 2. 调整batch_size 3. 调整epochs 的数量,可能需要更多的epoch,这严重“欠拟合”。

    另外,我建议单独处理 COVID 持续时间数据。

    【讨论】:

    • 初始化矩阵是什么意思?
    • 任何神经网络都有一个权重矩阵,它会根据网络的反向传播属性自动调整。但是,您初始化这些权重的起始值的方式可能会给您带来 NN 预测/回归方式的巨大差异。 deeplearning.ai/ai-notes/initialization 是一个很好的演示。这些权重初始值可能有助于权重/成本超空间中的成本最小化算法达到“更好”的最小值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 2017-12-07
    • 2021-04-21
    • 1970-01-01
    • 2021-12-13
    相关资源
    最近更新 更多