Tensorflow-LSTMRNN例子

使用Rnn预测一段波形的序列

导入模块

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.contrib import rnn

设置参数

batch_start = 0 #建立batch_data 的索引
time_steps = 20 #反向传播通过时间的time_steps
batch_size = 50
training_steps = 200
input_size = 1 #sin 数据输入 size
output_size = 1 #cos 数据输出 size
cell_size = 10 #RNN的隐藏单位的大小
learning_rate = 0.001 # 学习率

生成数据

def get_batch():
    global batch_start,time_steps
    # sx ==>(50batch, 20steps) 时间点Data
    sx = np.arange(batch_start,batch_start+time_steps*batch_size)
           .reshape((batch_size,time_steps))/(10*np.pi)
    seq = np.sin(sx)
    res = np.cos(sx)
    batch_start += time_steps
    # return shape==> (batch, step, input)
    return [seq[:, :, np.newaxis], res[:, :, np.newaxis], sx]

定义LTSMRNN主体结构

class LSTMRNN(object):
    def __init__(self, n_steps, input_size, output_size, cell_size, batch_size):
        self.n_steps = n_steps
        self.input_size = input_size
        self.output_size = output_size
        self.cell_size = cell_size
        self.batch_size = batch_size
        with tf.name_scope('inputs'):
            self.xs = tf.placeholder(tf.float32, [None, n_steps, input_size], name='xs')
            self.ys = tf.placeholder(tf.float32, [None, n_steps, output_size], name='ys')
        with tf.variable_scope('in_hidden'):
            self.add_input_layer()
        with tf.variable_scope('LSTM_cell'):
            self.add_cell()
        with tf.variable_scope('out_hidden'):
            self.add_output_layer()
        with tf.name_scope('cost'):
            self.compute_cost()
        with tf.name_scope('train'):
            self.train_op = tf.train
                            .AdamOptimizer(learning_rate)
                            .minimize(self.cost)

添加输入层

def add_input_layer(self):
    l_in_x = tf.reshape(self.xs, [-1, self.input_size], name='2_2D')  # (batch*n_step, in_size)
    # Ws (in_size, cell_size)
    Ws_in = self._weight_variable([self.input_size, self.cell_size])
    # bs (cell_size, )
    bs_in = self._bias_variable([self.cell_size,])
    # l_in_y = (batch * n_steps, cell_size)
    with tf.name_scope('Wx_plus_b'):
        l_in_y = tf.matmul(l_in_x, Ws_in) + bs_in
    # reshape l_in_y ==> (batch, n_steps, cell_size)
    self.l_in_y = tf.reshape(l_in_y, [-1, self.n_steps, self.cell_size], name='2_3D')

添加RNN的cell

def add_cell(self):
    lstm_cell = rnn.BasicLSTMCell(self.cell_size)
    with tf.name_scope('initial_state'):
        self.cell_init_state = lstm_cell.zero_state(self.batch_size, dtype=tf.float32)
    self.cell_outputs, self.cell_final_state = tf.nn.dynamic_rnn(lstm_cell, self.l_in_y, initial_state=self.cell_init_state, time_major=False)

添加输出层

def add_output_layer(self):
    # shape ==> (batch * steps, cell_size)
    l_out_x = tf.reshape(self.cell_outputs, [-1, self.cell_size], name='2_2D')
    Ws_out = self._weight_variable([self.cell_size, self.output_size])
    bs_out = self._bias_variable([self.output_size, ])
    # shape = (batch * steps, output_size)
    with tf.name_scope('Wx_plus_b'):
        self.pred = tf.matmul(l_out_x, Ws_out) + bs_out

计算损失和评估模型

def compute_cost(self):
    losses = tf.contrib.legacy_seq2seq.sequence_loss_by_example(
                    [tf.reshape(self.pred, [-1], name='reshape_pred')],
                    [tf.reshape(self.ys, [-1], name='reshape_target')],
                    [tf.ones([self.batch_size * self.n_steps], dtype=tf.float32)],
                    average_across_timesteps=True,
                    softmax_loss_function=self.ms_error,
                    name='losses'
    )
    with tf.name_scope('average_cost'):
        self.cost = tf.div( tf.reduce_sum(losses, name='losses_sum'), self.batch_size,name='average_cost')
        tf.summary.scalar('cost', self.cost)

def ms_error(self, labels, logits):
    return tf.square(tf.subtract(labels, logits))

定义权重变量

def _weight_variable(self, shape, name='weights'):
    initializer = tf.random_normal_initializer(mean=0., stddev=1.,)
    return tf.get_variable(shape=shape, initializer=initializer, name=name)

定义偏置变量

def _bias_variable(self, shape, name='biases'):
    initializer = tf.constant_initializer(0.1)
    return tf.get_variable(name=name, shape=shape, initializer=initializer)

开始训练测试

if __name__ == '__main__':
    model = LSTMRNN(time_steps, input_size, output_size, cell_size, batch_size)
    with tf.Session() as sess:
        #tensorboard
        merged = tf.summary.merge_all()
        writer = tf.summary.FileWriter("logs", sess.graph)
        sess.run(tf.global_variables_initializer())
        plt.ion()
        plt.show()
        for i in range(training_steps):
            seq, res, xs = get_batch()  # 提取 batch data
            if i == 0:
                # create initial state
                feed_dict = {
                    model.xs: seq,
                    model.ys: res
                }

            else :
                # use last state as the initial state for this run
                feed_dict = {
                    model.xs: seq,
                    model.ys: res,
                    model.cell_init_state: state
                }

            _, cost, state, pred = sess.run( [model.train_op, model.cost, model.cell_final_state, model.pred],feed_dict=feed_dict)

            # 画线
            plt.plot(xs[0, :], res[0].flatten(), 'r', xs[0, :], pred.flatten()[:time_steps], 'b--')
            plt.ylim((-1.2, 1.2))
            plt.draw()
            plt.pause(0.3)

            if i % 20 == 0:
                print('cost: ', round(cost, 4))
                result = sess.run(merged, feed_dict)
                writer.add_summary(result, i)

运输结果:
输出的波形图结果
计算的偏差概率
上面两幅图都是数据上的展示 从波形图看,LSTMRNN的准确度越来越高,基本已经和sin曲线吻合了。计算的偏差也是越来越低,偏差小于0.1。
在tensorboard可以看到神经图,图的结构如下:
lstm结构图
图的左边就是这个例子的LSTMRNN的组成结构 有三层的layer组成,两个hidden layer (input/output)+一个cell。
展开这三个layer
这里写图片描述

hidden layer 包括一个权重和偏执和输入数据,在inputer layer的输入数据先转成二维的数据在输出成3维的数据,在output layer中将cell输出的数据再转成二维数据再经过Wx_plus_b输出结果。
在cell中包括一个rnn的cell和一个initial_state,initial_state在每一次batch循环时被final_state代替。
tf中的bppt如下:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值