In [1]:
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("data/MNIST", one_hot=True)
In [2]:
# let us parameterize our code
num_classes = 10
img_size = 28
img_size_flat = img_size * img_size # 784
batch_size = 100
learning_rate = 0.01
training_epochs = 5000
# input data x
x_data = tf.placeholder(tf.float32, [None, img_size_flat], name="x_data") # shape = (batch_size, 784)
# input data labels
x_labels = tf.placeholder(tf.float32, [None, num_classes], name="x_labels") # shape = (batch_size, 10)
# random weights and biases
# we will make them Variable because we have to initialize with random values and then later train them
weights = tf.Variable(tf.ones([img_size_flat,num_classes]), name="weights") # shape = (784, 10)
biases = tf.Variable(tf.zeros([num_classes]), name="biases") # shape = (10)
# model
logits = tf.matmul(x_data, weights) + biases # predicted labels # shape = (batch_size, 10)
# introduce non-linearity
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=x_labels, name="cross_entropy")
# sum of all the cross_entropy is the cost of our model
cost = tf.reduce_mean(cross_entropy, name="cost")
optimizer = tf.train.AdamOptimizer(learning_rate=0.001, beta1=0.9, beta2=0.999, epsilon=1e-08, use_locking=False, name='Adam').minimize(cost)
### Now that the computation graph is ready, we need to run it using tf.Session()
# initialize the variables
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for _ in range(training_epochs):
batch_x , batch_y = mnist.train.next_batch(batch_size)
sess.run(fetches=optimizer, feed_dict={x_data:batch_x, x_labels:batch_y})
how_many_correct_pred = tf.equal(tf.argmax(x_labels, axis=1), tf.argmax(logits, 1), name="howManyCorrect")
accuracy = tf.reduce_mean(tf.cast(how_many_correct_pred, tf.float32))
# print the final accuracy on test data
print(" accuracy ", sess.run(fetches=accuracy, feed_dict={x_data:mnist.test.images, x_labels:mnist.test.labels}))