In [1]:
from IPython.display import Image, SVG
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import keras
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense
from keras.utils.vis_utils import model_to_dot
In [2]:
# Loads the training and test data sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()
In [3]:
first_image = X_train[0, :, :]
In [4]:
# To interpret the values as a 28x28 image, we need to reshape
# the numpy array, which is one dimensional.
plt.imshow(first_image, cmap=plt.cm.Greys);
In [5]:
num_classes = len(np.unique(y_train))
num_classes
Out[5]:
In [6]:
# 60K training 28 x 28 (pixel) images
X_train.shape
Out[6]:
In [7]:
# 10K test 28 x 28 (pixel) images
X_test.shape
Out[7]:
In [8]:
input_dim = np.prod(X_train.shape[1:])
input_dim
Out[8]:
In [9]:
# The training and test data sets are integers, ranging from 0 to 255.
# We reshape the training and test data sets to be matrices with 784 (= 28 * 28) features.
X_train = X_train.reshape(60000, input_dim).astype('float32')
X_test = X_test.reshape(10000, input_dim).astype('float32')
In [10]:
# Scales the training and test data to range between 0 and 1.
max_value = X_train.max()
X_train /= max_value
X_test /= max_value
In [11]:
# The training and test labels are integers from 0 to 9 indicating the class label
(y_train, y_test)
Out[11]:
In [12]:
# We convert the class labels to binary class matrices
y_train = np_utils.to_categorical(y_train, num_classes)
y_test = np_utils.to_categorical(y_test, num_classes)
In [13]:
# All the logic to build a (multinomial) logistic regression classifier
model = Sequential()
model.add(Dense(num_classes, input_dim=input_dim, activation='softmax'))
In [14]:
model.summary()
In [15]:
SVG(model_to_dot(model).create(prog='dot', format='svg'))
Out[15]:
In [16]:
# Trains the model, iterating on the training data in batches of 32 in 10 epochs.
# The optimizer is stochastic gradient descent (SGD).
model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(X_train, y_train, batch_size=32, epochs=10, verbose=1)
Out[16]:
In [17]:
# Test accuracy is ~92%.
model.evaluate(X_test, y_test, verbose=False)
Out[17]:
In [18]:
first_test_image = X_test[0, :]
plt.imshow(first_test_image.reshape(28, 28), cmap=plt.cm.Greys);
In [19]:
second_test_image = X_test[1, :]
plt.imshow(second_test_image.reshape(28, 28), cmap=plt.cm.Greys);
In [20]:
model.predict_classes(X_test[[0, 1], :])
Out[20]:
In [ ]: