In [1]:
from keras.datasets import mnist
import matplotlib.pyplot as plt
# load (downloaded if needed) the MNIST dataset
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# plot 4 images as gray scale
plt.subplot(221)
plt.imshow(X_train[0], cmap=plt.get_cmap(‘gray’))
plt.subplot(222)
plt.imshow(X_train[1], cmap=plt.get_cmap(‘gray’))
plt.subplot(223)
plt.imshow(X_train[2], cmap=plt.get_cmap(‘gray’))
plt.subplot(224)
plt.imshow(X_train[3], cmap=plt.get_cmap(‘gray’))
# show the plot
plt.show()
C:\Users\rsadeghian\AppData\Local\Continuum\anaconda3\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
from ._conv import register_converters as _register_converters
Using TensorFlow backend.

In [2]:
# flatten 28*28 images to a 784 vector for each image
num_pixels = X_train.shape[1] * X_train.shape[2]
X_train = X_train.reshape(X_train.shape[0], num_pixels)
X_test = X_test.reshape(X_test.shape[0], num_pixels)
In [3]:
# normalize inputs from 0-255 to 0-1
X_train = X_train / 255
X_test = X_test / 255
In [4]:
from keras.utils import np_utils
# one hot encode outputs
Y_test = y_test
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
num_classes = y_test.shape[1]
In [5]:
from keras.models import Sequential
from keras.layers import Dense
from keras import optimizers
hidden_layer_nodes=42
def baseline_model():
# create model
model = Sequential()
#model.add(Dense(num_pixels, input_dim=num_pixels, kernel_initializer=’normal’, activation=’sigmoid’))
#model.add(Dense(hidden_layer_nodes, kernel_initializer=’normal’, activation=’sigmoid’))
#model.add(Dense(num_classes, kernel_initializer=’normal’, activation=’sigmoid’))
# Compile model
#sgd = optimizers.SGD(lr=0.0001, momentum=0.0, decay=0.0, nesterov=False)
#model.compile(loss=’categorical_crossentropy’, optimizer=’sgd’, metrics=[‘accuracy’])
model.add(Dense(input_dim=num_pixels,output_dim=50, init=’normal’, activation=’tanh’))
model.add(Dense(input_dim=50, output_dim=50, init=’normal’, activation=’tanh’))
model.add(Dense(input_dim=50, output_dim=num_classes, init=’normal’, activation=’softmax’))
sgd = optimizers.SGD(lr=0.1, decay=1e-7)
model.compile(loss=’categorical_crossentropy’, optimizer=sgd, metrics=[‘accuracy’])
return model
In [6]:
model = baseline_model()
# Fit the model
nn_simple = model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=25, batch_size=128, validation_split=.1)
# Final evaluation of the model
scores = model.evaluate(X_test, y_test)
print(“Baseline Error: %.2f%%” % (100-scores[1]*100))
print(“Baseline Accuracy: %.2f%%” % (scores[1]*100))
C:\Users\rsadeghian\AppData\Local\Continuum\anaconda3\lib\site-packages\ipykernel_launcher.py:14: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(input_dim=784, activation=”tanh”, units=50, kernel_initializer=”normal”)`
C:\Users\rsadeghian\AppData\Local\Continuum\anaconda3\lib\site-packages\ipykernel_launcher.py:15: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(input_dim=50, activation=”tanh”, units=50, kernel_initializer=”normal”)`
from ipykernel import kernelapp as app
C:\Users\rsadeghian\AppData\Local\Continuum\anaconda3\lib\site-packages\ipykernel_launcher.py:16: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(input_dim=50, activation=”softmax”, units=10, kernel_initializer=”normal”)`
app.launch_new_instance()
Train on 60000 samples, validate on 10000 samples
Epoch 1/25
60000/60000 [==============================] – 2s 34us/step – loss: 0.8290 – acc: 0.7819 – val_loss: 0.3582 – val_acc: 0.8960
Epoch 2/25
60000/60000 [==============================] – 2s 26us/step – loss: 0.3087 – acc: 0.9129 – val_loss: 0.2595 – val_acc: 0.9252
Epoch 3/25
60000/60000 [==============================] – 2s 29us/step – loss: 0.2375 – acc: 0.9310 – val_loss: 0.2118 – val_acc: 0.9382
Epoch 4/25
60000/60000 [==============================] – 2s 25us/step – loss: 0.1942 – acc: 0.9435 – val_loss: 0.1783 – val_acc: 0.9464
Epoch 5/25
60000/60000 [==============================] – 1s 25us/step – loss: 0.1648 – acc: 0.9526 – val_loss: 0.1646 – val_acc: 0.9479
Epoch 6/25
60000/60000 [==============================] – 1s 25us/step – loss: 0.1454 – acc: 0.9573 – val_loss: 0.1525 – val_acc: 0.9535
Epoch 7/25
60000/60000 [==============================] – 1s 25us/step – loss: 0.1296 – acc: 0.9618 – val_loss: 0.1367 – val_acc: 0.9584
Epoch 8/25
60000/60000 [==============================] – 2s 28us/step – loss: 0.1177 – acc: 0.9651 – val_loss: 0.1259 – val_acc: 0.9610
Epoch 9/25
60000/60000 [==============================] – 2s 27us/step – loss: 0.1076 – acc: 0.9682 – val_loss: 0.1232 – val_acc: 0.9610
Epoch 10/25
60000/60000 [==============================] – 2s 27us/step – loss: 0.0992 – acc: 0.9709 – val_loss: 0.1137 – val_acc: 0.9640
Epoch 11/25
60000/60000 [==============================] – 2s 27us/step – loss: 0.0914 – acc: 0.9735 – val_loss: 0.1096 – val_acc: 0.9655
Epoch 12/25
60000/60000 [==============================] – 2s 26us/step – loss: 0.0850 – acc: 0.9753 – val_loss: 0.1109 – val_acc: 0.9654
Epoch 13/25
60000/60000 [==============================] – 1s 25us/step – loss: 0.0796 – acc: 0.9767 – val_loss: 0.1073 – val_acc: 0.9662
Epoch 14/25
60000/60000 [==============================] – 2s 26us/step – loss: 0.0736 – acc: 0.9784 – val_loss: 0.1025 – val_acc: 0.9678
Epoch 15/25
60000/60000 [==============================] – 2s 25us/step – loss: 0.0697 – acc: 0.9794 – val_loss: 0.1036 – val_acc: 0.9687
Epoch 16/25
60000/60000 [==============================] – 2s 25us/step – loss: 0.0656 – acc: 0.9807 – val_loss: 0.0972 – val_acc: 0.9692
Epoch 17/25
60000/60000 [==============================] – 1s 25us/step – loss: 0.0615 – acc: 0.9820 – val_loss: 0.0973 – val_acc: 0.9697
Epoch 18/25
60000/60000 [==============================] – 2s 28us/step – loss: 0.0575 – acc: 0.9832 – val_loss: 0.1030 – val_acc: 0.9688
Epoch 19/25
60000/60000 [==============================] – 2s 29us/step – loss: 0.0544 – acc: 0.9844 – val_loss: 0.0949 – val_acc: 0.9698
Epoch 20/25
60000/60000 [==============================] – 2s 27us/step – loss: 0.0511 – acc: 0.9855 – val_loss: 0.1021 – val_acc: 0.9688
Epoch 21/25
60000/60000 [==============================] – 2s 31us/step – loss: 0.0486 – acc: 0.9862 – val_loss: 0.0917 – val_acc: 0.9723
Epoch 22/25
60000/60000 [==============================] – 2s 27us/step – loss: 0.0459 – acc: 0.9868 – val_loss: 0.0975 – val_acc: 0.9706
Epoch 23/25
60000/60000 [==============================] – 2s 26us/step – loss: 0.0435 – acc: 0.9880 – val_loss: 0.0929 – val_acc: 0.9718
Epoch 24/25
60000/60000 [==============================] – 2s 26us/step – loss: 0.0408 – acc: 0.9889 – val_loss: 0.0919 – val_acc: 0.9724
Epoch 25/25
60000/60000 [==============================] – 2s 26us/step – loss: 0.0390 – acc: 0.9899 – val_loss: 0.0921 – val_acc: 0.9726
10000/10000 [==============================] – 0s 27us/step
Baseline Error: 2.74%
Baseline Accuracy: 97.26%
In [7]:
plt.subplot(2,1,1)
plt.plot(nn_simple.history[‘acc’])
plt.plot(nn_simple.history[‘val_acc’])
plt.title(‘model accuracy’)
plt.ylabel(‘accuracy’)
plt.xlabel(‘epoch’)
plt.legend([‘train’, ‘test’], loc=’lower right’)
plt.subplot(2,1,2)
plt.plot(nn_simple.history[‘loss’])
plt.plot(nn_simple.history[‘val_loss’])
plt.title(‘model loss’)
plt.ylabel(‘loss’)
plt.xlabel(‘epoch’)
plt.legend([‘train’, ‘test’], loc=’upper right’)
plt.show()

In [8]:
import numpy as np
predicted_classes = model.predict_classes(X_test)
# see which we predicted correctly and which not
correct_indices = np.nonzero(predicted_classes == Y_test)[0]
incorrect_indices = np.nonzero(predicted_classes != Y_test)[0]
print()
print(len(correct_indices),” classified correctly”)
print(len(incorrect_indices),” classified incorrectly”)
# adapt figure size to accomodate 18 subplots
plt.rcParams[‘figure.figsize’] = (7,14)
figure_evaluation = plt.figure()
# plot 9 correct predictions
for i, correct in enumerate(correct_indices[:9]):
plt.subplot(6,3,i+1)
plt.imshow(X_test[correct].reshape(28,28), cmap=’gray’, interpolation=’none’)
plt.title(
“Predicted: {}, Truth: {}”.format(predicted_classes[correct],
Y_test[correct]))
plt.xticks([])
plt.yticks([])
# plot 9 incorrect predictions
for i, incorrect in enumerate(incorrect_indices[:9]):
plt.subplot(6,3,i+10)
plt.imshow(X_test[incorrect].reshape(28,28), cmap=’gray’, interpolation=’none’)
plt.title(
“Predicted {}, Truth: {}”.format(predicted_classes[incorrect],
Y_test[incorrect]))
plt.xticks([])
plt.yticks([])
figure_evaluation
9726 classified correctly
274 classified incorrectly
Out[8]: