TensorFlow RNN实现MNIST分类

在最后附上完整的代码和运行结果

一层隐藏层(128个outputs),一层输出层(10个outputs)

 

 

 

导入数据

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

 

设置权重和偏置

weight = {
    #128====>hidden cell num ;
    'first': tf.Variable(tf.random_normal([28, 128])),
    'last' : tf.Variable(tf.random_normal([128, 10]))
}

bias = {
    'first': tf.Variable(tf.constant(0.1, shape=[128,])),
    'last' : tf.Variable(tf.constant(0.1, shape=[10, ]))
}

网络中的X是一个batch的数据,记128,shape=(128(batch_size),28(step),28(pixel))。

def RNN(X, weights, biases):
    X = tf.reshape(X, shape=[-1, 28])
    X_in = tf.matmul(X, weight['first']) + bias['first']
    X_in = tf.reshape(X_in, shape=[-1, 28, 128])

    #lstm_cell = tf.contrib.rnn.BasicLSTMCell(num_units=128)#过时的写法
    lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(num_units=128)#num_units: 在LSTM cell中unit 的数目
# https://tensorflow.google.cn/api_docs/python/tf/nn/dynamic_rnn  具体的参数和输出参看这个网址
# tf.nn.dynamic_rnn(cell,inputs,sequence_length=None,initial_state=None,dtype=None,
#                   parallel_iterations=None,swap_memory=False,time_major=False,scope=None)
    init_state = lstm_cell.zero_state(128, dtype=tf.float32)#通过zero_state得到全0的初始状态
    outputs,_ = tf.nn.dynamic_rnn(lstm_cell, X_in, initial_state=init_state, time_major=False)
    results = tf.matmul(final_state[1], weight['last']) + bias['last']
    return results
x = tf.placeholder(tf.float32, [None, 28, 28])
y = tf.placeholder(tf.float32, [None, 10])
pred = RNN(x, weight, bias)#输入参数
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=y, logits=pred))#labels:真实数据的标签 logits:预测的输出值
train = tf.train.AdamOptimizer(0.01).minimize(loss)

correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))#cast 类型转换

init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    step = 0
    for step in range(1200):
        step += 1
        batch_x, batch_y = mnist.train.next_batch(128)
        batch_x = batch_x.reshape([128, 28, 28])
        sess.run(train, feed_dict={x: batch_x, y: batch_y})

        if step%50 == 0:
            print(step)
            print(sess.run(accuracy, feed_dict={x: batch_x, y: batch_y}))
#完整代码#
#tf.__version__: 1.9.0#
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

weight = {
    #128====>hidden cell num ;
    'first': tf.Variable(tf.random_normal([28, 128])),
    'last' : tf.Variable(tf.random_normal([128, 10]))
}

bias = {
    'first': tf.Variable(tf.constant(0.1, shape=[128,])),
    'last' : tf.Variable(tf.constant(0.1, shape=[10, ]))
}

def RNN(X, weights, biases):
    X = tf.reshape(X, shape=[-1, 28])
    X_in = tf.matmul(X, weight['first']) + bias['first']
    X_in = tf.reshape(X_in, shape=[-1, 28, 128])

    #lstm_cell = tf.contrib.rnn.BasicLSTMCell(num_units=128)
    lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(num_units=128)
    #lstm_cell = tf.contrib.rnn.BasicLSTMCell(128, forget_bias=1.0, state_is_tuple=True)
    init_state = lstm_cell.zero_state(128, dtype=tf.float32)

    outputs,_ = tf.nn.dynamic_rnn(lstm_cell, X_in, initial_state=init_state, time_major=False)
    #results = tf.matmul(final_state[1], weight['last']) + bias['last']
    results= tf.layers.dense(outputs[:, -1, :], 10)
    return results

x = tf.placeholder(tf.float32, [None, 28, 28])
y = tf.placeholder(tf.float32, [None, 10])

pred = RNN(x, weight, bias)
#loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=pred, logits=y))
loss = tf.losses.softmax_cross_entropy(onehot_labels=y, logits=pred)
train = tf.train.AdamOptimizer(0.01).minimize(loss)

correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    step = 0
    for step in range(1200):
        step += 1
        batch_x, batch_y = mnist.train.next_batch(128)
        batch_x = batch_x.reshape([128, 28, 28])
        sess.run(train, feed_dict={x: batch_x, y: batch_y})

        if step%50 == 0:
            print(sess.run(accuracy, feed_dict={x: batch_x, y: batch_y}))

 

运行结果如下:

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值