Loading

循环神经网络(RNN)的代码实现

代码部分

import tensorflow as tf
import tensorflow.contrib as rnn #引入RNN
form tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("data/", one_hot=True)
batch_size = 128 #定义参数
#定义训练数据
x = tf.placeholder("float", [None, 28, 28])
y = tf.placeholder("float", [None, 10])
#定义w和b
weights = {
    'out': tf.Variable(tf.random_normal([128, 10]))}
biases = {
    'out': tf.Variable(tf.random_normal([10]))
}
def RNN(x, weights, biases):
    #按照RNN的方式处理输入层
    x = tf.unstack(x, 28, 1)
    #lstm层
    #forget_bias (默认为1)到遗忘门的偏置,为了减少在开始训练时遗忘的规模
    lstm_cell = rnn.BasicLSTMCell(128, forget_bias=1.0)
    #获得lstm层的输出
    outputs, states = rnn.static_rnn(lstm_cell, x, dtype=tf.float32)
    #得到最后一层的输出
    return rf.matmul(outputs[-1], weights['out'])+biases['out']
    
#预测值
pred = RNN(x, weights,biases)
#定义代价函数和最优算法
#寻找全局最优点的优化算法,引入了二次方梯度矫正
#AdamOptimizer 不容易陷于局部优点,速度更快
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pre, labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=0.0001).mnimizer(cost)
#结果对比
correct_pred = tf.wqual(tf.argmax(pred, 1),tf.argmax(y, 1))
#求正确率
accuracy = tf.reduce_mean(tf.case(corrext_pred, tf.float32))
#初始化所有参数
init = tf.initializer_all_variables()
with tf.Session() as sess:
    sess.run(init)
    step = 1
    
    while step * batch_size < 100000:
        batch_x, batch_y = mnist.train.next_batch(batch_size)
        batch_x = batch_x.reshape((batch_size,28,28))
        sess.run(optimizer, feed_dict={x: batch_x,y:batch_y})
        if step % 10 == 0:
            acc = sess.run(accuracy, feed_sict={x: batch_x,y:batch_y})
            loss = sess.run(cost, feed_dict={x: batch_x, y:batch_y})
            print "iter" + str(step * batch_size) + ",minibatch loss ="+ loss + acc
        step += 1
    print "optimization finished"
    #数据验证
    test_len = 128
    test_data = mnist.test.images[:test_len].reshape((-1,28,28))
    test_label = mnist.test.labels[:test_len]
    print "testing accuracy"+sess.run(accuracy, feed_dict={x: test_data,y: test_label})
posted @ 2018-07-10 17:18  摇橙子  阅读(5798)  评论(1编辑  收藏  举报