Express Deep Learning in Python - Examples

We will run a couple of examples to see how different parameters affect the performance of the classifier.


In [31]:
import numpy
import keras
import os

from keras import backend as K
from keras import losses, optimizers, regularizers
from keras.datasets import mnist
from keras.layers import Activation, ActivityRegularization, Conv2D, Dense, Dropout, Flatten, MaxPooling2D
from keras.models import Sequential
from keras.utils.np_utils import to_categorical

from keras.callbacks import TensorBoard

In [32]:
batch_size = 128
num_classes = 10
epochs = 10
TRAIN_EXAMPLES = 60000
TEST_EXAMPLES = 10000

# image dimensions
img_rows, img_cols = 28, 28

# load the data (already shuffled and splitted)
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# reshape the data to add the "channels" dimension
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)

# normalize the input in the range [0, 1]
# to make quick runs, select a smaller set of images.
train_mask = numpy.random.choice(x_train.shape[0], TRAIN_EXAMPLES, replace=False)
x_train = x_train[train_mask, :].astype('float32')
y_train = y_train[train_mask]
test_mask = numpy.random.choice(x_test.shape[0], TEST_EXAMPLES, replace=False)
x_test = x_test[test_mask, :].astype('float32')
y_test = y_test[test_mask]

x_train /= 255
x_test /= 255

print('Train samples: %d' % x_train.shape[0])
print('Test samples: %d' % x_test.shape[0])

# convert class vectors to binary class matrices
y_train = to_categorical(y_train, num_classes)
y_test = to_categorical(y_test, num_classes)


Train samples: 60000
Test samples: 10000

Convolutional 1


In [33]:
EXPERIMENT_COUNTER = 4

In [34]:
def write_summary(filename, model):
    with open(filename, 'w') as log_file:
        model.summary(print_fn=lambda x: log_file.write(x + '\n'))

In [48]:
def evaluate_model(model, experiment_name=EXPERIMENT_COUNTER):
    # train the model
    logs_dirname = './logs/experiment-{}'.format(experiment_name)
    tensorboard = TensorBoard(log_dir=logs_dirname, histogram_freq=0,
                          write_graph=False, write_images=False)
    epochs = 20
    history = model.fit(x_train, y_train,
                        batch_size=batch_size,
                        epochs=epochs,
                        verbose=1,
                        validation_data=(x_test, y_test),
                        callbacks=[tensorboard])
    # TIP: write the model summary to keep track of your experiments
    write_summary(os.path.join(logs_dirname, 'model-summary.txt'), model)

    # evaluate the model
    return model.evaluate(x_test, y_test, verbose=0)

In [10]:
# define the network architecture
model = Sequential()
model.add(Conv2D(filters=16,
                 kernel_size=(3, 3),
                 strides=(1,1),
                 padding='valid',
                 activation='relu',
                 input_shape=input_shape,
                 activity_regularizer='l2'))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))

# compile the model
model.compile(loss=losses.categorical_crossentropy,
              optimizer=optimizers.RMSprop(),
              metrics=['accuracy', 'mae'])

In [14]:
evaluate_model(model)
EXPERIMENT_COUNTER += 1


