Tensorflow 8: MNIST NN 1

My previous post. .  All my code can be found on github (8_MNIST_1.ipynb)

MNIST

The MNIST dataset consists of images of handwritten digits comprising of 55,000 training examples, 10,000 training examples and 5000 validation examples. MNIST is an extremely popular image dataset to work on because its easy to get started on and you can try different approaches that increase the accuracy of your solution.  It originates from http://yann.lecun.com/exdb/mnist/ 

Data Acquisition

First lets download the data.  We will store this in /tmp/data/ folder.  The MNIST dataset is so popular that Tensorflow includes helper functions to download it as part of the library.  We will copy the Tensorflow official MNIST tutorial code to download the files from here.  Each part will be stored in X_Type and Y_Type variables.

The data is split into training, validation and test data.  We use the training and validation data during training and the test data only to test our final solution.  We want to avoid an issue called overfitting.  This is when the system begins to memorise the training data instead of learning a more generalised solution.  If the accuracy of the system on the training data increases but the accuracy over the validation data (a dataset which it hasn’t been trained on) also doesn’t increase this indicates overfitting and you should stop training.

This is what the data actually looks like.

As you can see the Y_train[0] represents the number 7 because all other labels are value = 0 and 7 has value 1.  The image training data is a 28 x 28 pixel 2D array flattened down to one long 784 long array. This removes the 2D structure of the data.  Each pixel in the 784 long float array is between 0 and 1 in greyscale intensity.  Therefore X_Train[0] is a giant 784 long float array.

To visualise what the actual MNIST images look like i will make a simple matplotlib function to take an input array and output the image.  It has an optional inverse command.

Here is a 3 and an inversed 3.

And a 6 and 1.  The 6 is not very legible.

Basic NN

We will reuse the same model from the previous lesson with some minor modifications.

Firstly we now have 784 inputs for each pixel of data and 10 outputs for the digits 0 to 9.  To begin with i will set the hidden layer to 200 nodes and a learning rate of 0.001.

Same placeholders as before

I will reuse the same code for the Neural Network and use the same loss function and optimizer.

Note nn.softmax_cross_entropy_with_logits performs the nn.softmax operation so we don’t need to explicitly call it.

Training

The training looks the same as the previous lesson except for the following line:

Instead of training the network on the entire 55,000 training examples we use a random sample, a batch of training data.  In this case we use 1000 items.  This method of using batches instead of the complete set is called stochastic training.  Hence combining this with gradient descent is called stochastic gradient descent.

 

One of the first things you will notice is that with a huge increase in the number of inputs, hidden nodes and outputs we have a lot more weights and biases to train. Also we have a lot more training data to go though.  As a result the system takes a lot longer to train.  Many large Neural networks can take many hours, days or weeks to train.

Now we need to test the trained systems accuracy across the test data set to see how accurate the system is.  We will pass the training data through the system to do this.  The code was mainly taken from the ‘Evaluating Our Model‘ section on the tensorflow official MNIST tutorial.

tf.argmax(y,1) gets the index of the heighest value.  So this will give us the output of the trained system e.g. 7.  tf.argmax(y_data,1) will give us the trained data output.  If they are the same (tf.equal) then we get a list of boolean values.

This converts the boolean array list into a float percentage.

We need to pass the training data into the accuracy function.  Then print out the result

 

We get 87% which isn’t great but for a very simple one layer NN is ok.

Analysis

So with the model trained we can test the system by throwing in some test data and seeing what output probabilities are.

I will create another bar graph function that will take in an MNIST image and label and output what our trained system believes the image to be.

x1 and y1 are the input image and label.  I will output what the label says the “Actual” value and then what our system “Predicts” the value it.  I will also show the image and then show a bar graph of the probabilities.

I call it by getting a single test data set.

Some predictions work well.

Whilst others are clearly wrong.  This is expected with such a simple network structure and low 86% accuracy.  Note however that the items that it gets confused about are typically similar looking digits such as a 7 or 9.

In the next lesson we will implement a multilayer NN to try and inprove the accuracy of the system.