This notebook contains code to train a linear classifier on MNIST. At the end is a short exercise. When you've finished with this notebook, move on to 'deep_mnist' which expands our model into a deep, fully connected neural network and adds summaries to display in TensorBoard.


In [ ]:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

import numpy as np
import math

%matplotlib inline
import matplotlib.pyplot as plt

In [ ]:
tf.reset_default_graph()
sess = tf.Session()

In [ ]:
# Import the dataset
# It will be downloaded if you don't already have a local copy
mnist = input_data.read_data_sets('/tmp/data', one_hot=True)

In [ ]:
# Uncomment these lines to understand the format of the dataset.

## 1. How many examples do we have?
# print ('Train, validation, test: %d, %d, %d' % 
#       (len(mnist.train.images), len(mnist.validation.images), len(mnist.test.images)))

## 2. What's the format of a label? Notice they're "one-hot".
# print (mnist.train.labels[0])

## 3. What's the format of an image? 
# They're a "flattened" array of 28*28 = 784 pixels.
# print (len(mnist.train.images[0]))

## 4. How can you display an image?
# plt.imshow(mnist.train.images[0].reshape((28,28)), cmap=plt.cm.gray_r)   
## Note: the first image is oddly drawn.

In [ ]:
NUM_CLASSES = 10
NUM_PIXELS = 28 * 28
TRAIN_STEPS = 1000
BATCH_SIZE = 100
LEARNING_RATE = 0.5

# Placeholders for images and labels
# The first dimension is the batch size
# None means it can be of any length
# TensorFlow will infer it later
x = tf.placeholder(tf.float32, [None, NUM_PIXELS])
y_ = tf.placeholder(tf.float32, [None, NUM_CLASSES])

# Define the model
W = tf.Variable(tf.truncated_normal([NUM_PIXELS, NUM_CLASSES]))
b = tf.Variable(tf.zeros([NUM_CLASSES]))
y = tf.matmul(x, W) + b

# Define loss and optimizer
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y, labels=y_))
train_step = tf.train.GradientDescentOptimizer(LEARNING_RATE).minimize(loss)

# Initialize variables after the model is defined
sess.run(tf.global_variables_initializer())

# Train the model
for i in range(TRAIN_STEPS):
    batch_xs, batch_ys = mnist.train.next_batch(BATCH_SIZE)
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

# Evaluate the trained model
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
                                  
print("Accuracy %f" % sess.run(accuracy, feed_dict={x: mnist.test.images, 
                                                    y_: mnist.test.labels}))

Exercise

As written, the above code evaluates the accuracy of the trained model on the entire testing set. Can you write a function to predict the label for a single image from the testing set? Your function should display the image, the correct label, and the predicted label.

Tip: keep in mind the placeholders expect 2d arrays.


In [ ]:
# put your solution here