上一讲博客主要对RNN的理论做了介绍,如果想看详细的RNN推导过程以及一些变种,如:LSTM、GRU等,可以给博主留言。下面基于mnist数据集,采用RNN建模和仿真,发现RNN对于时序模型的强大建模能力,迭代20次就已经达到96准确率。下面直接上代码,有不懂的问题,可以留言。

#-*- coding:utf-8 -*-
#author : zhangwei

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import  input_data

mnist = input_data.read_data_sets('MNIST.data/' , one_hot=True)

n_inputs = 28
max_time = 28
lstm_size = 100
n_classes = 10
batch_size = 50
n_batch = mnist.train.num_examples // batch_size

with tf.Graph().as_default():
    with tf.name_scope('Input'):
        x = tf.placeholder(tf.float32 , [None , 784] , name='input_x')
        y = tf.placeholder(tf.float32 , [None , 10] , name='input_y')

        weights = tf.Variable(tf.truncated_normal([lstm_size , n_classes] , stddev=0.1) , name='weights')
        biases = tf.Variable(tf.constant(0.1 , shape=[n_classes]) , name='biases')

    def RNN(x , weights , biases):
        inputs = tf.reshape(x , [-1 , max_time , n_inputs])
        lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(lstm_size)
        outputs , final_states = tf.nn.dynamic_rnn(lstm_cell , inputs , dtype=tf.float32)
        results = tf.nn.softmax(tf.matmul(final_states[1] , weights) + biases)
        return results

    prediction = RNN(x , weights , biases)
    with tf.name_scope('Loss'):
        with tf.name_scope('cross_entropy'):
            cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y , logits = prediction) , name='cross_entropy')
        with tf.name_scope('train'):
            train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
    with tf.name_scope('Acc'):
        correct_prediction = tf.equal(tf.argmax(y , 1) , tf.argmax(prediction , 1) , name='correct_prediction')
        accuracy = tf.reduce_mean(tf.cast(correct_prediction , tf.float32) , name='accuracy')

    init = tf.global_variables_initializer()
    # saver = tf.train.Saver()
    writer = tf.summary.FileWriter(logdir='/home/zhangwei/tensorboard' , graph=tf.get_default_graph())
    writer.close()

    with tf.Session() as sess:
        sess.run(init)
        for epoch in range(200):
            for batch in range(n_batch):
                batch_xs , batch_ys = mnist.train.next_batch(batch_size)
                training , loss = sess.run([train_step , cross_entropy] , feed_dict={x : batch_xs , y : batch_ys})

            acc = sess.run(accuracy , feed_dict={x : mnist.test.images , y : mnist.test.labels})

            print "Iter" + str(epoch) + " , Testing Accuracy" + str(acc) + ', loss:'+str(loss)
        # saver.restore(sess , '/home/zhangwei/my_net_1/second')
        # print sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels})

  下面是识别结果以及在tensorboard上进行可视化结果:

循环神经网络学习研究(二)-代码

tensorboard可视化:

循环神经网络学习研究(二)-代码

继续搬砖!