<jupyter_start><jupyter_text>Intermediate Neural Network in TensorFlow In this notebook, we convert our [intermediate-depth MNIST-classifying neural network](https://github.com/the-deep-learners/TensorFlow-LiveLessons/blob/master/notebooks/intermediate_net_in_keras.ipynb) from Keras to TensorFlow (compare them side by side) following <NAME>'s [Multi-Layer Perceptron Notebook](https://github.com/aymericdamien/TensorFlow-Examples/blob/master/notebooks/3_NeuralNetworks/multilayer_perceptron.ipynb) style.  Load dependencies<jupyter_code>import numpy as np
np.random.seed(42)
import tensorflow as tf
tf.set_random_seed(42)<jupyter_output><empty_output><jupyter_text>Load dataSmall cheat here - it's easier to load the data from keras' libraries. TensorFlow previously had these common datasets in `tf.contrib` however that module is experimental and is likely to change heavily between TensorFlow versions. As such, it's easier to get the data from a stable source.You could, however, download the data yourself from (Yann LeCun's website)[http://yann.lecun.com/exdb/mnist/] and process it on your own. We just think this is the simplest approach here, since this notebook is not about how to process MNIST images.<jupyter_code>from keras.datasets import mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()

# Reshape the data and cast as float
X_train = X_train.reshape(60000, 784).astype('float32')
X_test = X_test.reshape(10000, 784).astype('float32')

# Normalize the data so that each pixel value is in the range [0,1]
X_train /= 255
X_test /= 255

# One-hot encode the labels
from keras.utils import to_categorical
y_train = to_categorical(y_train,10)
y_test = to_categorical(y_test,10)<jupyter_output>Using TensorFlow backend.<jupyter_text>Set neural network hyperparameters (tidier at top of file!)<jupyter_code>lr = 0.1
epochs = 20
train_batch_size = 128
weight_initializer = tf.contrib.layers.xavier_initializer()<jupyter_output><empty_output><jupyter_text>Set number of neurons for each layer<jupyter_code>n_input = 784
n_dense_1 = 64
n_dense_2 = 64
n_classes = 10<jupyter_output><empty_output><jupyter_text>Define placeholders Tensors for inputs and labels<jupyter_code>x_placeholder = tf.placeholder(tf.float32, [None, n_input])
y_placeholder = tf.placeholder(tf.int32, [None, n_classes])
batch_size = tf.placeholder(tf.int64)<jupyter_output><empty_output><jupyter_text>Create TensorFlow data sets<jupyter_code>mnist_data = tf.data.Dataset.from_tensor_slices((x_placeholder, y_placeholder)).repeat().batch(batch_size)<jupyter_output><empty_output><jupyter_text>Initialize iterator for data<jupyter_code>iterator = mnist_data.make_initializable_iterator()
x, y = iterator.get_next()<jupyter_output><empty_output><jupyter_text>Define types of layers<jupyter_code># dense layer with ReLU activation:
def dense(x, W, b):
    z = tf.add(tf.matmul(x, W), b)
    a = tf.nn.relu(z)
    return a<jupyter_output><empty_output><jupyter_text>Design neural network architecture<jupyter_code>def network(x, weights, biases):
    
    # two dense hidden layers: 
    dense_1 = dense(x, weights['W1'], biases['b1'])
    dense_2 = dense(dense_1, weights['W2'], biases['b2'])
    
    # linear output layer (softmax)
    out_layer_z = tf.add(tf.matmul(dense_2, weights['W_out']), biases['b_out'])
    
    return out_layer_z<jupyter_output><empty_output><jupyter_text>Define dictionaries for storing weights and biases for each layer -- and initialize<jupyter_code>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)
}<jupyter_output><empty_output><jupyter_text>Build model<jupyter_code>predictions = network(x, weights=weight_dict, biases=bias_dict)<jupyter_output><empty_output><jupyter_text>Define model's loss and its optimizer<jupyter_code>cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=predictions, labels=y))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=lr).minimize(cost)<jupyter_output><empty_output><jupyter_text>Define evaluation metrics<jupyter_code># 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<jupyter_output><empty_output><jupyter_text>Create op for variable initialization<jupyter_code>initializer_op = tf.global_variables_initializer()<jupyter_output><empty_output><jupyter_text>Train the network in a session<jupyter_code>with tf.Session() as session:
    # Initialize the session variables
    session.run(initializer_op)
    # Initialize the dataset iterator
    session.run(iterator.initializer, feed_dict={x_placeholder: X_train,
                                                 y_placeholder: y_train,
                                                 batch_size: train_batch_size})
    
    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
        n_batches = int(round(X_train.shape[0] / train_batch_size))
        
        # Loop over batches
        for batch in range(n_batches):
            # feed batch data to run optimization and fetch cost and accuracy: 
            _, batch_cost, batch_acc = session.run([optimizer, cost, accuracy_pct])
            
            # 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")
    
    # Re-initialize the dataset iterator with the test data
    session.run(iterator.initializer, feed_dict={x_placeholder: X_test,
                                                 y_placeholder: y_test,
                                                 batch_size: X_test.shape[0]})
    
    # Run the session using the test data and fetch the cost and accuracy
    _, test_cost, test_accuracy_pct = session.run([optimizer, cost, accuracy_pct])
    
    # Print the final results
    print("Test Cost:", '{:.3f}'.format(test_cost))
    print("Test Accuracy: ", '{:.2f}'.format(test_accuracy_pct), "%", sep='')<jupyter_output>Training for 20 epochs.
Epoch 001: cost = 0.477, accuracy = 86.41%
Epoch 002: cost = 0.237, accuracy = 93.05%
Epoch 003: cost = 0.181, accuracy = 94.62%
Epoch 004: cost = 0.147, accuracy = 95.69%
Epoch 005: cost = 0.124, accuracy = 96.38%
Epoch 006: cost = 0.108, accuracy = 96.89%
Epoch 007: cost = 0.096, accuracy = 97.21%
Epoch 008: cost = 0.086, accuracy = 97.54%
Epoch 009: cost = 0.077, accuracy = 97.78%
Epoch 010: cost = 0.071, accuracy = 97.96%
Epoch 011: cost = 0.065, accuracy = 98.13%
Epoch 012: cost = 0.059, accuracy = 98.31%
Epoch 013: cost = 0.055, accuracy = 98.48%
Epoch 014: cost = 0.051, accuracy = 98.61%
Epoch 015: cost = 0.047, accuracy = 98.71%
Epoch 016: cost = 0.043, accuracy = 98.84%
Epoch 017: cost = 0.040, accuracy = 98.96%
Epoch 018: cost = 0.037, accuracy = 99.03%
Epoch 019: cost = 0.034, accuracy = 99.11%
Epoch 020: cost = 0.032, accuracy = 99.21%
Training Complete. Testing Model.

Test Cost: 0.088
Test Accuracy: 97.31%
{'fraction_non_alphanumeric': 0.11242378048780488, 'fraction_numerical': 0.04217479674796748, 'mean_word_length': 4.227755644090306, 'pattern_counts': {'":': 1, '<': 60, '<?xml version=': 0, '>': 60, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '68798', 'license': 'MIT', 'n_tokens_mistral': 2909, 'n_tokens_neox': 2618, 'n_words': 640, 'path': 'notebooks/Deep Learning with TensorFlow/2.6_intermediate_net_in_tensorflow.ipynb', 'repo_name': 'grantbey/TensorFlow-LiveLessons', 'size': '7880'}