import tensorflow as tf
import input_data
import numpy as np
import matplotlib.pyplot as plt
mnist = input_data.read_data_sets('./data/MNIST/', one_hot=True)
trainimgs, trainlabels, testimgs, testlabels \
= mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels
ntrain, ntest, dim, nclasses \
= trainimgs.shape[0], testimgs.shape[0], trainimgs.shape[1], trainlabels.shape[1]
diminput = 28
dimhidden = 128
dimoutput = nclasses
nsteps = 28
weights = {
'hidden': tf.Variable(tf.random_normal([diminput, dimhidden])),
'out': tf.Variable(tf.random_normal([dimhidden, dimoutput]))
}
biases = {
'hidden': tf.Variable(tf.random_normal([dimhidden])),
'out': tf.Variable(tf.random_normal([dimoutput]))
}
from tensorflow.contrib import rnn
def _RNN(_X, _W, _b, _nsteps, _name):
_X = tf.transpose(_X, [1, 0, 2])
_X = tf.reshape(_X, [-1, diminput])
_H = tf.matmul(_X, _W['hidden']) + _b['hidden']
_Hsplit = tf.split(_H, _nsteps, 0)
lstm_cell = rnn.BasicLSTMCell(dimhidden,reuse=tf.get_variable_scope().reuse)
_LSTM_O, _LSTM_S = rnn.static_rnn(lstm_cell, _Hsplit,dtype=tf.float32)
_O = tf.matmul(_LSTM_O[-1], _W['out']) + _b['out']
return {
'X': _X, 'H': _H, 'Hsplit': _Hsplit,
'LSTM_O': _LSTM_O, 'LSTM_S': _LSTM_S, 'O': _O
}
learning_rate = 0.001
x = tf.placeholder("float", [None, nsteps, diminput])
y = tf.placeholder("float", [None, dimoutput])
myrnn = _RNN(x, weights, biases, nsteps, 'basic')
pred = myrnn['O']
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=pred, labels=y))
optm = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
accr = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(pred,1), tf.argmax(y,1)), tf.float32))
init = tf.global_variables_initializer()
training_epochs = 20
batch_size = 100
display_step = 2
sess = tf.Session()
sess.run(init)
print ("Start optimization")
for epoch in range(training_epochs):
avg_cost = 0.
total_batch = int(mnist.train.num_examples/batch_size)
for i in range(total_batch):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
batch_xs = batch_xs.reshape((batch_size, nsteps, diminput))
feeds = {x: batch_xs, y: batch_ys}
sess.run(optm, feed_dict=feeds)
avg_cost += sess.run(cost, feed_dict=feeds)/total_batch
if epoch % display_step == 0:
print ("Epoch: %03d/%03d cost: %.9f" % (epoch, training_epochs, avg_cost))
feeds = {x: batch_xs, y: batch_ys}
train_acc = sess.run(accr, feed_dict=feeds)
print (" Training accuracy: %.3f" % (train_acc))
testimgs = testimgs.reshape((ntest, nsteps, diminput))
feeds = {x: testimgs, y: testlabels}
test_acc = sess.run(accr, feed_dict=feeds)
print (" Test accuracy: %.3f" % (test_acc))
print ("Optimization Finished.")
Start optimization
Epoch: 000/020 cost: 1.300222228
Training accuracy: 0.780
Test accuracy: 0.715
Epoch: 002/020 cost: 0.559806970
Training accuracy: 0.880
Test accuracy: 0.828
Epoch: 004/020 cost: 0.413306093
Training accuracy: 0.870
Test accuracy: 0.861
Epoch: 006/020 cost: 0.332932736
Training accuracy: 0.920
Test accuracy: 0.874
Epoch: 008/020 cost: 0.273848717
Training accuracy: 0.900
Test accuracy: 0.893
Epoch: 010/020 cost: 0.239274396
Training accuracy: 0.940
Test accuracy: 0.915
Epoch: 012/020 cost: 0.210719406
Training accuracy: 0.950
Test accuracy: 0.919
Epoch: 014/020 cost: 0.187901423
Training accuracy: 0.950
Test accuracy: 0.927
Epoch: 016/020 cost: 0.170967947
Training accuracy: 0.960
Test accuracy: 0.934
Epoch: 018/020 cost: 0.155789046
Training accuracy: 0.920
Test accuracy: 0.929
Optimization Finished.