Tensorflow MNIST beginners tutorial from scratch

02.06.16. M.Evans


In [1]:
# Check tensorflow is installed
import tensorflow as tf

In [2]:
# Find out where it is
! python -c 'import os; import inspect; import tensorflow; print(os.path.dirname(inspect.getfile(tensorflow)))'


/home/mathew/miniconda/envs/tensorflow/lib/python3.4/site-packages/tensorflow

In [3]:
# Check the demo works 
! python -m tensorflow.models.image.mnist.convolutional


Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting data/train-images-idx3-ubyte.gz
Extracting data/train-labels-idx1-ubyte.gz
Extracting data/t10k-images-idx3-ubyte.gz
Extracting data/t10k-labels-idx1-ubyte.gz
Initialized!
Step 0 (epoch 0.00), 4.7 ms
Minibatch loss: 12.054, learning rate: 0.010000
Minibatch error: 90.6%
Validation error: 84.6%
Step 100 (epoch 0.12), 204.4 ms
Minibatch loss: 3.289, learning rate: 0.010000
Minibatch error: 6.2%
Validation error: 7.0%
Step 200 (epoch 0.23), 201.0 ms
Minibatch loss: 3.427, learning rate: 0.010000
Minibatch error: 10.9%
Validation error: 3.6%
Step 300 (epoch 0.35), 204.0 ms
Minibatch loss: 3.175, learning rate: 0.010000
Minibatch error: 9.4%
Validation error: 3.2%
Step 400 (epoch 0.47), 202.9 ms
Minibatch loss: 3.228, learning rate: 0.010000
Minibatch error: 7.8%
Validation error: 2.5%
Step 500 (epoch 0.58), 207.8 ms
Minibatch loss: 3.282, learning rate: 0.010000
Minibatch error: 9.4%
Validation error: 2.8%
Step 600 (epoch 0.70), 202.0 ms
Minibatch loss: 3.150, learning rate: 0.010000
Minibatch error: 4.7%
Validation error: 2.5%
Step 700 (epoch 0.81), 204.9 ms
Minibatch loss: 3.031, learning rate: 0.010000
Minibatch error: 3.1%
Validation error: 2.3%
Step 800 (epoch 0.93), 208.4 ms
Minibatch loss: 3.093, learning rate: 0.010000
Minibatch error: 6.2%
Validation error: 2.1%
Step 900 (epoch 1.05), 207.2 ms
Minibatch loss: 2.918, learning rate: 0.009500
Minibatch error: 3.1%
Validation error: 1.7%
Step 1000 (epoch 1.16), 204.4 ms
Minibatch loss: 2.848, learning rate: 0.009500
Minibatch error: 0.0%
Validation error: 1.7%
Step 1100 (epoch 1.28), 204.2 ms
Minibatch loss: 2.835, learning rate: 0.009500
Minibatch error: 1.6%
Validation error: 1.4%
Step 1200 (epoch 1.40), 204.6 ms
Minibatch loss: 2.923, learning rate: 0.009500
Minibatch error: 6.2%
Validation error: 1.5%
Step 1300 (epoch 1.51), 202.1 ms
Minibatch loss: 2.771, learning rate: 0.009500
Minibatch error: 1.6%
Validation error: 1.8%
Step 1400 (epoch 1.63), 202.5 ms
Minibatch loss: 2.808, learning rate: 0.009500
Minibatch error: 4.7%
Validation error: 1.4%
Step 1500 (epoch 1.75), 202.2 ms
Minibatch loss: 2.846, learning rate: 0.009500
Minibatch error: 6.2%
Validation error: 1.3%
Step 1600 (epoch 1.86), 203.4 ms
Minibatch loss: 2.702, learning rate: 0.009500
Minibatch error: 1.6%
Validation error: 1.3%
Step 1700 (epoch 1.98), 201.3 ms
Minibatch loss: 2.653, learning rate: 0.009500
Minibatch error: 0.0%
Validation error: 1.4%
Step 1800 (epoch 2.09), 201.7 ms
Minibatch loss: 2.661, learning rate: 0.009025
Minibatch error: 1.6%
Validation error: 1.5%
Step 1900 (epoch 2.21), 200.2 ms
Minibatch loss: 2.646, learning rate: 0.009025
Minibatch error: 1.6%
Validation error: 1.1%
Step 2000 (epoch 2.33), 202.7 ms
Minibatch loss: 2.657, learning rate: 0.009025
Minibatch error: 3.1%
Validation error: 1.3%
Step 2100 (epoch 2.44), 203.1 ms
Minibatch loss: 2.575, learning rate: 0.009025
Minibatch error: 1.6%
Validation error: 1.1%
Step 2200 (epoch 2.56), 201.3 ms
Minibatch loss: 2.574, learning rate: 0.009025
Minibatch error: 0.0%
Validation error: 1.2%
Step 2300 (epoch 2.68), 204.3 ms
Minibatch loss: 2.575, learning rate: 0.009025
Minibatch error: 1.6%
Validation error: 1.1%
Step 2400 (epoch 2.79), 206.4 ms
Minibatch loss: 2.499, learning rate: 0.009025
Minibatch error: 0.0%
Validation error: 1.2%
Step 2500 (epoch 2.91), 206.0 ms
Minibatch loss: 2.477, learning rate: 0.009025
Minibatch error: 0.0%
Validation error: 1.2%
Step 2600 (epoch 3.03), 206.1 ms
Minibatch loss: 2.461, learning rate: 0.008574
Minibatch error: 0.0%
Validation error: 1.2%
Step 2700 (epoch 3.14), 207.4 ms
Minibatch loss: 2.462, learning rate: 0.008574
Minibatch error: 1.6%
Validation error: 1.0%
Step 2800 (epoch 3.26), 205.1 ms
Minibatch loss: 2.425, learning rate: 0.008574
Minibatch error: 1.6%
Validation error: 1.2%
Step 2900 (epoch 3.37), 206.9 ms
Minibatch loss: 2.486, learning rate: 0.008574
Minibatch error: 3.1%
Validation error: 1.2%
Step 3000 (epoch 3.49), 208.6 ms
Minibatch loss: 2.400, learning rate: 0.008574
Minibatch error: 1.6%
Validation error: 1.0%
Step 3100 (epoch 3.61), 203.8 ms
Minibatch loss: 2.396, learning rate: 0.008574
Minibatch error: 3.1%
Validation error: 0.9%
Step 3200 (epoch 3.72), 204.0 ms
Minibatch loss: 2.341, learning rate: 0.008574
Minibatch error: 0.0%
Validation error: 1.2%
Step 3300 (epoch 3.84), 202.3 ms
Minibatch loss: 2.308, learning rate: 0.008574
Minibatch error: 0.0%
Validation error: 1.3%
Step 3400 (epoch 3.96), 202.5 ms
Minibatch loss: 2.295, learning rate: 0.008574
Minibatch error: 0.0%
Validation error: 1.1%
Step 3500 (epoch 4.07), 203.4 ms
Minibatch loss: 2.274, learning rate: 0.008145
Minibatch error: 0.0%
Validation error: 1.0%
Step 3600 (epoch 4.19), 201.3 ms
Minibatch loss: 2.259, learning rate: 0.008145
Minibatch error: 0.0%
Validation error: 1.0%
Step 3700 (epoch 4.31), 203.3 ms
Minibatch loss: 2.263, learning rate: 0.008145
Minibatch error: 3.1%
Validation error: 1.0%
Step 3800 (epoch 4.42), 202.5 ms
Minibatch loss: 2.244, learning rate: 0.008145
Minibatch error: 1.6%
Validation error: 0.9%
Step 3900 (epoch 4.54), 204.9 ms
Minibatch loss: 2.277, learning rate: 0.008145
Minibatch error: 3.1%
Validation error: 1.0%
Step 4000 (epoch 4.65), 204.4 ms
Minibatch loss: 2.213, learning rate: 0.008145
Minibatch error: 1.6%
Validation error: 1.0%
Step 4100 (epoch 4.77), 205.1 ms
Minibatch loss: 2.182, learning rate: 0.008145
Minibatch error: 1.6%
Validation error: 0.9%
Step 4200 (epoch 4.89), 204.3 ms
Minibatch loss: 2.238, learning rate: 0.008145
Minibatch error: 1.6%
Validation error: 1.0%
Step 4300 (epoch 5.00), 202.8 ms
Minibatch loss: 2.194, learning rate: 0.007738
Minibatch error: 1.6%
Validation error: 0.9%
Step 4400 (epoch 5.12), 203.6 ms
Minibatch loss: 2.154, learning rate: 0.007738
Minibatch error: 1.6%
Validation error: 1.0%
Step 4500 (epoch 5.24), 202.3 ms
Minibatch loss: 2.171, learning rate: 0.007738
Minibatch error: 1.6%
Validation error: 0.9%
Step 4600 (epoch 5.35), 203.7 ms
Minibatch loss: 2.115, learning rate: 0.007738
Minibatch error: 1.6%
Validation error: 0.9%
Step 4700 (epoch 5.47), 204.8 ms
Minibatch loss: 2.103, learning rate: 0.007738
Minibatch error: 1.6%
Validation error: 0.9%
Step 4800 (epoch 5.59), 201.8 ms
Minibatch loss: 2.062, learning rate: 0.007738
Minibatch error: 0.0%
Validation error: 1.1%
Step 4900 (epoch 5.70), 203.9 ms
Minibatch loss: 2.046, learning rate: 0.007738
Minibatch error: 0.0%
Validation error: 0.9%
Step 5000 (epoch 5.82), 202.9 ms
Minibatch loss: 2.125, learning rate: 0.007738
Minibatch error: 3.1%
Validation error: 1.0%
Step 5100 (epoch 5.93), 203.6 ms
Minibatch loss: 2.012, learning rate: 0.007738
Minibatch error: 1.6%
Validation error: 1.0%
Step 5200 (epoch 6.05), 203.4 ms
Minibatch loss: 2.059, learning rate: 0.007351
Minibatch error: 3.1%
Validation error: 0.9%
Step 5300 (epoch 6.17), 205.3 ms
Minibatch loss: 1.986, learning rate: 0.007351
Minibatch error: 0.0%
Validation error: 0.9%
Step 5400 (epoch 6.28), 204.3 ms
Minibatch loss: 1.955, learning rate: 0.007351
Minibatch error: 0.0%
Validation error: 0.8%
Step 5500 (epoch 6.40), 203.6 ms
Minibatch loss: 1.964, learning rate: 0.007351
Minibatch error: 1.6%
Validation error: 0.9%
Step 5600 (epoch 6.52), 203.8 ms
Minibatch loss: 1.928, learning rate: 0.007351
Minibatch error: 0.0%
Validation error: 0.7%
Step 5700 (epoch 6.63), 202.9 ms
Minibatch loss: 1.913, learning rate: 0.007351
Minibatch error: 0.0%
Validation error: 0.9%
Step 5800 (epoch 6.75), 203.2 ms
Minibatch loss: 1.899, learning rate: 0.007351
Minibatch error: 0.0%
Validation error: 0.9%
Step 5900 (epoch 6.87), 202.7 ms
Minibatch loss: 1.892, learning rate: 0.007351
Minibatch error: 0.0%
Validation error: 0.8%
Step 6000 (epoch 6.98), 200.7 ms
Minibatch loss: 1.884, learning rate: 0.007351
Minibatch error: 0.0%
Validation error: 0.9%
Step 6100 (epoch 7.10), 201.4 ms
Minibatch loss: 1.858, learning rate: 0.006983
Minibatch error: 0.0%
Validation error: 1.0%
Step 6200 (epoch 7.21), 201.5 ms
Minibatch loss: 1.842, learning rate: 0.006983
Minibatch error: 0.0%
Validation error: 0.7%
Step 6300 (epoch 7.33), 204.8 ms
Minibatch loss: 1.836, learning rate: 0.006983
Minibatch error: 0.0%
Validation error: 0.8%
Step 6400 (epoch 7.45), 203.5 ms
Minibatch loss: 1.915, learning rate: 0.006983
Minibatch error: 3.1%
Validation error: 0.8%
Step 6500 (epoch 7.56), 201.6 ms
Minibatch loss: 1.807, learning rate: 0.006983
Minibatch error: 0.0%
Validation error: 0.9%
Step 6600 (epoch 7.68), 202.8 ms
Minibatch loss: 1.829, learning rate: 0.006983
Minibatch error: 1.6%
Validation error: 0.9%
Step 6700 (epoch 7.80), 202.8 ms
Minibatch loss: 1.784, learning rate: 0.006983
Minibatch error: 0.0%
Validation error: 0.9%
Step 6800 (epoch 7.91), 201.9 ms
Minibatch loss: 1.772, learning rate: 0.006983
Minibatch error: 0.0%
Validation error: 0.8%
Step 6900 (epoch 8.03), 202.5 ms
Minibatch loss: 1.760, learning rate: 0.006634
Minibatch error: 0.0%
Validation error: 1.0%
Step 7000 (epoch 8.15), 203.1 ms
Minibatch loss: 1.786, learning rate: 0.006634
Minibatch error: 1.6%
Validation error: 0.8%
Step 7100 (epoch 8.26), 205.7 ms
Minibatch loss: 1.736, learning rate: 0.006634
Minibatch error: 0.0%
Validation error: 0.9%
Step 7200 (epoch 8.38), 202.4 ms
Minibatch loss: 1.740, learning rate: 0.006634
Minibatch error: 0.0%
Validation error: 0.9%
Step 7300 (epoch 8.49), 202.5 ms
Minibatch loss: 1.719, learning rate: 0.006634
Minibatch error: 0.0%
Validation error: 0.8%
Step 7400 (epoch 8.61), 201.3 ms
Minibatch loss: 1.700, learning rate: 0.006634
Minibatch error: 0.0%
Validation error: 0.8%
Step 7500 (epoch 8.73), 203.9 ms
Minibatch loss: 1.697, learning rate: 0.006634
Minibatch error: 0.0%
Validation error: 0.8%
Step 7600 (epoch 8.84), 202.8 ms
Minibatch loss: 1.823, learning rate: 0.006634
Minibatch error: 1.6%
Validation error: 0.9%
Step 7700 (epoch 8.96), 206.3 ms
Minibatch loss: 1.667, learning rate: 0.006634
Minibatch error: 0.0%
Validation error: 1.0%
Step 7800 (epoch 9.08), 205.2 ms
Minibatch loss: 1.662, learning rate: 0.006302
Minibatch error: 0.0%
Validation error: 0.9%
Step 7900 (epoch 9.19), 205.1 ms
Minibatch loss: 1.651, learning rate: 0.006302
Minibatch error: 0.0%
Validation error: 0.9%
Step 8000 (epoch 9.31), 203.4 ms
Minibatch loss: 1.682, learning rate: 0.006302
Minibatch error: 3.1%
Validation error: 0.8%
Step 8100 (epoch 9.43), 203.8 ms
Minibatch loss: 1.626, learning rate: 0.006302
Minibatch error: 0.0%
Validation error: 0.9%
Step 8200 (epoch 9.54), 203.6 ms
Minibatch loss: 1.632, learning rate: 0.006302
Minibatch error: 0.0%
Validation error: 0.8%
Step 8300 (epoch 9.66), 205.4 ms
Minibatch loss: 1.609, learning rate: 0.006302
Minibatch error: 0.0%
Validation error: 0.8%
Step 8400 (epoch 9.77), 203.8 ms
Minibatch loss: 1.595, learning rate: 0.006302
Minibatch error: 0.0%
Validation error: 0.8%
Step 8500 (epoch 9.89), 203.8 ms
Minibatch loss: 1.617, learning rate: 0.006302
Minibatch error: 1.6%
Validation error: 0.8%
Test error: 0.8%

