1. 程式人生 > >利用LSTM預測股票日最高價

利用LSTM預測股票日最高價

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf

f=open('G:\\Kaggle\\RNN\\LSTM\\dataset_1.csv')  
df=pd.read_csv(f)     
data=np.array(df['max'])

#資料按照日期從前往後排列
data=data[::-1]      
plt.figure()
plt.plot(data)
plt.show()

#標準化
normalize_data=(data-np.mean(data
)
)/np.std(data)
#增加維度 normalize_data=normalize_data[:,np.newaxis]

這裡寫圖片描述

#----------------------形成訓練集-------------------------#
#設定常量
time_step=20      #時間步
rnn_unit=10       #隱藏層神經單元
batch_size=60     #每一批次訓練多少個樣例
input_size=1      #輸入層維度
output_size=1     #輸出層維度
lr=0.0006         #學習率

#生成訓練集
train_x,train_y=[],[]   
for
i in range(len(normalize_data)-time_step-1): x=normalize_data[i:i+time_step] y=normalize_data[i+1:i+time_step+1] train_x.append(x.tolist()) train_y.append(y.tolist()) In [5]: #每批次輸入的tensor X=tf.placeholder(tf.float32, [None,time_step,input_size]) #每批次Tensor的對應的標籤 Y=tf.placeholder(tf.float32, [None
,time_step,output_size]) #輸入層、輸出層權重、偏置 weights={ 'in':tf.Variable(tf.random_normal([input_size,rnn_unit])), 'out':tf.Variable(tf.random_normal([rnn_unit,1])) } biases={ 'in':tf.Variable(tf.constant(0.1,shape=[rnn_unit,])), 'out':tf.Variable(tf.constant(0.1,shape=[1,])) }
def lstm(batch):     #引數:輸入網路批次數目    
    w_in=weights['in']
    b_in=biases['in']
    #需要將tensor轉成2維進行計算,計算後的結果作為隱藏層的輸入
    input=tf.reshape(X,[-1,input_size]) 
    input_rnn=tf.matmul(input,w_in)+b_in
    #將tensor轉成3維,作為lstm cell的輸入
    input_rnn=tf.reshape(input_rnn,[-1,time_step,rnn_unit]) 

    cell=tf.contrib.rnn.BasicLSTMCell(rnn_unit,reuse=tf.get_variable_scope().reuse)
    init_state=cell.zero_state(batch,dtype=tf.float32)
    #output_rnn是記錄lstm每個輸出節點的結果,final_states是最後一個cell的結果
    output_rnn,final_states=tf.nn.dynamic_rnn(cell, input_rnn,initial_state=init_state, dtype=tf.float32)
    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
def train_lstm():
    global batch_size
    with tf.variable_scope("sec_lstm"):
        pred,_=lstm(batch_size)
    #定義損失函式
    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())
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        #訓練1000次,可以增加次數
        for i in range(10):
            step=0
            start=0
            end=start+batch_size
            while(end<len(train_x)):
                _,loss_=sess.run([train_op,loss],feed_dict={X:train_x[start:end],Y:train_y[start:end]})
                start+=batch_size
                end=start+batch_size
                #每10步儲存一次引數
                if step%10==0:
                    print("Number of iterations:",i," loss:",loss_)
                    print("model_save",\
                          saver.save(sess,\
                                     'G:\\Kaggle\\RNN\\LSTM\\model_save1\\modle.ckpt'))
 #執行在windows 10,使用'model_save1\\modle.ckpt'
 #執行在Linux,使用 'model_save1/modle.ckpt'
                step+=1
        print("The train has finished")
train_lstm()

Number of iterations: 0 loss: 11.3699 model_save C:\Users\shaoqiu\Desktop\RNN\LSTM\model_save1\modle.ckpt

Number of iterations: 0 loss: 2.35093 model_save C:\Users\shaoqiu\Desktop\RNN\LSTM\model_save1\modle.ckpt

Number of iterations: 0 loss: 2.44522 model_save C:\Users\shaoqiu\Desktop\RNN\LSTM\model_save1\modle.ckpt

……

def prediction():
    with tf.variable_scope("sec_lstm",reuse=True):
        pred,_=lstm(1)    
    saver=tf.train.Saver(tf.global_variables())
    with tf.Session() as sess:
        saver.restore(sess, 'G:\\Kaggle\\RNN\\LSTM\\model_save1\\modle.ckpt')
        prev_seq=train_x[-1]
        predict=[]
        for i in range(100):
            next_seq=sess.run(pred,feed_dict={X:[prev_seq]})
            predict.append(next_seq[-1])
            prev_seq=np.vstack((prev_seq[1:],next_seq[-1]))

        plt.figure()
        plt.plot(list(range(len(normalize_data))), normalize_data, color='b')
        plt.plot(list(range(len(normalize_data), len(normalize_data) + len(predict))), predict, color='r')
        plt.show()
prediction() 

#INFO:tensorflow:Restoring parameters from G:\Kaggle\RNN\LSTM\model_save1\modle.ckpt

這裡寫圖片描述