This notebook contains code to train a fully connected neural network on MNIST using tf.contrib.learn. At the end is a short exercise.


In [ ]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import tensorflow as tf
learn = tf.contrib.learn
tf.logging.set_verbosity(tf.logging.ERROR)

Import the dataset


In [ ]:
mnist = learn.datasets.load_dataset('mnist')
data = mnist.train.images
labels = np.asarray(mnist.train.labels, dtype=np.int32)
test_data = mnist.test.images
test_labels = np.asarray(mnist.test.labels, dtype=np.int32)

There are 55k examples in train, and 10k in eval. You may wish to limit the size to experiment faster.


In [ ]:
max_examples = 10000
data = data[:max_examples]
labels = labels[:max_examples]

Display some digits


In [ ]:
def display(i):
    img = test_data[i]
    plt.title('Example %d. Label: %d' % (i, test_labels[i]))
    plt.imshow(img.reshape((28,28)), cmap=plt.cm.gray_r)

In [ ]:
display(0)

In [ ]:
display(1)

These digits are clearly drawn. Here's one that's not.


In [ ]:
display(8)

Now let's take a look at how many features we have.


In [ ]:
print len(data[0])

Fit a Linear Classifier

Our goal here is to get about 90% accuracy with this simple classifier.


In [ ]:
feature_columns = learn.infer_real_valued_columns_from_input(data)
classifier = learn.LinearClassifier(feature_columns=feature_columns, n_classes=10)
classifier.fit(data, labels, batch_size=100, steps=1000)

Evaluate accuracy


In [ ]:
classifier.evaluate(test_data, test_labels)["accuracy"]

Classify a few examples

We can make predictions on individual images as well. Note: the predict method accepts an array of samples as input, and returns a generator.


In [ ]:
# here's one it gets right
print ("Predicted %d, Label: %d" % (list(classifier.predict(test_data[0:1]))[0], test_labels[0]))
display(0)

In [ ]:
# and one it gets wrong
print ("Predicted %d, Label: %d" % (list(classifier.predict(test_data[8:9]))[0], test_labels[8]))
display(8)

Visualize learned weights

Let's see if we can reproduce the pictures of the weights in the TensorFlow Basic MNSIT tutorial.


In [ ]:
weights = classifier.weights_
f, axes = plt.subplots(2, 5, figsize=(10,4))
axes = axes.reshape(-1)
for i in range(len(axes)):
    a = axes[i]
    a.imshow(weights.T[i].reshape(28, 28), cmap=plt.cm.seismic)
    a.set_title(i)
    a.set_xticks(()) # ticks be gone
    a.set_yticks(())
plt.show()

Exercise: switch the estimator to a DNN


In [ ]:
# Build 2 layer DNN with 128, 32 units respectively.
# Play with these parameters to see if you can do better
# How? See https://www.tensorflow.org/versions/r0.12/tutorials/tflearn/index.html#tf-contrib-learn-quickstart

Has our accuracy improved?


In [ ]:
classifier.evaluate(test_data, test_labels)["accuracy"]

Next steps