In [1]:
import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

import tensorflow as tf
sess = tf.InteractiveSession()


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

In [2]:
x = tf.placeholder('float', shape=[None, 784])
y_ = tf.placeholder('float', shape=[None,10])

In [3]:
# create the weights and bias for the linear layer
w = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))

In [4]:
# before we can initiate the session, we must initialize the variables 
sess.run(tf.initialize_all_variables())

In [5]:
# do prediction using linear softmax classfier, into 10 classes
y = tf.nn.softmax(tf.matmul(x,w)+b)

# cross entropy loss function
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
# note tf.reduce sums across all images in the minibatch, as well as classes, thus we're computing
# the cross entropy for the entire minibatch

In [7]:
# train the network with minibatch gradient descent
# note that Tensorflow knows the entire computational graph and thus can automatically differentiate the gradient
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
# training step size of 0.01

for i in range (1000):
    batch = mnist.train.next_batch(50)
    train_step.run(feed_dict={x:batch[0], y_: batch[1]})

In [8]:
# evaluate our model performance
# correct_prediction creates a list of booleans [true, false, false....] of size # of examples,
# which we can then use to calculate the mean and accuracy of our prediction
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))

In [10]:
# creats accuracy, which we can use to calculate the mean and accuracy
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

print accuracy.eval(feed_dict={x: mnist.test.images, y_:mnist.test.labels})

# accuracy should be around 91% or 0.91.


0.9092

In [ ]: