Simple TensorFlow Model on MNIST Database

This section provides a tutorial example on how to build a TensorFlow model for the MNIST database. The model is about 92% accurate after training with 100,000 samples.

In the previous tutorial, we learned how to access the MNIST database and fetch sample 28x28 images of handwritten digits. In this tutorial, let's review a simple TensorFlow model on the MNIST database, provided by Seldon Technologies at https://docs.seldon.io/projects/seldon-core/en/v0.3.0/examples/deep_mnist.html

Here is their source code with my comments:

#- mnist_simple_model.py
#- Source: https://www.geeksforgeeks.org/introduction-to-tensorflow/
#- Comments added

# load MNIST database with the target label converted
# to a one-hot vector like 3 -> [0,0,0,1,0,0,0,0,0,0]
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot = True)

import tensorflow as tf

# to run a the main script only
if __name__ == '__main__':

    # placeholder for 784 (28x28 pixels) features of input images
    # can feed a batch of multiple images
    x = tf.placeholder(tf.float32, [None,784], name="x")

    # variable for weights, to be optimized
    W = tf.Variable(tf.zeros([784,10]))

    # variable for bias, to be optimized
    b = tf.Variable(tf.zeros([10]))

    # output function to generate predictions
    y = tf.nn.softmax(tf.matmul(x,W) + b, name="y")

    # placeholder for expected targets
    y_ = tf.placeholder(tf.float32, [None, 10])

    # cost function
    cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y),
                    reduction_indices=[1]))

    # optimize operation
    train_step =
        tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

    # initialize operation for all variables: weights W and bias b
    init = tf.initialize_all_variables()

    # creates a TensorFlow session
    sess = tf.Session()

    # run the initialization operation for weights W and bias b
    sess.run(init)

    # loop 1000 batches
    for i in range(1000):

        # fetch 100 samples in a batch
        batch_xs, batch_ys = mnist.train.next_batch(100)

        # run optimize operation once with the batch
        sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

    # operation to compare predictions with targets of the batch
    correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))

    # operation to calculate accuracy: success / total
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    # calculate and print accuracy
    print(sess.run(accuracy, feed_dict = {x: mnist.test.images,
        y_:mnist.test.labels}))

    # save the trained model
    saver = tf.train.Saver()
    saver.save(sess, "model/deep_mnist_model")

If you run the above script, you should get something like:

herong$ python3 mnist_simple_model.py

0.9152
'model/deep_mnist_model'

Not too bad. After training with 100,000 (1000x100) samples, the model is about 91.52% accurate.

Table of Contents

 About This Book

 Deep Playground for Classical Neural Networks

 Building Neural Networks with Python

 Simple Example of Neural Networks

TensorFlow - Machine Learning Platform

 What Is TensorFlow

 "tensorflow" - TensorFlow Python Library

 "tensorflow" Interactive Test Web Page

 Tensor and Tensor Flow Graph

 Tensor Operation Properties

 TensorFlow Session Class and run() Function

 TensorFlow Variable Class and load() Function

 Linear Regression with TensorFlow

 tensorflow.examples.tutorials.mnist Module

Simple TensorFlow Model on MNIST Database

 Commonly Used TensorFlow Funcitons

 PyTorch - Machine Learning Platform

 CNN (Convolutional Neural Network)

 RNN (Recurrent Neural Network)

 GNN (Graph Neural Network)

 References

 Full Version in PDF/EPUB