莫烦 tensorflow LSTM regression code 完整代码 tensorboard可视化

学习莫烦 tesorflow视频,然后敲代码,改了原来有的错误,现在是可以运行的版本了。加了很多注释。

# 参考https://morvanzhou.github.io/tutorials/machine-learning/tensorflow/5-09-RNN3/

修改的错误是:版本问题tf.train.SummaryWriter改为tf.summary.FilterWritwer;

tf.merge_all_summaries()改为tf.summary.merge_all();

错误:crossent = softmax_loss_function(labels=target, logits=logit)

TypeError: ms_error() got an unexpected keyword argument 'labels'

解决:def ms_error(self, labels, logits):

return tf.square(tf.subtract(labels,logits))

完整代码如下

# -*- coding: utf-8 -*-

"""
Created on Fri May 25 17:19:53 2018


regression  RNN LSTM
tensorboard
plt.plot
RNN
LSTM
"""
#执行过程 运行本文件,再cmd -> activate  tensorflow -> tensorboard --logdir=E://tensorflow-example//logs
# google -> http://AOC:6006


#分类使用[(batch_size, output_size)*steps] 中最后一个step的值;
#分类使用或者描述为(batch_size, n_step, output_size)中(batch_size, -1, output_size)


#回归问题中,尽管可能输入和输出维度是1,
#但是可以time_steps=20,即把20个点当成个序列,这时候就要考虑每一步的output,合起来就是20个输出,即一个序列。
#import packages
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt


tf.reset_default_graph()  ####制胜法宝


#define hypeparameter
BATCH_START = 0
TIME_STEPS = 20
BATCH_SIZE = 50
INPUT_SIZE = 1
OUTPUT_SIZE = 1
CELL_SIZE = 10
LR = 0.006
BATCH_START_TEST = 0


#fake data
def get_batch():
    global BATCH_START, TIME_STEPS
    # xs shape=(50batch, 20steps)
    xs = np.arange(BATCH_START, BATCH_START+TIME_STEPS*BATCH_SIZE).reshape((BATCH_SIZE,TIME_STEPS))/(np.pi)
    # 用seq去拟合res,使得seq和res曲线最小均方误差
    seq = np.sin(xs)
    res = np.cos(xs)
    BATCH_START += TIME_STEPS #可能为了滚动效果,一次接着一个TIME_STEPS
    # 画图时,每次取第一个batch,即长度为TIME_STEPS的序列。
    plt.plot(xs[0, :], res[0, :], 'r', xs[0, :], seq[0, :],'b--')
    plt.show()
    # res and seq shape is (batch, step, input) input = 1
    return [seq[:, :, np.newaxis], res[:, :, np.newaxis], xs] #改变维度,准备做LSTMRNN的输入
    


# class LSTMRNN
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"): # xs->seq, ys ->res
            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(LR).minimize(self.cost)


    def add_input_layer(self):
        # (batch*n_step, in_size)
        l_in_x = tf.reshape(self.xs, [-1, self.input_size], name='2_2D')
        Ws_in = self._weight_variabe([self.input_size, self.cell_size])
        bs_in = self._biases_variabe([self.cell_size,])
        with tf.name_scope('Wx_plus_b'):
            l_in_y = tf.matmul(l_in_x, Ws_in)+bs_in
        
        self.l_in_y = tf.reshape(l_in_y, [-1, self.n_steps, self.cell_size], name='2_3D')
        
    
    def add_cell(self):
        lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(self.cell_size, forget_bias=1.0, state_is_tuple=True)
        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_variabe([self.cell_size, self.output_size])
        bs_out = self._biases_variabe([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): #参数可能是因为 tf.contrib.legacy_seq2seq.sequence_loss_by_example参数的
        return tf.square(tf.subtract(labels,logits))


    
            
            
    def _weight_variabe(sef, shape, name='weights'):
        initializer = tf.random_normal_initializer(mean=0., stddev=1.,)
        return tf.get_variable(shape=shape, initializer=initializer, name=name)
            
            
    def _biases_variabe(sef, shape, name='biases'):
        initializer = tf.constant_initializer(0.1)
        return tf.get_variable(name=name, initializer=initializer, shape=shape)
    
if __name__ == '__main__':
    model = LSTMRNN(TIME_STEPS, INPUT_SIZE, OUTPUT_SIZE, CELL_SIZE, BATCH_SIZE)
    sess = tf.Session()
    # tf.merge_all_summaries()
    merged = tf.summary.merge_all()
    writer = tf.summary.FileWriter("logs", sess.graph)
    sess.run(tf.initialize_all_variables())
    plt.ion()
    plt.show()
    for i in range(200):
        seq, res, xs = get_batch()
        if i == 0:
            feed_dict={model.xs:seq, model.ys:res}
        else:
            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)
        if(i % 20 == 0):
            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)
            print('cost:', round(cost, 4))
            result = sess.run(merged, feed_dict)
            writer.add_summary(result, i)
        

你可能感兴趣的:(tensorflow)