In [1]:
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 cv2


gnumpy: failed to import cudamat. Using npmat instead. No GPU will be used.

In [2]:
dataset = datasets.fetch_mldata("MNIST Original")

In [3]:
# scale the data to the range [0, 1] and then construct the training
# and testing splits
(trainX, testX, trainY, testY) = train_test_split(dataset.data / 255.0, dataset.target.astype("int0"), test_size = 0.33)

In [4]:
# train the Deep Belief Network with 784 input units (the flattened,
# 28x28 grayscale image), 300 hidden units, 10 output units (one for
# each possible output classification, which are the digits 1-10)
dbn = DBN(
    [trainX.shape[1], 300, 10],
    learn_rates = 0.3,
    learn_rate_decays = 0.9,
    epochs = 10,
    verbose = 1)
dbn.fit(trainX, trainY)


[DBN] fitting X.shape=(46900, 784)
[DBN] layers [784, 300, 10]
[DBN] Fine-tune...
100%
Epoch 1:
100%
  loss 0.281380938839
  err  0.0848702185792
  (0:00:07)
Epoch 2:
100%
  loss 0.17249368979
  err  0.0496712773224
  (0:00:07)
Epoch 3:
100%
  loss 0.125732793689
  err  0.0350281762295
  (0:00:07)
Epoch 4:
100%
  loss 0.0903794974387
  err  0.0261057035519
  (0:00:07)
Epoch 5:
100%
  loss 0.0708371902667
  err  0.0212389002732
  (0:00:07)
Epoch 6:
100%
  loss 0.0496749311719
  err  0.0148779030055
  (0:00:06)
Epoch 7:
100%
  loss 0.0370386198311
  err  0.0116547131148
  (0:00:06)
Epoch 8:
100%
  loss 0.0273948178272
  err  0.00817537568306
  (0:00:06)
Epoch 9:
100%
  loss 0.0206042011946
  err  0.00601946721311
  (0:00:07)
Epoch 10:
  loss 0.0173045642584
  err  0.00505891393443
  (0:00:07)

In [5]:
# compute the predictions for the test data and show a classification
# report
preds = dbn.predict(testX)
print classification_report(testY, preds)


             precision    recall  f1-score   support

          0       0.98      0.98      0.98      2265
          1       0.99      0.99      0.99      2588
          2       0.98      0.98      0.98      2312
          3       0.99      0.96      0.97      2345
          4       0.99      0.97      0.98      2286
          5       0.96      0.98      0.97      2073
          6       0.99      0.98      0.99      2261
          7       0.98      0.98      0.98      2384
          8       0.95      0.98      0.97      2260
          9       0.97      0.97      0.97      2326

avg / total       0.98      0.98      0.98     23100


In [ ]:
# randomly select a few of the test instances
for i in np.random.choice(np.arange(0, len(testY)), size = (10,)):
    # classify the digit
    pred = dbn.predict(np.atleast_2d(testX[i]))
 
    # reshape the feature vector to be a 28x28 pixel image, then change
    # the data type to be an unsigned 8-bit integer
    image = (testX[i] * 255).reshape((28, 28)).astype("uint8")
 
    # show the image and prediction
    print "Actual digit is {0}, predicted {1}".format(testY[i], pred[0])
#     cv2.imshow("Digit", image)
#     cv2.waitKey(0)