import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import pandas as pd
import keras
from keras.models import Sequential
from keras.layers import Dense, Flatten
from tensorflow import set_random_seed
from sklearn.model_selection import train_test_split
from keras.datasets import mnist
The MNIST dataset consists of 70,000, 28x28 black-and-white images of handwritten digits.
(X_train, y_train), (X_holdout, y_holdout) = mnist.load_data()
X_val, X_test, y_val, y_test = train_test_split(X_holdout, y_holdout, test_size = 0.5, random_state=1)
print(X_train.shape)
print(y_train.shape)
print(X_val.shape)
print(y_val.shape)
print(X_test.shape)
print(y_test.shape)
np.set_printoptions(linewidth=120)
mydigit = X_train[0]
print(mydigit)
np.set_printoptions(linewidth=75)
plt.imshow(mydigit, cmap=cm.binary)
plt.axis('off')
plt.show()
sel = np.random.choice(range(60000), 40, replace=False)
X_sel = X_train[sel]
y_sel = y_train[sel]
plt.close()
plt.rcParams["figure.figsize"] = [16,10]
for i in range(40):
plt.subplot(5,8,i+1)
plt.imshow(X_sel[i], cmap=cm.binary)
plt.text(-1, 10, s = str(int(y_sel[i])), fontsize=16, color='b')
plt.axis('off')
plt.show()
Xs_train = X_train / 255
Xs_val = X_val / 255
Xs_test = X_test / 255
%%time
np.random.seed(1)
set_random_seed(1)
model = Sequential()
model.add(Flatten(input_shape=(28,28)))
model.add(Dense(512, activation='relu'))
model.add(Dense(256, activation='relu'))
model.add(Dense(10, activation='softmax'))
opt = keras.optimizers.Adam(lr = 0.001)
model.compile(loss='sparse_categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
h = model.fit(Xs_train, y_train, batch_size=1024, epochs=20, validation_data=(Xs_val, y_val), verbose=2)
plt.rcParams["figure.figsize"] = [8,4]
plt.subplot(1,2,1)
plt.plot(h.history['acc'], label='Training')
plt.plot(h.history['val_acc'], label='Validation')
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend()
plt.subplot(1,2,2)
plt.plot(h.history['loss'], label='Training')
plt.plot(h.history['val_loss'], label='Validation')
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend()
plt.show()
score = model.evaluate(X_test, y_test, verbose=0)
print('Testing loss: ', score[0])
print('Testing accuracy:', score[1])
from sklearn.metrics import confusion_matrix, classification_report
y_pred = model.predict_classes(X_test)
confmat = confusion_matrix(y_test, y_pred)
df = pd.DataFrame(confmat)
df
print(classification_report(y_test, y_pred))
import math
# Find misclassified samples in the test set.
sel = y_pred != y_test
n_mc = np.sum(sel) # number misclassified
X_mc = X_test[sel,:]
y_mc = y_test[sel]
yp_mc = y_pred[sel]
idx = np.argsort(y_mc)
X_mc = X_mc[idx,:]
y_mc = y_mc[idx]
yp_mc = yp_mc[idx]
rows = math.ceil(n_mc / 6)
plt.figure(figsize=(12,30))
for i in range(0, n_mc):
plt.subplot(rows,6,i+1)
plt.imshow(X_mc[i], cmap=cm.binary)
plt.text(-1, 10, s = str(int(y_mc[i])), fontsize=16, color='b')
plt.text(-1, 16, s = str(int(yp_mc[i])), fontsize=16, color='r')
plt.axis('off')