ANN with MNIST
Table of Contents
From Wikipedia
More here
We will be using MNIST to create a Multinomial Classifier that can detect if the MNIST image shown is a member of class 0,1,2,3,4,5,6,7,8 or 9. Susinctly, we're teaching a computer to recognize hand written digets.
# Import Library
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
Let's load the dataset.
mnist_train_images = np.load('./data_files/mnist_train_images.npy')
mnist_train_labels = np.load('./data_files/mnist_train_labels.npy')
mnist_test_images = np.load('./data_files/mnist_test_images.npy')
mnist_test_labels = np.load('./data_files/mnist_test_labels.npy')
print ("The training data set is:\n")
print (mnist_train_images.shape)
print (mnist_train_labels.shape)
print ("The test data set is:")
print (mnist_test_images.shape)
print (mnist_test_labels.shape)
Display a few random samples from it:
mnist_train_images[5]
# well, that's not a picture (or image), it's an array.
mnist_train_images[5].shape
You might think the training set is made up of 28 $\times$28 grayscale images of handwritten digits. No !!!
The thing is, the iamge has been flattened. These are 28x28 images that have been flattened into a 1D array. Let's reshape one.
img = np.reshape(mnist_train_images[5], [28,28])
img = mnist_train_images[5].reshape([28,28])
# So now we have a 28x28 matrix, where each element is an intensity level from 0 to 1.
img.shape
Let's visualize what some of these images and their corresponding training labels look like.
plt.figure(figsize = (6,6))
plt.imshow(img, 'gray')
plt.xticks([])
plt.yticks([])
plt.show()
mnist_train_labels[5]
np.argmax(mnist_train_labels[5])
# Import Library
import numpy as np
import matplotlib.pyplot as plt
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score
Only use (1, 5, 6) digits to visualize latent space in 2-D
Download data files
train_x = np.load('./data_files/mnist_train_images.npy')
train_y = np.load('./data_files/mnist_train_labels.npy')
test_x = np.load('./data_files/mnist_test_images.npy')
test_y = np.load('./data_files/mnist_test_labels.npy')
img = train_x[1,:].reshape(28,28)
plt.figure(figsize=(6,6))
plt.imshow(img,'gray')
plt.title("Label : {}".format(np.argmax(train_y[1,:])))
plt.xticks([])
plt.yticks([])
plt.show()
One hot encoding
print ('Train labels : {}'.format(train_y[1, :]))
clf = MLPClassifier(hidden_layer_sizes=(100,), activation = 'relu', solver='adam',
learning_rate_init = 0.0001, batch_size = 50, max_iter = 20, verbose = True)
clf.fit(train_x, train_y)
pred = clf.predict(test_x)
print("Accuracy : {}%".format(accuracy_score(test_y, pred)*100))
logits = clf.predict_proba(test_x[:1])
predict = clf.predict(test_x[:1])
plt.figure(figsize = (6,6))
plt.imshow(test_x[:1].reshape(28,28), 'gray')
plt.xticks([])
plt.yticks([])
plt.show()
print('Prediction : {}'.format(np.argmax(predict)))
np.set_printoptions(precision = 2, suppress = True)
print('Probability : {}'.format(logits.ravel()))
%%javascript
$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')