实现一个简单的RNN(代码如下)
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.contrib import rnn
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
batch_size = 50
num_batch = mnist.train.num_examples // batch_size
# 输入(图片尺寸28*28)
num_input = 28 # 一行28个数据
max_time = 28 # 行数量
LSTM_size = 100 # 隐藏单元数量(block数量)
num_classes = 10 # 10个分类(对应输出)
x = tf.placeholder(tf.float32, [None, 784])
y = tf.placeholder(tf.float32, [None, 10])
weights = tf.Variable(tf.truncated_normal([LSTM_size, num_classes], stddev=0.1))
biases = tf.Variable(tf.constant(0.1, shape=[num_classes]))
def RNN(X):
# inputs格式固定:[batch_size, max_time, num_input]
inputs = tf.reshape(X, [-1, max_time, num_input])
# 定义LSTM基本的cell
LSTM_cell = rnn.BasicLSTMCell(LSTM_size)
outputs, final_state = tf.nn.dynamic_rnn(LSTM_cell, inputs, dtype=tf.float32)
results = tf.nn.softmax(tf.matmul(final_state[1], weights) + biases)
return results
prediction = RNN(x)
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=prediction, labels=y))
train = tf.train.AdamOptimizer(0.0001).minimize(loss)
correct_prediction = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(6):
for batch in range(num_batch):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
sess.run(train, 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))