CNN basic example – MNIST digit classification
In this section, we will do a complete example of implementing a CNN for digit classification using the MNIST dataset. We will build a simple model of two convolution layers and fully connected layers.
Let's start off by importing the libraries that will be needed for this implementation:
%matplotlibinlineimportmatplotlib.pyplotaspltimporttensorflowastfimportnumpyasnpfromsklearn.metricsimportconfusion_matriximportmath
Next, we will use TensorFlow helper functions to download and preprocess the MNIST dataset as follows:
from tensorflow.examples.tutorials.mnist import input_data
mnist_data = input_data.read_data_sets('data/MNIST/', one_hot=True)
Output: Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes. Extracting data/MNIST/train-images-idx3-ubyte.gz Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes. Extracting data/MNIST/train-labels-idx1-ubyte.gz Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes. Extracting...