In [5]:
! pip install matplotlib


Collecting matplotlib
  Downloading matplotlib-1.5.1.tar.gz (54.0MB)
    100% |████████████████████████████████| 54.0MB 18kB/s 
Requirement already satisfied (use --upgrade to upgrade): numpy>=1.6 in /home/mathew/miniconda/envs/tensorflow/lib/python3.4/site-packages (from matplotlib)
Collecting python-dateutil (from matplotlib)
  Downloading python_dateutil-2.5.3-py2.py3-none-any.whl (201kB)
    100% |████████████████████████████████| 204kB 2.5MB/s 
Collecting pytz (from matplotlib)
  Downloading pytz-2016.4-py2.py3-none-any.whl (480kB)
    100% |████████████████████████████████| 481kB 1.8MB/s 
Collecting cycler (from matplotlib)
  Downloading cycler-0.10.0-py2.py3-none-any.whl
Collecting pyparsing!=2.0.0,!=2.0.4,>=1.5.6 (from matplotlib)
  Downloading pyparsing-2.1.4-py2.py3-none-any.whl (40kB)
    100% |████████████████████████████████| 40kB 6.4MB/s 
Requirement already satisfied (use --upgrade to upgrade): six>=1.5 in /home/mathew/miniconda/envs/tensorflow/lib/python3.4/site-packages (from python-dateutil->matplotlib)
Building wheels for collected packages: matplotlib
  Running setup.py bdist_wheel for matplotlib ... - \ | / - \ | / - \ | / - \ | / - \ | / - \ | / - \ | / - \ | / - \ | / - \ | / - \ | / - \ | / - \ | / - \ | / - \ | / - \ | / - \ | / - \ | / - \ | / - \ | / - \ | / - \ | / - \ | done
  Stored in directory: /home/mathew/.cache/pip/wheels/d8/38/3c/a388e11fd09f9b23f5e4cd74594197394d9fd65f91f64c4aa7
Successfully built matplotlib
Installing collected packages: python-dateutil, pytz, cycler, pyparsing, matplotlib
Successfully installed cycler-0.10.0 matplotlib-1.5.1 pyparsing-2.1.4 python-dateutil-2.5.3 pytz-2016.4

Hacking starts here


In [20]:
# Tensorflow example from first page of docs
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

from IPython import display

In [7]:
# Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data * 0.1 + 0.3

# Plot the data
plt.scatter(x_data,y_data)


Out[7]:
<matplotlib.collections.PathCollection at 0x7f6ae9a56cf8>

In [24]:
# Try to find values for W and b that compute y_data = W * x_data + b
# (We know that W should be 0.1 and b 0.3, but Tensorflow will
# figure that out for us.)

# Variables to be computed (changed) must be initialised within tensorflow
W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
b = tf.Variable(tf.zeros([1]))
y = W * x_data + b

# Then specify what you will do with some of your variables in what tf calls a 'data flow graph' e.g.

# Minimize the mean squared errors.
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

# Before starting, initialize the variables.  We will 'run' this first.
init = tf.initialize_all_variables()

# Launch the graph by starting a tf session, then running 'init'
sess = tf.Session()
sess.run(init)

# Fit the line with tensorflow.
for step in range(201): #no xrange in python 3
    sess.run(train)
    new_y = sess.run(W) * x_data + sess.run(b)
    
    if step % 20 == 0:
        # Plot the data and the result
        plt.scatter(x_data,y_data)
        plt.plot(x_data,new_y,'r')
        display.clear_output(wait=True)
        display.display(plt.gcf())
        print(step, sess.run(W), sess.run(b))
