Deep Neural Network in TensorFlow

In this notebook, we convert our intermediate-depth MNIST-classifying neural network from Keras to TensorFlow (compare them side by side) following Aymeric Damien's Multi-Layer Perceptron Notebook style.

Subsequently, we add a layer to make it deep!

Load dependencies


In [ ]:
import numpy as np
np.random.seed(42)
import tensorflow as tf
tf.set_random_seed(42)

Load data


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

Set neural network hyperparameters (tidier at top of file!)


In [ ]:
lr = 0.1
epochs = 1
batch_size = 128
weight_initializer = tf.contrib.layers.xavier_initializer()

Set number of neurons for each layer


In [ ]:
n_input = 784
n_dense_1 = 64
n_dense_2 = 64
n_classes = 10

Define placeholders Tensors for inputs and labels


In [ ]:
x = tf.placeholder(tf.float32, [None, n_input])
y = tf.placeholder(tf.float32, [None, n_classes])

Define types of layers


In [ ]:
# dense layer with ReLU activation:
def dense(x, W, b):
    # DEFINE
    # DEFINE
    return a

Define dictionaries for storing weights and biases for each layer -- and initialize


In [ ]:
bias_dict = {
    'b1': tf.Variable(tf.zeros([n_dense_1])), 
    'b2': tf.Variable(tf.zeros([n_dense_2])),
    'b_out': tf.Variable(tf.zeros([n_classes]))
}

weight_dict = {
    'W1': tf.get_variable('W1', [n_input, n_dense_1], initializer=weight_initializer),
    'W2': tf.get_variable('W2', [n_dense_1, n_dense_2], initializer=weight_initializer),
    'W_out': tf.get_variable('W_out', [n_dense_2, n_classes], initializer=weight_initializer)
}

Design neural network architecture


In [ ]:
def network(x, weights, biases):
    
    # DEFINE
  
    return out_layer_z

Build model


In [ ]:
predictions = network(x, weights=weight_dict, biases=bias_dict)

Define model's loss and its optimizer


In [ ]:
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=predictions, labels=y))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=lr).minimize(cost)

Define evaluation metrics


In [ ]:
# calculate accuracy by identifying test cases where the model's highest-probability class matches the true y label: 
correct_prediction = tf.equal(tf.argmax(predictions, 1), tf.argmax(y, 1))
accuracy_pct = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) * 100

Create op for variable initialization


In [ ]:
initializer_op = tf.global_variables_initializer()

Train the network in a session


In [ ]:
with tf.Session() as session:
    session.run(initializer_op)
    
    print("Training for", epochs, "epochs.")
    
    # loop over epochs: 
    for epoch in range(epochs):
        
        avg_cost = 0.0 # track cost to monitor performance during training
        avg_accuracy_pct = 0.0
        
        # loop over all batches of the epoch:
        n_batches = int(mnist.train.num_examples / batch_size)
        for i in range(n_batches):
            
            batch_x, batch_y = mnist.train.next_batch(batch_size)
            
            # feed batch data to run optimization and fetching cost and accuracy: 
            _, batch_cost, batch_acc = session.run([optimizer, cost, accuracy_pct], 
                                                   feed_dict={x: batch_x, y: batch_y})
            
            # accumulate mean loss and accuracy over epoch: 
            avg_cost += batch_cost / n_batches
            avg_accuracy_pct += batch_acc / n_batches
            
        # output logs at end of each epoch of training:
        print("Epoch ", '%03d' % (epoch+1), 
              ": cost = ", '{:.3f}'.format(avg_cost), 
              ", accuracy = ", '{:.2f}'.format(avg_accuracy_pct), "%", 
              sep='')
    
    print("Training Complete. Testing Model.\n")
    
    test_cost = cost.eval({x: mnist.test.images, y: mnist.test.labels})
    test_accuracy_pct = accuracy_pct.eval({x: mnist.test.images, y: mnist.test.labels})
    
    print("Test Cost:", '{:.3f}'.format(test_cost))
    print("Test Accuracy: ", '{:.2f}'.format(test_accuracy_pct), "%", sep='')