Train on 60000 samples, validate on 10000 samples
Epoch 1/20
60000/60000 [==============================] - 37s - loss: 6.2937 - acc: 0.8517 - mean_absolute_error: 0.0595 - val_loss: 0.2786 - val_acc: 0.9516 - val_mean_absolute_error: 0.0195
Epoch 2/20
60000/60000 [==============================] - 36s - loss: 0.2862 - acc: 0.9376 - mean_absolute_error: 0.0229 - val_loss: 0.1703 - val_acc: 0.9662 - val_mean_absolute_error: 0.0129
Epoch 3/20
60000/60000 [==============================] - 36s - loss: 0.2088 - acc: 0.9526 - mean_absolute_error: 0.0165 - val_loss: 0.1470 - val_acc: 0.9701 - val_mean_absolute_error: 0.0095
Epoch 4/20
60000/60000 [==============================] - 36s - loss: 0.1781 - acc: 0.9600 - mean_absolute_error: 0.0139 - val_loss: 0.1284 - val_acc: 0.9719 - val_mean_absolute_error: 0.0090
Epoch 5/20
60000/60000 [==============================] - 36s - loss: 0.1593 - acc: 0.9628 - mean_absolute_error: 0.0124 - val_loss: 0.1049 - val_acc: 0.9771 - val_mean_absolute_error: 0.0067
Epoch 6/20
60000/60000 [==============================] - 37s - loss: 0.1437 - acc: 0.9662 - mean_absolute_error: 0.0110 - val_loss: 0.0913 - val_acc: 0.9793 - val_mean_absolute_error: 0.0063
Epoch 7/20
60000/60000 [==============================] - 36s - loss: 0.1364 - acc: 0.9685 - mean_absolute_error: 0.0104 - val_loss: 0.1006 - val_acc: 0.9795 - val_mean_absolute_error: 0.0058
Epoch 8/20
60000/60000 [==============================] - 36s - loss: 0.1298 - acc: 0.9692 - mean_absolute_error: 0.0099 - val_loss: 0.0967 - val_acc: 0.9785 - val_mean_absolute_error: 0.0057
Epoch 9/20
60000/60000 [==============================] - 36s - loss: 0.1235 - acc: 0.9718 - mean_absolute_error: 0.0092 - val_loss: 0.0908 - val_acc: 0.9781 - val_mean_absolute_error: 0.0061
Epoch 10/20
60000/60000 [==============================] - 36s - loss: 0.1164 - acc: 0.9730 - mean_absolute_error: 0.0087 - val_loss: 0.0747 - val_acc: 0.9827 - val_mean_absolute_error: 0.0053
Epoch 11/20
60000/60000 [==============================] - 36s - loss: 0.1152 - acc: 0.9740 - mean_absolute_error: 0.0085 - val_loss: 0.0807 - val_acc: 0.9800 - val_mean_absolute_error: 0.0065
Epoch 12/20
60000/60000 [==============================] - 36s - loss: 0.1143 - acc: 0.9740 - mean_absolute_error: 0.0082 - val_loss: 0.0790 - val_acc: 0.9829 - val_mean_absolute_error: 0.0045
Epoch 13/20
60000/60000 [==============================] - 36s - loss: 0.1116 - acc: 0.9740 - mean_absolute_error: 0.0081 - val_loss: 0.0762 - val_acc: 0.9823 - val_mean_absolute_error: 0.0048
Epoch 14/20
60000/60000 [==============================] - 36s - loss: 0.1094 - acc: 0.9752 - mean_absolute_error: 0.0078 - val_loss: 0.0804 - val_acc: 0.9821 - val_mean_absolute_error: 0.0045
Epoch 15/20
60000/60000 [==============================] - 36s - loss: 0.1084 - acc: 0.9753 - mean_absolute_error: 0.0078 - val_loss: 0.0794 - val_acc: 0.9804 - val_mean_absolute_error: 0.0080
Epoch 16/20
60000/60000 [==============================] - 36s - loss: 0.1074 - acc: 0.9757 - mean_absolute_error: 0.0077 - val_loss: 0.0775 - val_acc: 0.9816 - val_mean_absolute_error: 0.0046
Epoch 17/20
60000/60000 [==============================] - 36s - loss: 0.1076 - acc: 0.9760 - mean_absolute_error: 0.0075 - val_loss: 0.0820 - val_acc: 0.9786 - val_mean_absolute_error: 0.0075
Epoch 18/20
60000/60000 [==============================] - 36s - loss: 0.1069 - acc: 0.9768 - mean_absolute_error: 0.0075 - val_loss: 0.0734 - val_acc: 0.9822 - val_mean_absolute_error: 0.0049
Epoch 19/20
60000/60000 [==============================] - 36s - loss: 0.1056 - acc: 0.9770 - mean_absolute_error: 0.0074 - val_loss: 0.0678 - val_acc: 0.9838 - val_mean_absolute_error: 0.0050
Epoch 20/20
60000/60000 [==============================] - 36s - loss: 0.1058 - acc: 0.9765 - mean_absolute_error: 0.0074 - val_loss: 0.0613 - val_acc: 0.9847 - val_mean_absolute_error: 0.0044
Out[14]:
[0.04902356365174055, 0.98470000000000002, 0.0043716243533417579]

Convolutional 2


In [46]:
# define the network architecture
model = Sequential()
model.add(Conv2D(filters=16,
                 kernel_size=(3, 3),
                 strides=(1,1),
                 padding='valid',
                 activation='sigmoid',
                 input_shape=input_shape,
                 activity_regularizer='l2'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='sigmoid'))
model.add(Dropout(0.25))
model.add(Dense(num_classes, activation='softmax'))

# compile the model
model.compile(loss=losses.categorical_crossentropy,
              optimizer=optimizers.RMSprop(),
              metrics=['accuracy', 'mae'])

In [49]:
evaluate_model(model)
EXPERIMENT_COUNTER += 1


