Keras预测问题(做出一些错误的预测,然后只是预测一些接近1的值)

时间:2019-04-02 21:52:44

标签: keras lstm recurrent-neural-network

我已经b了一段时间。我对此有很多问题,无法弄清楚我在做什么错。这是预测(红色)与实际(蓝色)的图像:

enter image description here

我的代码如下:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
from keras.preprocessing.sequence import TimeseriesGenerator
from keras.optimizers import RMSprop
import inspect

data = pd.read_csv("D:\\School\\Spring_2019\\GraduateProject\\Stock Data\\AMZN.csv",header=0,usecols=['Date','Close'],index_col='Date',parse_dates=True)

# Data Scaling
scaler = MinMaxScaler()
data_s = scaler.fit_transform(data)
data_s

# Data Splitting into Train, Test, and Validation
trainLen = int(0.5*len(data_s))
train = data_s[0:trainLen,:]

valLen = int(0.25*len(data_s))
validation = data_s[trainLen:(trainLen+valLen),:]

test = data_s[trainLen+valLen:,:]

# Generators
trainGen = TimeseriesGenerator(data=train,targets=train,
                               length=1,sampling_rate=1,batch_size=1)

valGen = TimeseriesGenerator(data=validation,targets=validation,
                               length=1,sampling_rate=1,batch_size=1)

testGen = TimeseriesGenerator(data=test,targets=test,
                               length=1,sampling_rate=1,batch_size=1)

# Network Design
AMZN = Sequential()
AMZN.add(LSTM(256,input_shape=(1,1)))
AMZN.add(Dense(1,activation='sigmoid'))
AMZN.summary()

AMZN.compile(loss='mean_absolute_error',optimizer=RMSprop()) #,metrics=['accuracy']

# Model Fitting
epochSteps = int(trainLen/2)
epochSteps_v = int(valLen/2)
history = AMZN.fit_generator(trainGen,steps_per_epoch = trainLen,epochs=2,verbose=1,validation_data=valGen,validation_steps=epochSteps_v)

predicted = AMZN.predict_generator(testGen,verbose=1)
#test_r = np.reshape(test,(-1,1,1))
#predicted = AMZN.predict(test_r)
predicted

plt.plot(predicted,color='red')
plt.plot(test,color='blue')

请让我知道你们中谁能知道发生了什么事?我从字面上一直在左右改变,但我一直得到完全相同的结果。 3个或20个时间段都没关系。当我尝试在第二层LSTM中添加第二层LSTM时,一切就开始了。现在我不知道为什么会遇到所有这些问题。到目前为止,我已经尝试过更改损失函数,优化器,纪元,尝试摆脱模型拟合中的验证部分。我非常感谢。

编辑:在这种情况下,一切正常运行,但是我还同时出现以下错误:

Blas GEMM启动失败

0 个答案:

没有答案