# Learns best fit is W: [0.1], b: [0.3]


200 [ 0.1000001] [ 0.29999995]

In [25]:
# Get result from tf

new_y = sess.run(W) * x_data + sess.run(b)

# Plot the data and the result
plt.scatter(x_data,y_data)
plt.plot(x_data,new_y,'r')


Out[25]:
[<matplotlib.lines.Line2D at 0x7f6a9c54d9b0>]

Now run the MNIST code


In [26]:
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/",one_hot=True)


Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Extracting MNIST_data/train-images-idx3-ubyte.gz
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz

In [32]:
mnist.train.images.shape


Out[32]:
(55000, 784)

In [42]:
# Imageplot of one of the images
plt.imshow(mnist.train.images[1,:].reshape((28,28)))


Out[42]:
<matplotlib.image.AxesImage at 0x7f6aac05acf8>

In [39]:
im = mnist.train.images[0,:].reshape((28,28))
im.shape


Out[39]:
(28, 28)

In [51]:
# Image of some labels
fig, ax = plt.subplots(figsize = (20,1))
plt.imshow(mnist.train.labels[0:100,:].T,interpolation = 'none')


Out[51]:
<matplotlib.image.AxesImage at 0x7f6a9c5d3ba8>

In [52]:
# Placeholder array. None means it can be any number of 784 parameter inputs
x = tf.placeholder(tf.float32,[None,784])

In [53]:
# W and b are model variables to be fit by the model
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))

In [54]:
# This is the softmax nn model
y = tf.nn.softmax(tf.matmul(x,W) + b)

In [55]:
# Placeholder for correct answers (test labels)
y_ = tf.placeholder(tf.float32,[None,10])

In [57]:
# Cross entropy loss to assess model performance
# i.e. batch mean of negative sum of log(prediction) * labels
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))

In [58]:
# Implement cross entropy loss with gradient descent on every training step
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

In [60]:
# initialise variables in tf
init = tf.initialize_all_variables()

In [62]:
# Start tf session
sess = tf.Session()
sess.run(init)

In [64]:
# Run a training loop
for i in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step,feed_dict={x: batch_xs, y_: batch_ys})

In [65]:
# Evaluate model performance
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))

In [68]:
# Convert boolean correct_prediction array into numbers, then compute an average accuracy
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

In [70]:
# Print output of calculation, which involves a sess.run()
print(sess.run(accuracy,feed_dict={x: mnist.test.images,y_:mnist.test.labels}))


0.916

We did it!

So concludes this simple example notebook

In [73]:
sess.close()

In [ ]: