用tensorflow LSTM如何预测股票价格?

用tensorflow LSTM如何预测股票价格?

写篇文章督促下自己,懒惰症拖延症得治。亲测tensorflow学习成本挺高,不过也更有意思呢?

Win10使用tensorboard到处踩坑。首先外部环境必须装tensorflow,我原先是只在自己创建环境里安装,试了多次浏览器打开生成的地址都是白屏。然后必须在cmd里运行tensorboard --logdir=wenjianjia, pycharm terminal 运行生成地址浏览器打开也是白屏,注意wenjianjia地址打绝对路径,且不能加引号。最后必须用chrome打开,其他浏览器显示都有问题。

这段话写得有点绕,很多博客文章解释得好得多,感觉LSTM原理自己推一遍就清楚得多,关键是顺着网络图走一遍当图纸来看,重点是下边这张图,我写的直接可以忽略了。LSTM有三个门,输入门It,遗忘门ft,输出门ot,各自初始化W,b,与上一时刻ht-1和新输入值xt线性相乘Wi[ht-1,xt]+bi,再放入激活函数sigmoid中,映射到0和1之间,个人理解吗sigmoid可以让It,ft,ot分别控制输入、保留及输出百分之多少信息。遗忘门与上一时刻memory cell ct-1相乘控制记住上一时刻cell中多少信息,输入门与有关上一时刻隐藏状态和新输入新息xt线性表达式的tanh非线性变化结果相乘控制有多少信息流入cell,上一时刻memory cell中部分信息和新流入的部分信息组成了新的memory cell。对新时刻memory cell作tanh非线性变化后与输出门相乘得到当前隐藏状态ht信息值。

我设计了一层神经网络层+一层LSTM层的结构。tensorflow一直在升级,很多现成代码过时了都报错,所以我更新改编了网上几位大牛的代码,最终跑通,也加上了注释,可以说最要注意的就是shape问题,我把类class写法也改掉了,便于查看,有问题请大家留言指出哦,感谢~

Step1~股票数据处理与准备

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import tushare as ts
import pandas as pd
from sklearn.preprocessing import MinMaxScaler

#参数设置/parameter setting
timesteps = seq_length = 7 #时间窗/window length
data_dim = 5 #输入数据维度/dimension of input data
output_dim = 1 #输出数据维度/dimension of output data

#数据准备/data preparation 
#变量选取Open,High,Low,Close,Volume,以浦发银行股票为例
stock_data = ts.get_k_data('600000',start='2015-01-01',end='2017-12-01')
xy = stock_data[['open','close','high','low','volume']]
#变量归一化
xy_new = pd.DataFrame()
scaler = MinMaxScaler()
for col in xy.columns:
    scaler.fit(xy[col])
    xy_new.ix[:,col] = scaler.transform(xy[col])

x = xy_new
y = xy_new[['close']]  # Close as label
dataX = []
dataY = []
for i in range(0, len(y) - seq_length):
   _x = x[i:i + seq_length]
   _y = y.loc[i + seq_length]  # Next close price
   print(_x, "->", _y)
   dataX.append(_x)
   dataY.append(_y)

#处理数据shape,准备进入神经网络层
x_real = np.vstack(dataX).reshape(-1,seq_length,data_dim)
y_real= np.vstack(dataY).reshape(-1,output_dim)
print(x_real.shape)
print(y_real.shape)
dataX = x_real
dataY = y_real

#切分训练集合测试集/split to train and testing
train_size = int(len(dataY) * 0.7)
test_size = len(dataY) - train_size
trainX, testX = np.array(dataX[0:train_size]),np.array(dataX[train_size:len(dataX)])
trainY, testY = np.array(dataY[0:train_size]),np.array(dataY[train_size:len(dataY)])

Step2~一层神经网络层+一层LSTM层的结构搭建

# input placeholders
X = tf.placeholder(tf.float32, [None, seq_length, data_dim])
Y = tf.placeholder(tf.float32, [None, 1])

# 添加神经网络层/add layer
def add_layer(inputs, in_size, out_size, activation_function=None):
    inputs = tf.reshape(inputs,[-1,in_size])
    Weights = tf.Variable(tf.random_normal([in_size, out_size]))
    biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
    Wx_plus_b = tf.matmul(inputs, Weights) + biases
    if activation_function is None:
        outputs = Wx_plus_b
    else:
        outputs = activation_function(Wx_plus_b)
    return outputs

outsize_first = 5
l1 = add_layer(X, data_dim, outsize_first, activation_function=tf.nn.relu)
l1_output = tf.reshape(l1,[-1,seq_length,outsize_first])

#添加LSTM层/add LSTM layer
#上层输出即LSTM输入,注意shape变换
cell = tf.nn.rnn_cell.BasicLSTMCell(num_units=output_dim, state_is_tuple=True)
outputs, _states = tf.nn.dynamic_rnn(cell, l1_output, dtype=tf.float32)
Y_pred = outputs[:, -1]  # We use the last cell's output

#计算损失/cost
loss = tf.reduce_sum(tf.square(Y_pred - Y))  # sum of the squares
#优化/optimizer
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)

#启动session
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for i in range(2000):
   _, l = sess.run([train, loss],
          feed_dict={X: trainX, Y: trainY})
   print(i, l)

Step3~预测结果可视化

testPredict = sess.run(Y_pred, feed_dict={X: testX})
plt.plot(testY)
plt.plot(testPredict)
plt.show()
Stockdata_Test_Visualization
trainPredict = sess.run(Y_pred, feed_dict={X: trainX})
plt.plot(trainY)
plt.plot(trainPredict)
plt.show()
Stockdata_Train_Visualization

还算有点work吧,这只是一个初步的demo,还想要加入更多有效因子技术指标等,参数还要调整,结构再搭复杂点会不会好点呢?有待尝试~

相比机器学习的算法,感觉神经网络更灵活,可变可改造性更强,只要愿意可以搭出任意结构,每层信息的传递真的好像各种基建结构里力的传递。

感谢各位大牛的无私分享,我也写篇攒RP,祝我好运~~啦啦啦!有问题请大家留言指出哦,感谢!

编辑于 2018-01-30