tensorflow学习--使用多层LSTM预测三角函数

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

hidden_size = 30   # LSTM 中隐藏节点的个数   //TODO 这个size是是什么意思  //是lstm模块输出单元的维度,
num_layers = 2     # LSTM 的层数
timestep = 10      # 循环神经网络的训练序列长度
train_step = 10000  # 训练的轮数
batch_size = 32     # batch的大小
training_examples = 10000   # 训练数据的个数
test_examples = 1000        # 测试数据的个数
sample_gap = 0.01           # 采样间隔
# 使用序列的第i项以及后面的timestep-1 作为输入来预测第i+timestep项
def generate_data(seq):
    X = []
    Y = []
    for i in range(len(seq) - timestep):
        X.append([seq[i:i+timestep]])
        Y.append([seq[i + timestep]])
    return np.array(X,dtype=np.float32),np.array(Y,dtype=np.float32)
# 在这里有一些问题,这些LSTM的结构是什么样的
def lstm_model(X,y,is_train):
    # 使用多层的LSTM结构
    cell = tf.nn.rnn_cell.MultiRNNCell([tf.nn.rnn_cell.BasicLSTMCell(hidden_size) for _ in range(num_layers)])
    # 使用Tensorflow 接口将多层的LSTM结构连接成RNN网络并计算其前向传播结果
    outputs,_ = tf.nn.dynamic_rnn(cell,X,dtype=tf.float32)
    # output 是顶层LSTM在每一步的输出结果,它的维度是[batch_size,timestep,hidden_size],在本问题中只关心最后一个输出
    outputs = outputs[:,-1,:]
    # 对lstm层的输出再加上上一个全连接层。
    predictions = tf.contrib.layers.fully_connected(outputs,1,activation_fn=None)
    if not is_train:
        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="Adagrad",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(10000).batch(batch_size)
    X,y = ds.make_one_shot_iterator().get_next()
    # 调用模型,得到预测结果,损失函数以及训练操作
    with tf.variable_scope("model",reuse=tf.AUTO_REUSE):
        predicttion,loss,train_op = lstm_model(X,y,True)
    # 初始化变量
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver()
    for i in range(train_step):
        _,l = sess.run([train_op,loss])
        if i % 100 == 0:
            print("train step: " + str(i) + ",loss: "+ str(l))
    #保存模型,下次可以直接加载模型
    saver.save(sess,"model_basic_RNN/model.ckpt")
    
            
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(test_examples):
        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("RMSE",rmse)
    
    plt.figure()
    plt.plot(predictions,label='predictions')
    plt.plot(labels,label='real_sign')
    plt.legend()
    plt.show()
    print("ok")
    return predictions,labels
    
test_start = (training_examples + timestep) * sample_gap
test_end = test_start + (test_examples + timestep) * sample_gap
train_X,train_Y = generate_data(np.sin(np.linspace(0,test_start,training_examples+timestep,dtype=np.float32)))
test_X,test_Y = generate_data(np.sin(np.linspace(test_start,test_end,test_examples+timestep,dtype=np.float32)))


with tf.Session() as sess:
    train(sess,train_X,train_Y)
    p,l = run_eval(sess,test_X,test_Y)
    

tensorflow学习--使用多层LSTM预测三角函数_第1张图片

你可能感兴趣的:(python,人工智能,tensorflow,学习,tensorflow,多层LSTM,序列分析)