Implementing a simple CNN
In this recipe, we will develop a four-layer convolutional neural network to improve upon our accuracy in predicting MNIST digits. The first two convolution layers will each be composed of convolution-ReLU-Max Pool operations, and the final two layers will be fully connected layers.
Getting ready
To access the MNIST data, TensorFlow has an examples.tutorials
package that has great dataset-loading functionalities. After we load the data, we will set up our model variables, create the model, train the model in batches, and then visualize loss, accuracy, and some sample digits.
How to do it...
Perform the following steps:
- First, we'll load the necessary libraries and start a graph session:
import matplotlib.pyplot as plt import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data from tensorflow.python.framework import ops ops.reset_default_graph() sess = tf.Session()
- Next, we will load the data and transform the images into 28x28...