coding=utf-8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import gzip
import os
import sys
import time
import numpy as np
from six.moves import urllib
from six.moves import xrange
import tensorflow as tf
from tensorflow.python.ops import rnn, rnn_cell
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
# 载入数据
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
# 把每张图片看作一个序列,28个元素,每个元素28个点
# Parameters
learning_rate = 0.001
training_iters = 100000
batch_size = 128
display_step = 10
# 每行看成上一行的序列
n_input = 28 # MNIST data input (img shape: 28*28)
n_steps = 28 # timesteps
# n_hidden个lstm单元
n_hidden = 128
# 最终分类到0~9数字上
n_classes = 10 # MNIST total classes (0-9 digits)
# tf Graph input
x = tf.placeholder("float", [None, n_steps, n_input])
y = tf.placeholder("float", [None, n_classes])
# 从n_hidden个lstm到n_classes个最后一层神经元的权重
weights = {
'out': tf.Variable(tf.random_normal([n_hidden, n_classes]))
}
biases = {
'out': tf.Variable(tf.random_normal([n_classes]))
}
def RNN(x, weights, biases):
# 为rnn作数据转换
# 输入数据是: (batch_size, n_steps, n_input)
# 需要转换为: 'n_steps' 个矩阵 (batch_size, n_input)
# 把n_steps和batch_size交换下,矩阵变成[n_steps,batch_size,n_input]
x = tf.transpose(x, [1, 0, 2])
# 再变成 (n_steps*batch_size, n_input)
x = tf.reshape(x, [-1, n_input])
# 拆成n_steps个 (batch_size, n_input)
x = tf.split(0, n_steps, x)
# n_hidden个lstm单元
lstm_cell = rnn_cell.BasicLSTMCell(n_hidden, forget_bias=1.0)
# 计算
outputs, states = rnn.rnn(lstm_cell, x, dtype=tf.float32)
# 取最后一个output,rnn后再走一层网络
return tf.matmul(outputs[-1], weights['out']) + biases['out']
pred = RNN(x, weights, biases)
# loss函数,优化方法
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
# 准确率评估
correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
init = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init)
step = 1
while step * batch_size < training_iters:
batch_x, batch_y = mnist.train.next_batch(batch_size)
# Reshape data to get 28 seq of 28 elements
batch_x = batch_x.reshape((batch_size, n_steps, n_input))
# Run optimization op (backprop)
sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})
if step % display_step == 0:
# 计算准确率和loss
acc = sess.run(accuracy, feed_dict={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= " + \
"{:.6f}".format(loss) + ", Training Accuracy= " + \
"{:.5f}".format(acc))
step += 1
print ("Optimization Finished!")
# 用测试数据再评估下
test_len = 128
test_data = mnist.test.images[:test_len].reshape((-1, n_steps, n_input))
test_label = mnist.test.labels[:test_len]
print ("Testing Accuracy:", \
sess.run(accuracy, feed_dict={x: test_data, y: test_label}))