# 通過tensorflow使用RNN訓練MNIST資料集

```import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
sess = tf.InteractiveSession()
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
# input_layer
X_ = tf.placeholder(tf.float32, [None, 784])
y_ = tf.placeholder(tf.float32, [None, 10])
# FC1
W_fc1 = weight_variable([784, 1024])
b_fc1 = bias_variable([1024])
h_fc1 = tf.nn.relu(tf.matmul(X_, W_fc1)   b_fc1)
# FC2
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_pre = tf.nn.softmax(tf.matmul(h_fc1, W_fc2)   b_fc2)
cross_entropy = -tf.reduce_sum(y_ * tf.log(y_pre))
correct_prediction = tf.equal(tf.argmax(y_pre, 1), tf.arg_max(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
sess.run(tf.global_variables_initializer())
for i in range(5000):
X_batch, y_batch = mnist.train.next_batch(batch_size=100)
train_step.run(feed_dict={X_: X_batch, y_: y_batch})
if (i 1) % 200 == 0:
train_accuracy = accuracy.eval(feed_dict={X_: mnist.train.images, y_: mnist.train.labels})
print "step %d, training acc %g" % (i 1, train_accuracy)
if (i 1) % 1000 == 0:
train_accuracy = accuracy.eval(feed_dict={X_: mnist.train.images, y_: mnist.train.labels})
print "step %d, training acc %g" % (i   1, train_accuracy)
if (i   1) % 1000 == 0:
test_accuracy = accuracy.eval(feed_dict={X_: mnist.test.images, y_: mnist.test.labels})
print "= " * 10, "step %d, testing acc %g" % (i   1, test_accuracy)

step 200, training acc 0.9436
step 400, training acc 0.9648
step 600, training acc 0.9766
step 800, training acc 0.975927
step 1000, training acc 0.981745
step 1000, training acc 0.981745
= = = = = = = = = =  step 1000, testing acc 0.97
step 1200, training acc 0.986473
step 1400, training acc 0.9892
step 1600, training acc 0.988964
step 1800, training acc 0.992818
step 2000, training acc 0.989309
step 2000, training acc 0.989309
= = = = = = = = = =  step 2000, testing acc 0.9732
step 2200, training acc 0.993764
step 2400, training acc 0.993691
step 2600, training acc 0.995709
step 2800, training acc 0.996836
step 3000, training acc 0.997236
step 3000, training acc 0.997236
= = = = = = = = = =  step 3000, testing acc 0.9805
step 3200, training acc 0.996982
step 3400, training acc 0.997964
step 3600, training acc 0.998091
step 3800, training acc 0.9976
step 4000, training acc 0.998255
step 4000, training acc 0.998255
= = = = = = = = = =  step 4000, testing acc 0.9795
step 4200, training acc 0.996727
step 4400, training acc 0.996364
step 4600, training acc 0.999255
step 4800, training acc 0.997891
step 5000, training acc 0.997655
step 5000, training acc 0.997655
= = = = = = = = = =  step 5000, testing acc 0.9776
```