TensorFlow Simple Model

MNIST Data


In [ ]:
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data

def display_stats(sample_image, sample_label):
    print('Extract Not One Hot Encoded data for Stats:')
    mnist_not_one_hot = input_data.read_data_sets('.', one_hot=False)

    print('\nStats:')
    print('Training Samples: {}'.format(mnist_not_one_hot.train.num_examples))
    print(dict(zip(*np.unique(mnist_not_one_hot.train.labels, return_counts=True))))
    print('Validation Samples: {}'.format(mnist_not_one_hot.validation.num_examples))
    print(dict(zip(*np.unique(mnist_not_one_hot.validation.labels, return_counts=True))))
    print('Testing Samples: {}'.format(mnist_not_one_hot.test.num_examples))
    print(dict(zip(*np.unique(mnist_not_one_hot.test.labels, return_counts=True))))

    print('\nExamples:')
    print('Image - Min: {} Max: {}'.format(sample_image.min(), sample_image.max()))
    print('Label: {}'.format(sample_label))
    print('First 20 Labels: {}'.format(mnist_not_one_hot.train.labels[:20]))

mnist = input_data.read_data_sets('.', one_hot=True)
display_stats(mnist.train.images[0], mnist.train.labels[0])

Hyperparameter


In [ ]:
import tensorflow as tf

# Parameters
training_epochs = 16
batch_size = 100

Graph Input


In [ ]:
# Input Sizes
n_input = 784  # MNIST data input (img shape: 28*28)
n_classes = 10  # MNIST total classes (0-9 digits)

features = tf.placeholder("float", [None, n_input], name='features')
labels = tf.placeholder("float", [None, n_classes], name='labels')

Layer Widths, Weights, and biases


In [ ]:
layer_widths = {
    'h1': 256,
    'h2': 256,
    'out': n_classes}
weights = {
    'h1': tf.Variable(tf.random_normal([n_input, layer_widths['h1']])),
    'h2': tf.Variable(tf.random_normal([layer_widths['h1'], layer_widths['h2']])),
    'out': tf.Variable(tf.random_normal([layer_widths['h2'], layer_widths['out']]))}
biases = {
    'b1': tf.Variable(tf.random_normal([layer_widths['h1']])),
    'b2': tf.Variable(tf.random_normal([layer_widths['h2']])),
    'out': tf.Variable(tf.random_normal([layer_widths['out']]))}

Build Layers


In [ ]:
# Hidden layer with RELU activation
layer_1 = tf.add(tf.matmul(features, weights['h1']), biases['b1'])
layer_1 = tf.nn.relu(layer_1)
# Hidden layer with RELU activation
layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
layer_2 = tf.nn.relu(layer_2)
# Output layer with linear activation
logits = tf.matmul(layer_2, weights['out']) + biases['out']

Loss and Optimizer


In [ ]:
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels), name='cost')
optimizer = tf.train.AdamOptimizer().minimize(cost, name='optimizer')

Accuracy


In [ ]:
correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(labels, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"), name='accuracy')

Train and Save


In [ ]:
save_path = './2_simple_model'

with tf.Session() as sess:
    # Initializing the variables
    sess.run(tf.global_variables_initializer())

    # Training cycle
    for epoch in range(training_epochs):
        # Loop over all batches
        for _ in range(mnist.train._num_examples//batch_size):
            batch_x, batch_y = mnist.train.next_batch(batch_size)
            _, loss = sess.run([optimizer, cost], feed_dict={features: batch_x, labels: batch_y})

        # Print Loss
        print('Epoch {:>2} - Loss: {}'.format(epoch, loss))
        
    # Save Model
    saver = tf.train.Saver()
    save_path = saver.save(sess, save_path)

    # Print accuracy
    print("Accuracy:", accuracy.eval({features: mnist.test.images, labels: mnist.test.labels}))