qq_44669715
2019-05-13 19:40
采纳率: 0%
浏览 3.9k

为什么LSTM预测股票,预测结果为一条直线?

import numpy as np
import tensorflow as tf
import matplotlib as mpl
import pandas as pd
from matplotlib import pyplot as plt 

HIDDEN_SIZE =128
NUM_LAYERS=2

TIMESTEPS=10
TRAINING_STEPS=10000
BATCH_SIZE=60

def generate_data(seq):
    x=[]
    y=[]

    for i in range(len(seq)-TIMESTEPS):
        x.append([seq[i:i+TIMESTEPS]])
        y.append([seq[i+TIMESTEPS]])
    return np.array(x,dtype=np.float32),np.array(y,dtype=np.float32)

def lstm_model(x,y,is_training):
    lstm_cell=tf.nn.rnn_cell.BasicLSTMCell
    cell=tf.nn.rnn_cell.MultiRNNCell([lstm_cell(HIDDEN_SIZE) for _ in range(NUM_LAYERS)])
    '''
    cell=tf.nn.rnn_cell.MultiRNNCell(
        [tf.nn.rnn_cell.DropoutWrapper(lstm_cell(HIDDEN_SIZE)) 
        for _ in range(NUM_LAYERS)])
    '''

    outputs,_=tf.nn.dynamic_rnn(cell,x,dtype=tf.float32)

    output=outputs[:,-1,:]

    predictions=tf.contrib.layers.fully_connected(output,1,activation_fn=tf.nn.relu)

    if not is_training:
        return predictions,None,None

    loss=tf.losses.mean_squared_error(labels=y,predictions=predictions)

    train_op=tf.contrib.layers.optimize_loss(loss,tf.train.get_global_step(),optimizer="Adam",learning_rate=0.01)

    return predictions,loss,train_op

def train(sess,train_x,train_y):
    ds=tf.data.Dataset.from_tensor_slices((train_x,train_y))
    ds=ds.repeat().shuffle(1000).batch(BATCH_SIZE)
    x,y=ds.make_one_shot_iterator().get_next()

    with tf.variable_scope("model"):
        predictions,loss,train_op=lstm_model(x,y,True)

    sess.run(tf.global_variables_initializer())
    for i in range(TRAINING_STEPS):
        _,l=sess.run([train_op,loss])
        if i%100==0:
            print("train step:"+str(i)+", loss:"+str(l))

def run_eval(sess,test_X,test_y):
    ds=tf.data.Dataset.from_tensor_slices((test_X,test_y))
    ds=ds.batch(1)
    x,y=ds.make_one_shot_iterator().get_next()

    with tf.variable_scope("model",reuse=True):
        prediction,_,_=lstm_model(x,[0.0],False)

    predictions=[]
    labels=[]
    for i in range(200):
        p,l=sess.run([prediction,y])
        predictions.append(p)
        labels.append(l)

    predictions=np.array(predictions).squeeze()
    labels=np.array(labels).squeeze()
    rmse=np.sqrt(((predictions-labels)**2).mean(axis=0))
    print("Mean Square Error is %f" % rmse)

    plt.figure()
    plt.plot(predictions,label='predictions')
    plt.plot(labels,label='real')
    plt.legend()
    plt.show()


f=open('IBM.csv')
df=pd.read_csv(f)
seq=df.iloc[:,1].values
train_data=seq[2:504]
test_data=seq[505:750]
train_x,train_y=generate_data(train_data)
test_x,test_y=generate_data(test_data)

with tf.Session() as sess:
    train(sess,train_x,train_y)

    run_eval(sess,test_x,test_y)


图片说明

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • ha_____ha 2019-09-07 10:47

    请问,你模型训练结果是什么样的?我也是用LSTM预测,训练集预测效果很好,但测试集预测结果就是一条直线

    打赏 评论
  • lsjtqkm94 2020-03-11 17:31

    学习率的问题,学习率取得太大了,尝试调小量级,如0.0001

    打赏 评论
  • threenewbee 2019-05-13 20:53

    这个模型是你自己写的?整个都不对,建议你从github上下载一个例子先入门下

    1 打赏 评论

相关推荐 更多相似问题