lstm神经网络异常输出相同的值
Posted
技术标签:
【中文标题】lstm神经网络异常输出相同的值【英文标题】:lstm neural network abnormally output the same value 【发布时间】:2017-09-21 14:28:40 【问题描述】:我目前正在研究并尝试改进一个 python 脚本,其目的是预测股市走势(一个非常简单的)。
问题:我得到相同的输出值,但我真的不明白为什么,因为它应该更像这张图表(红线是我应该得到的预测,蓝线是真实数据) :https://i.stack.imgur.com/dvQvY.png
代码如下:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from pandas import datetime
import math, time
import itertools
from sklearn import preprocessing
import datetime
from operator import itemgetter
from sklearn.metrics import mean_squared_error
from math import sqrt
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.layers.recurrent import LSTM
def get_stock_data(stock_name, normalized=0):
url = 'http://chart.finance.yahoo.com/table.csv?s=%s&a=11&b=15&c=2011&d=29&e=10&f=2016&g=d&ignore=.csv' % stock_name
col_names = ['Date','Open','High','Low','Close','Volume','Adj Close']
stocks = pd.read_csv(url, header=0, names=col_names)
df = pd.DataFrame(stocks)
date_split = df['Date'].str.split('-').str
df['Year'], df['Month'], df['Day'] = date_split
df["Volume"] = df["Volume"] / 10000
df.drop(df.columns[[0,3,5,6, 7,8,9]], axis=1, inplace=True)
return df
stock_name = 'GOOGL'
df = get_stock_data(stock_name,0)
df.head()
today = datetime.date.today()
file_name = stock_name+'_stock_%s.csv' % today
df.to_csv(file_name)
df['High'] = df['High'] / 100
df['Open'] = df['Open'] / 100
df['Close'] = df['Close'] / 100
df.head(5)
def load_data(stock, seq_len):
amount_of_features = len(stock.columns)
data = stock.as_matrix() #pd.DataFrame(stock)
sequence_length = seq_len + 1
result = []
for index in range(len(data) - sequence_length):
result.append(data[index: index + sequence_length])
result = np.array(result)
row = round(0.9 * result.shape[0])
train = result[:int(row), :]
x_train = train[:, :-1]
y_train = train[:, -1][:,-1]
x_test = result[int(row):, :-1]
y_test = result[int(row):, -1][:,-1]
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], amount_of_features))
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], amount_of_features))
return [x_train, y_train, x_test, y_test]
def build_model2(layers):
d = 0.2
model = Sequential()
model.add(LSTM(128, input_shape=(layers[1], layers[0]), return_sequences=True))
model.add(Dropout(d))
model.add(LSTM(64, input_shape=(layers[1], layers[0]), return_sequences=False))
model.add(Dropout(d))
model.add(Dense(16,init='uniform',activation='relu'))
model.add(Dense(1,init='uniform',activation='linear'))
model.compile(loss='mse',optimizer='adam',metrics=['accuracy'])
return model
window = 22
X_train, y_train, X_test, y_test = load_data(df[::-1], window)
print("X_train", X_train.shape)
print("y_train", y_train.shape)
print("X_test", X_test.shape)
print("y_test", y_test.shape)
model = build_model2([3,window,1])
model.fit(
X_train,
y_train,
batch_size=512,
nb_epoch=500,
validation_split=0.1,
verbose=1)
trainScore = model.evaluate(X_train, y_train, verbose=0)
print('Train Score: %.2f MSE (%.2f RMSE)' % (trainScore[0], math.sqrt(trainScore[0])))
testScore = model.evaluate(X_test, y_test, verbose=0)
print('Test Score: %.2f MSE (%.2f RMSE)' % (testScore[0],math.sqrt(testScore[0])))
# print(X_test[-1])
diff=[]
ratio=[]
p = model.predict(X_test)
for u in range(len(y_test)):
pr = p[u][0]
ratio.append((y_test[u]/pr)-1)
diff.append(abs(y_test[u]- pr))
#print(u, y_test[u], pr, (y_test[u]/pr)-1, abs(y_test[u]- pr))
import matplotlib.pyplot as plt2
plt2.plot(p,color='red', label='prediction')
plt2.plot(y_test,color='blue', label='y_test')
plt2.legend(loc='upper left')
plt2.show()
我得到的输出:https://i.stack.imgur.com/6TVRb.png
(我已经尝试更改批量大小和 epoch 数)
我目前正在使用(在 macosx sierra 上):
Python 3.6.0(默认,2017 年 1 月 2 日,18:14:29) [GCC 4.2.1 兼容 Apple LLVM 8.0.0 (clang-800.0.42.1)] 在 darwin 键入“copyright”、“credits”或“license()”以获取更多信息。 警告:使用的 Tcl/Tk (8.5.9) 版本可能不稳定。
之前代码中使用的每个模块都是最新的(2017 年 4 月 24 日)
我可能忘记了一些信息,请随时问我。
谢谢
【问题讨论】:
是什么让你相信它应该输出你链接的原始图像? 因为它做了一次,至少我希望脚本输出不同于简单直线的东西(例如遵循平均值的东西,或类似的东西)。让我觉得有一个问题是,现在,无论我输入什么数据集(苹果股票、谷歌……),输出始终是一条直的红线。我是神经网络的新手,所以说实话,我真的不知道预期的输出应该是什么。如果这是正常行为,我怎样才能找到触发正确行为的原因。 您是否尝试将数据缩放到 mean=0? 【参考方案1】:将nb_epoch=500
更改为nb_epoch=2
你会看到 LSTM 可以正常工作。
【讨论】:
以上是关于lstm神经网络异常输出相同的值的主要内容,如果未能解决你的问题,请参考以下文章