为什么我的LSTM网络性能有时会更差?

时间:2018-03-02 03:15:11

标签: python tensorflow lstm

我目前正在使用LSTM来预测股票价格序列。我在网上找到了一些实现代码,并尝试自己做类似的事情。

在运行我的程序时,我在迭代过程中显示了列车的准确性,以检查性能,但问题出现了:有时候训练时期的列车精度有所提高,但有时保持不变。

这是我的代码:

import tensorflow as tf
from tensorflow.contrib import rnn
import numpy as np
import matplotlib.pyplot as plt
from pandas_datareader import data, wb
import pandas as pd
import datetime

sequence = 7 # time span
inputD = 1 # input dimension
outD = 1 # output dimension

# normalize data in each column
def Normalized(data): 
    numerator = data - np.min(data, 0)
    denominator = np.max(data, 0) - np.min(data, 0)
    # noise term prevents the zero division
    return numerator / denominator

# Convert sequence data to usable for training
def dataConvert(data):
    data = data[::-1]
    data = Normalized(data)
    x_data = []
    y_data = []
    for i in range(0, len(data) - sequence):
        _x = data[i:i + sequence]
        _y = data[i + sequence]

        x_data.append(_x)
        y_data.append(_y)
    x_data = np.array(x_data)
    y_data = np.array(y_data)
    return x_data, y_data


data = np.loadtxt('ibm2.csv')
a = []
for i in range(len(data)):
    a.append([data[i]])

data = np.array(a)
x_data, y_data = dataConvert(data)

train_size = int(len(y_data) * 0.7) # 70% data for training

x_train = np.array(x_data[:train_size])
x_test = np.array(x_data[train_size:])

y_train = np.array(y_data[:train_size])
y_test = np.array(y_data[train_size:])


X = tf.placeholder(tf.float32, [None, None, 1])
Y = tf.placeholder(tf.float32, [None, outD])

cell = rnn.BasicLSTMCell(num_units=10, state_is_tuple=True, activation=tf.tanh)

outputs, _state = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32)
y_pred = tf.contrib.layers.fully_connected(outputs[:,-1], outD)

loss = tf.reduce_sum(tf.square(y_pred - Y))

optimizer = tf.train.AdamOptimizer(learning_rate=0.01)
train = optimizer.minimize(loss)

targets = tf.placeholder(tf.float32, [None, 1])
predictions = tf.placeholder(tf.float32, [None, 1])
rmse = tf.sqrt(tf.reduce_mean(tf.square(targets - predictions)))

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for i in range(1001):
        _, step_loss = sess.run([train, loss], feed_dict={X: x_train, Y: y_train})
        if(i % 100 == 0):
          print(i, step_loss)

    result = sess.run(y_pred, feed_dict={X:x_test})
    rmse = sess.run(rmse, feed_dict={targets:y_test, predictions:result})


print("rmse:",rmse)

当我展示表演时,RMSE显示好像是未经训练的: enter image description here

我已经检查过lstm的其他实现,但我找不到问题所在。

0 个答案:

没有答案