根據(jù)股票歷史數(shù)據(jù)中的最低價(jià),、最高價(jià)、開(kāi)盤(pán)價(jià),、收盤(pán)價(jià),、交易量,、交易額,、跌漲幅等因素,,對(duì)下一日股票最高價(jià)進(jìn)行預(yù)測(cè),。
實(shí)驗(yàn)用到的數(shù)據(jù)長(zhǎng)這個(gè)樣子:
label是標(biāo)簽y,,也就是下一日的最高價(jià)。列C——I為輸入特征,。
本實(shí)例用前5800個(gè)數(shù)據(jù)做訓(xùn)練數(shù)據(jù),。
單因素輸入特征及RNN、LSTM的介紹請(qǐng)戳上一篇 Tensorflow實(shí)例:利用LSTM預(yù)測(cè)股票每日最高價(jià)(一)
導(dǎo)入包及聲明常量
import pandas as pd
import numpy as np
import tensorflow as tf
#定義常量
rnn_unit=10 #hidden layer units
input_size=7
output_size=1
lr=0.0006 #學(xué)習(xí)率
導(dǎo)入數(shù)據(jù)
f=open('dataset.csv')
df=pd.read_csv(f) #讀入股票數(shù)據(jù)
data=df.iloc[:,2:10].values #取第3-10列
生成訓(xùn)練集,、測(cè)試集
考慮到真實(shí)的訓(xùn)練環(huán)境,,這里把每批次訓(xùn)練樣本數(shù)(batch_size)、時(shí)間步(time_step),、訓(xùn)練集的數(shù)量(train_begin,train_end)設(shè)定為參數(shù),,使得訓(xùn)練更加機(jī)動(dòng)。
#——————————獲取訓(xùn)練集——————————
def get_train_data(batch_size=60,time_step=20,train_begin=0,train_end=5800):
batch_index=[]
data_train=data[train_begin:train_end]
normalized_train_data=(data_train-np.mean(data_train,axis=0))/np.std(data_train,axis=0) #標(biāo)準(zhǔn)化
train_x,train_y=[],[] #訓(xùn)練集x和y初定義
for i in range(len(normalized_train_data)-time_step):
if i % batch_size==0:
batch_index.append(i)
x=normalized_train_data[i:i+time_step,:7]
y=normalized_train_data[i:i+time_step,7,np.newaxis]
train_x.append(x.tolist())
train_y.append(y.tolist())
batch_index.append((len(normalized_train_data)-time_step))
return batch_index,train_x,train_y
#——————————獲取測(cè)試集——————————
def get_test_data(time_step=20,test_begin=5800):
data_test=data[test_begin:]
mean=np.mean(data_test,axis=0)
std=np.std(data_test,axis=0)
normalized_test_data=(data_test-mean)/std #標(biāo)準(zhǔn)化
size=(len(normalized_test_data)+time_step-1)//time_step #有size個(gè)sample
test_x,test_y=[],[]
for i in range(size-1):
x=normalized_test_data[i*time_step:(i+1)*time_step,:7]
y=normalized_test_data[i*time_step:(i+1)*time_step,7]
test_x.append(x.tolist())
test_y.extend(y)
test_x.append((normalized_test_data[(i+1)*time_step:,:7]).tolist())
test_y.extend((normalized_test_data[(i+1)*time_step:,7]).tolist())
return mean,std,test_x,test_y
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
構(gòu)建神經(jīng)網(wǎng)絡(luò)
#——————————————————定義神經(jīng)網(wǎng)絡(luò)變量——————————————————
def lstm(X):
batch_size=tf.shape(X)[0]
time_step=tf.shape(X)[1]
w_in=weights['in']
b_in=biases['in']
input=tf.reshape(X,[-1,input_size]) #需要將tensor轉(zhuǎn)成2維進(jìn)行計(jì)算,,計(jì)算后的結(jié)果作為隱藏層的輸入
input_rnn=tf.matmul(input,w_in)+b_in
input_rnn=tf.reshape(input_rnn,[-1,time_step,rnn_unit]) #將tensor轉(zhuǎn)成3維,,作為lstm cell的輸入
cell=tf.nn.rnn_cell.BasicLSTMCell(rnn_unit)
init_state=cell.zero_state(batch_size,dtype=tf.float32)
output_rnn,final_states=tf.nn.dynamic_rnn(cell, input_rnn,initial_state=init_state, dtype=tf.float32) #output_rnn是記錄lstm每個(gè)輸出節(jié)點(diǎn)的結(jié)果,final_states是最后一個(gè)cell的結(jié)果
output=tf.reshape(output_rnn,[-1,rnn_unit]) #作為輸出層的輸入
w_out=weights['out']
b_out=biases['out']
pred=tf.matmul(output,w_out)+b_out
return pred,final_states
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
訓(xùn)練模型
#——————————————————訓(xùn)練模型——————————————————
def train_lstm(batch_size=80,time_step=15,train_begin=0,train_end=5800):
X=tf.placeholder(tf.float32, shape=[None,time_step,input_size])
Y=tf.placeholder(tf.float32, shape=[None,time_step,output_size])
batch_index,train_x,train_y=get_train_data(batch_size,time_step,train_begin,train_end)
pred,_=lstm(X)
#損失函數(shù)
loss=tf.reduce_mean(tf.square(tf.reshape(pred,[-1])-tf.reshape(Y, [-1])))
train_op=tf.train.AdamOptimizer(lr).minimize(loss)
saver=tf.train.Saver(tf.global_variables(),max_to_keep=15)
module_file = tf.train.latest_checkpoint()
with tf.Session() as sess:
#sess.run(tf.global_variables_initializer())
saver.restore(sess, module_file)
#重復(fù)訓(xùn)練2000次
for i in range(2000):
for step in range(len(batch_index)-1):
_,loss_=sess.run([train_op,loss],feed_dict={X:train_x[batch_index[step]:batch_index[step+1]],Y:train_y[batch_index[step]:batch_index[step+1]]})
print(i,loss_)
if i % 200==0:
print("保存模型:",saver.save(sess,'stock2.model',global_step=i))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
嗯,,這里說(shuō)明一下,,這里的參數(shù)是基于已有模型恢復(fù)的參數(shù),,意思就是說(shuō)之前訓(xùn)練過(guò)模型,保存過(guò)神經(jīng)網(wǎng)絡(luò)的參數(shù),,現(xiàn)在再取出來(lái)作為初始化參數(shù)接著訓(xùn)練,。如果是第一次訓(xùn)練,就用sess.run(tf.global_variables_initializer()),,也就不要用到 module_file = tf.train.latest_checkpoint() 和saver.store(sess, module_file)了,。
測(cè)試
#————————————————預(yù)測(cè)模型————————————————————
def prediction(time_step=20):
X=tf.placeholder(tf.float32, shape=[None,time_step,input_size])
mean,std,test_x,test_y=get_test_data(time_step)
pred,_=lstm(X)
saver=tf.train.Saver(tf.global_variables())
with tf.Session() as sess:
#參數(shù)恢復(fù)
module_file = tf.train.latest_checkpoint()
saver.restore(sess, module_file)
test_predict=[]
for step in range(len(test_x)-1):
prob=sess.run(pred,feed_dict={X:[test_x[step]]})
predict=prob.reshape((-1))
test_predict.extend(predict)
test_y=np.array(test_y)*std[7]+mean[7]
test_predict=np.array(test_predict)*std[7]+mean[7]
acc=np.average(np.abs(test_predict-test_y[:len(test_predict)])/test_y[:len(test_predict)]) #acc為測(cè)試集偏差
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
最后的結(jié)果畫(huà)出來(lái)是這個(gè)樣子:
紅色折線是真實(shí)值,藍(lán)色折線是預(yù)測(cè)值
偏差大概在1.36%
代碼和數(shù)據(jù)上傳到了github上,,想要的戳全部代碼
注?。喝缫D(zhuǎn)載,請(qǐng)經(jīng)過(guò)本人允許并注明出處,!
|