Train on 60000 samples, validate on 10000 samples
Epoch 1/20
60000/60000 [==============================] - 14s - loss: 2212.1909 - acc: 0.4919 - mean_absolute_error: 0.1331 - val_loss: 1478.8597 - val_acc: 0.8306 - val_mean_absolute_error: 0.0632
Epoch 2/20
60000/60000 [==============================] - 14s - loss: 1081.5625 - acc: 0.8475 - mean_absolute_error: 0.0536 - val_loss: 752.2486 - val_acc: 0.8850 - val_mean_absolute_error: 0.0405
Epoch 3/20
60000/60000 [==============================] - 14s - loss: 538.8397 - acc: 0.8803 - mean_absolute_error: 0.0415 - val_loss: 363.0824 - val_acc: 0.8976 - val_mean_absolute_error: 0.0368
Epoch 4/20
60000/60000 [==============================] - 14s - loss: 253.5612 - acc: 0.8886 - mean_absolute_error: 0.0391 - val_loss: 165.6639 - val_acc: 0.9091 - val_mean_absolute_error: 0.0329
Epoch 5/20
60000/60000 [==============================] - 14s - loss: 113.4703 - acc: 0.8901 - mean_absolute_error: 0.0391 - val_loss: 72.4159 - val_acc: 0.9119 - val_mean_absolute_error: 0.0336
Epoch 6/20
60000/60000 [==============================] - 14s - loss: 48.9976 - acc: 0.8842 - mean_absolute_error: 0.0413 - val_loss: 30.7873 - val_acc: 0.9109 - val_mean_absolute_error: 0.0365
Epoch 7/20
60000/60000 [==============================] - 14s - loss: 20.7492 - acc: 0.8756 - mean_absolute_error: 0.0446 - val_loss: 12.9325 - val_acc: 0.9073 - val_mean_absolute_error: 0.0385
Epoch 8/20
60000/60000 [==============================] - 14s - loss: 8.8104 - acc: 0.8624 - mean_absolute_error: 0.0496 - val_loss: 5.4994 - val_acc: 0.9019 - val_mean_absolute_error: 0.0425
Epoch 9/20
60000/60000 [==============================] - 14s - loss: 3.9018 - acc: 0.8456 - mean_absolute_error: 0.0552 - val_loss: 2.4772 - val_acc: 0.8959 - val_mean_absolute_error: 0.0479
Epoch 10/20
60000/60000 [==============================] - 14s - loss: 1.9496 - acc: 0.8218 - mean_absolute_error: 0.0625 - val_loss: 1.2842 - val_acc: 0.8795 - val_mean_absolute_error: 0.0538
Epoch 11/20
60000/60000 [==============================] - 14s - loss: 1.2220 - acc: 0.7908 - mean_absolute_error: 0.0710 - val_loss: 0.8427 - val_acc: 0.8816 - val_mean_absolute_error: 0.0601
Epoch 12/20
60000/60000 [==============================] - 14s - loss: 0.9848 - acc: 0.7848 - mean_absolute_error: 0.0727 - val_loss: 0.7302 - val_acc: 0.8807 - val_mean_absolute_error: 0.0557
Epoch 13/20
60000/60000 [==============================] - 14s - loss: 0.8805 - acc: 0.8015 - mean_absolute_error: 0.0666 - val_loss: 0.6688 - val_acc: 0.8825 - val_mean_absolute_error: 0.0517
Epoch 14/20
60000/60000 [==============================] - 14s - loss: 0.8166 - acc: 0.8137 - mean_absolute_error: 0.0625 - val_loss: 0.6291 - val_acc: 0.8801 - val_mean_absolute_error: 0.0487
Epoch 15/20
60000/60000 [==============================] - 14s - loss: 0.7728 - acc: 0.8190 - mean_absolute_error: 0.0595 - val_loss: 0.5991 - val_acc: 0.8850 - val_mean_absolute_error: 0.0467
Epoch 16/20
60000/60000 [==============================] - 14s - loss: 0.7329 - acc: 0.8277 - mean_absolute_error: 0.0568 - val_loss: 0.5733 - val_acc: 0.8871 - val_mean_absolute_error: 0.0451
Epoch 17/20
60000/60000 [==============================] - 14s - loss: 0.7106 - acc: 0.8287 - mean_absolute_error: 0.0554 - val_loss: 0.5565 - val_acc: 0.8874 - val_mean_absolute_error: 0.0435
Epoch 18/20
60000/60000 [==============================] - 14s - loss: 0.6838 - acc: 0.8376 - mean_absolute_error: 0.0533 - val_loss: 0.5388 - val_acc: 0.8903 - val_mean_absolute_error: 0.0422
Epoch 19/20
60000/60000 [==============================] - 14s - loss: 0.6654 - acc: 0.8397 - mean_absolute_error: 0.0520 - val_loss: 0.5240 - val_acc: 0.8916 - val_mean_absolute_error: 0.0412
Epoch 20/20
60000/60000 [==============================] - 14s - loss: 0.6516 - acc: 0.8424 - mean_absolute_error: 0.0511 - val_loss: 0.5121 - val_acc: 0.8944 - val_mean_absolute_error: 0.0406

In [ ]: