text_plain_output_1.png
from tensorflow import keras

import matplotlib.pyplot as plt

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import seaborn as sns

import tensorflow as tf

train_imgs = pd.read_csv('../input/train.csv')
test_imgs = pd.read_csv('../input/test.csv')

label_train = train_imgs['label']

label_train = keras.utils.to_categorical(label_train, num_classes=10)

def CNN(n_conv):
    """
    Build a Convolutional neural network for n_conv number of convolutional layers.
    n_conv: Integer.
    """
    model = keras.Sequential()
    for _ in range(n_conv):
        model.add(keras.layers.Conv2D(64, kernel_size=2, activation=tf.nn.relu, input_shape=(28, 28, 1)))
        model.add(keras.layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
        model.add(keras.layers.Dropout(0.2))
    model.add(keras.layers.Flatten())
    model.add(keras.layers.Dense(256, kernel_regularizer=keras.regularizers.l2(0.001), activation=tf.nn.relu))
    model.add(keras.layers.Dense(10, activation=tf.nn.softmax))
    return model

model = CNN(n_conv=3)
model.compile(optimizer=keras.optimizers.Adam(), loss=keras.losses.categorical_crossentropy, metrics=['accuracy'])

visible = keras.layers.Input(shape=(28, 28, 1))
conv1 = keras.layers.Conv2D(32, kernel_size=4, activation='relu')(visible)
pool1 = keras.layers.MaxPooling2D(pool_size=(2, 2))(conv1)
flat1 = keras.layers.Flatten()(pool1)
conv2 = keras.layers.Conv2D(32, kernel_size=4, activation='relu')(visible)
pool2 = keras.layers.MaxPooling2D(pool_size=(2, 2))(conv2)
flat2 = keras.layers.Flatten()(pool2)
merge = keras.layers.concatenate([flat1, flat2])
hidden1 = keras.layers.Dense(256, activation='relu')(merge)
drop = keras.layers.Dropout(0.5)(hidden1)
output = keras.layers.Dense(10, activation='softmax')(drop)
model = keras.Model(inputs=visible, outputs=output)
model.compile(optimizer=keras.optimizers.Adam(), loss=keras.losses.categorical_crossentropy, metrics=['accuracy'])

earlystop = keras.callbacks.EarlyStopping(monitor='val_acc', min_delta=0.001, patience=7, mode='min')
reduce_lr = keras.callbacks.ReduceLROnPlateau(monitor='val_acc', factor=0.2, patience=5, min_lr=0.001)

history = model.fit(train_x, train_y, epochs=20, batch_size=200, validation_data=(test_x, test_y), verbose=1, callbacks=[reduce_lr])

train_accuracy = history.history['acc']
validation_accuracy = history.history['val_acc']
train_loss = history.history['loss']
validation_loss = history.history['val_loss']
epoch_range = range(1, len(train_accuracy) + 1)
fig, ax = plt.subplots(1, 2, figsize=(12, 5))
ax[0].set_title('Accuracy per Epoch')
sns.lineplot(x=epoch_range, y=train_accuracy, marker='o', ax=ax[0])
sns.lineplot(x=epoch_range, y=validation_accuracy, marker='o', ax=ax[0])
ax[0].legend(['training', 'validation'])
ax[0].set_xlabel('Epoch')
ax[0].set_ylabel('Accuracy')
ax[1].set_title('Loss per Epoch')
sns.lineplot(x=epoch_range, y=train_loss, marker='o', ax=ax[1])
sns.lineplot(x=epoch_range, y=validation_loss, marker='o', ax=ax[1])
ax[1].legend(['training', 'validation'])
ax[1].set_xlabel('Epoch')
ax[1].set_ylabel('Loss')
plt.show()