This Python notebook shows you how to use deep learning to build a classification model that maps images of single digit numbers to their corresponding numeric representations.

This notebook is adapted from the deep learning tutorial that describes the important deep learning concepts and includes code snippets at http://www.pyimagesearch.com/2014/09/22/getting-started-deep-learning-python/.

If you haven't already installed it, run the following cell to install the `nolearn`

deep learning Python library in your notebook environment. Then, import the the libraries that you will be using for this notebook.

In [12]:

```
#!pip install --user nolearn
```

In [13]:

```
import warnings
warnings.filterwarnings("ignore")
from sklearn.cross_validation import train_test_split
from sklearn.metrics import classification_report
from sklearn import datasets
from nolearn.dbn import DBN
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
%matplotlib inline
```

MNIST is a database of handwritten numbers. Because it uses real-world data, it is an ideal database for training neural networks and machine learning tools that use pattern recognition.

Run the following cell to get the MNIST data set of images of handwritten numbers:

In [14]:

```
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/")
```

In [15]:

```
mnist_images = mnist.train.images
mnist_labels = mnist.train.labels
```

Then split the images into training and testing data sets:

In [16]:

```
trX, teX, trY, teY = train_test_split(mnist_images, mnist_labels.astype("int0"), test_size = 0.33)
print "Number of images for training:", trX.shape[0]
print "Number of images used for testing:", trY.shape[0]
pix = int(np.sqrt(teX.shape[1]))
print "Each image is:", pix, "by", pix, "pixels"
```

This notebook shows you how to train a pattern recognition model for handwritten numbers by using the Deep Belief Network (DBN) solver. The DBN solver is trained on approximately 47,000 images each with 784 pixels (28 by 28 before vectorizing) and uses 300 hidden units to support more efficient learning rates. The ten output units correspond to each of the single digit numbers (0 - 9). The results of the learning algorithms have an accuracy of 98 percent.

Run the following cell to train the DBN on handwritten digits. For details on the parameters used in the function, see http://www.pyimagesearch.com/2014/09/22/getting-started-deep-learning-python/.

In [17]:

```
dbn = DBN(
[trX.shape[1], 300, 10],
learn_rates = 0.3,
learn_rate_decays = 0.9,
epochs = 10,
verbose = 1) # set verbose to 0 for not printing output
dbn.fit(trX, trY)
```

Then evaluate the the quality of the predictions for each digit:

In [18]:

```
preds = dbn.predict(teX)
print classification_report(teY, preds)
```

Define a function that randomly selects a few of the test images and their predictions:

In [19]:

```
def randIm():
i = np.random.choice(np.arange(0, len(teY)), size = (1,))
pred = dbn.predict(np.atleast_2d(teX[i]))
image = (teX[i] * 255).reshape((28, 28)).astype("uint8")
# show the prediction
print "Actual digit is", teY[i][0], "predicted", pred[0]
imgplot = plt.imshow(image)
imgplot.set_cmap('gray')
randIm()
```

Run this function several times to see randomly picked examples from the test data set:

In [20]:

```
randIm()
```

In [21]:

```
randIm()
```

In this Python notebook, you learned how to access the MNIST database that contains sample handwritten numbers. You learned to train the DBN solver to recognize handwritten single digit numbers and evaluate the predicted numeric output.

**Jorge Castañón** is a data scientist at IBM Analytics, specializing in machine learning and text analytics using Apache Spark and Hadoop.

Some of the code used in this notebook was contributed by **Adrian Rosebrook**, entrepreneur and Chief PyImageSearcher at www.pyimagesearch.com.

Copyright © IBM Corp. 2016. This notebook and its source code are released under the terms of the MIT License.

In [ ]:

```
```