import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
Extracting MNIST_data/train-images-idx3-ubyte.gz Extracting MNIST_data/train-labels-idx1-ubyte.gz Extracting MNIST_data/t10k-images-idx3-ubyte.gz Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
x = tf.placeholder("float", [None, 784])
y = tf.placeholder("float", [None, 10])
x_image = tf.reshape(x, [-1, 28, 28, 1])
print x_image.get_shape()
(?, 28, 28, 1)
W_conv1 = tf.Variable(tf.truncated_normal([5, 5, 1, 32], stddev=0.1))
b_conv1 = tf.Variable(tf.constant(0.1, shape=[32]))
h_conv1 = tf.nn.relu(tf.nn.conv2d(x_image, W_conv1, strides=[1, 1, 1, 1], padding='SAME') + b_conv1)
h_pool1 = tf.nn.max_pool(h_conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
W_conv2 = tf.Variable(tf.truncated_normal([5, 5, 32, 64], stddev=0.1))
b_conv2 = tf.Variable(tf.constant(0.1, shape=[64]))
h_conv2 = tf.nn.relu(tf.nn.conv2d(h_pool1, W_conv2, strides=[1, 1, 1, 1], padding='SAME') + b_conv2)
h_pool2 = tf.nn.max_pool(h_conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])
W_fc1 = tf.Variable(tf.truncated_normal([7 * 7 * 64, 1024], stddev=0.1))
b_fc1 = tf.Variable(tf.constant(0.1, shape=[1024]))
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
keep_prob = tf.placeholder("float")
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob=keep_prob)
W_fc2 = tf.Variable(tf.truncated_normal([1024, 10], stddev=0.1))
b_fc2 = tf.Variable(tf.constant(0.1, shape=[10]))
y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
cross_entropy = -tf.reduce_sum(y * tf.log(y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
# Initializing the variables
init = tf.initialize_all_variables()
# Parameters
training_epochs = 40
learning_rate = 0.001
batch_size = 100
display_step = 1
# Calculate accuracy with a Test model
prediction_and_ground_truth = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(prediction_and_ground_truth, "float"))
# Launch the tensorflow graph
with tf.Session() as sess:
sess.run(init)
total_batch = int(mnist.train.num_examples/batch_size)
print "total batch: %d" % total_batch
# Training cycle
for epoch in range(training_epochs):
# Loop over all batches
for i in range(total_batch):
batch_images, batch_labels = mnist.train.next_batch(batch_size)
sess.run(train_step, feed_dict={x: batch_images, y: batch_labels, keep_prob: 0.5})
print "Epoch %d Finished - Accuracy %g" % (epoch, sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels, keep_prob: 0.5}))
print("Optimization Finished!")
total batch: 550 Epoch 0 Finished - Accuracy 0.9337 Epoch 1 Finished - Accuracy 0.9567 Epoch 2 Finished - Accuracy 0.9673 Epoch 3 Finished - Accuracy 0.9714 Epoch 4 Finished - Accuracy 0.9735 Epoch 5 Finished - Accuracy 0.9767 Epoch 6 Finished - Accuracy 0.9802 Epoch 7 Finished - Accuracy 0.9801 Epoch 8 Finished - Accuracy 0.9832 Epoch 9 Finished - Accuracy 0.9821 Epoch 10 Finished - Accuracy 0.9854 Epoch 11 Finished - Accuracy 0.9867 Epoch 12 Finished - Accuracy 0.9868 Epoch 13 Finished - Accuracy 0.9851 Epoch 14 Finished - Accuracy 0.9892 Epoch 15 Finished - Accuracy 0.9886 Epoch 16 Finished - Accuracy 0.9859 Epoch 17 Finished - Accuracy 0.987 Epoch 18 Finished - Accuracy 0.9877 Epoch 19 Finished - Accuracy 0.9876 Epoch 20 Finished - Accuracy 0.9884 Epoch 21 Finished - Accuracy 0.989 Epoch 22 Finished - Accuracy 0.988 Epoch 23 Finished - Accuracy 0.9893 Epoch 24 Finished - Accuracy 0.9872 Epoch 25 Finished - Accuracy 0.9881 Epoch 26 Finished - Accuracy 0.9873 Epoch 27 Finished - Accuracy 0.9889 Epoch 28 Finished - Accuracy 0.9877 Epoch 29 Finished - Accuracy 0.9893 Epoch 30 Finished - Accuracy 0.9896 Epoch 31 Finished - Accuracy 0.9896 Epoch 32 Finished - Accuracy 0.9901 Epoch 33 Finished - Accuracy 0.9878 Epoch 34 Finished - Accuracy 0.9895 Epoch 35 Finished - Accuracy 0.9884 Epoch 36 Finished - Accuracy 0.9895 Epoch 37 Finished - Accuracy 0.9891 Epoch 38 Finished - Accuracy 0.99 Epoch 39 Finished - Accuracy 0.9893 Optimization Finished!