此代码主体与前面mnist一样,用rnn长短期记忆进行多分类
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.contrib.layers import fully_connected as fc
# 读取数据
mnist = input_data.read_data_sets('../../datas/MNIST_data',one_hot=True)
# 站位
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.int32,[None,10])
x_img = tf.reshape(x,[-1,28,28])
# 测试集
test_x = mnist.test.images
test_y = mnist.test.labels
#
cell = tf.nn.rnn_cell.MultiRNNCell([tf.nn.rnn_cell.BasicLSTMCell(10) for i in range(3)])
# 动态rnn
outputs , state = tf.nn.dynamic_rnn(cell,x_img,dtype = tf.float32)
# 全连接
in_top_key = state[-1][1]
logits = fc(in_top_key,10,activation_fn=None)
# 代价
cost = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=tf.argmax(y,1),logits=logits)
# 优化器
optimizer = tf.train.AdamOptimizer(0.01).minimize(cost)
# 准确率
accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(logits,1),tf.argmax(y,1)),tf.float32))
# 开启会话
sess = tf.Session()
sess.run(tf.global_variables_initializer())
batch_size = 150
for epoch in range(3):
batch = int(mnist.train.num_examples / batch_size)
for k in range(batch):
batch_x,batch_y = mnist.train.next_batch(batch_size)
sess.run([cost,optimizer],feed_dict={x:batch_x,y:batch_y})
acc_train = sess.run(accuracy,feed_dict={x:batch_x,y:batch_y})
acc_test = sess.run(accuracy,feed_dict={x:test_x,y:test_y})
print("Epoch", epoch, "Train accuracy =", acc_train, "Test accuracy =", acc_test)
Epoch 0 Train accuracy = 0.8933333 Test accuracy = 0.905
Epoch 1 Train accuracy = 0.96 Test accuracy = 0.9494
Epoch 2 Train accuracy = 0.96666664 Test accuracy = 0.955
Epoch 3 Train accuracy = 0.98 Test accuracy = 0.9631
Epoch 4 Train accuracy = 0.98 Test accuracy = 0.9646