<jupyter_start><jupyter_text><jupyter_code># Credits: https://github.com/keras-team/keras/blob/master/examples/mnist_cnn.py


from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K

batch_size = 128
num_classes = 10
epochs = 12

# input image dimensions
img_rows, img_cols = 28, 28

# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()

if K.image_data_format() == 'channels_first':
    x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
    x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
    input_shape = (1, img_rows, img_cols)
else:
    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)

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
#https://github.com/sunneysood/Tensorflow/blob/master/Multi_Layer_LSTM.ipynb
import matplotlib.pyplot as plt


def plot_graphs(history, string):
  plt.plot(history.history[string])
  plt.plot(history.history['val_'+string])
  plt.xlabel("Epochs")
  plt.ylabel(string)
  plt.legend([string, 'val_'+string])
  plt.show()<jupyter_output><empty_output><jupyter_text>CNN with 3*3 Kernel wth 3 conv layers with maxpooling , drop out and batch normalisation<jupyter_code>model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
                 activation='relu',
                 input_shape=input_shape))

model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(3, 3)))
model.add(Dropout(0.2))

model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(3, 3)))
model.add(Dropout(0.2))

model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(num_classes, activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])

model.summary()

model_run = model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          verbose=1,
          validation_data=(x_test, y_test))


plot_graphs(model_run, 'acc')
plot_graphs(model_run, 'loss')

score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
#model_run.history['val_loss']<jupyter_output><empty_output><jupyter_text>CNN with 5*5 Kernel wth 12 conv layers with maxpooling and drop out<jupyter_code>from keras.layers.normalization import BatchNormalization

model = Sequential()
model.add(Conv2D(32, kernel_size=(5, 5),
                 activation='relu',
                 input_shape=input_shape,padding='same'))
model.add(Conv2D(16, (5, 5), activation='relu',padding='same'))
#model.add(MaxPooling2D(pool_size=(5, 5),strides=(1,1),padding='same'))
model.add(Dropout(0.25))

model.add(Conv2D(32, (5, 5), activation='relu',padding='same'))
#model.add(MaxPooling2D(pool_size=(5, 5),padding='same'))
model.add(BatchNormalization())
model.add(Dropout(0.25))

#model.add(Conv2D(256, (5, 5), activation='relu',padding='same'))
#model.add(MaxPooling2D(pool_size=(5, 5)))
#model.add(Dropout(0.25))

#model.add(Conv2D(512, (5, 5), activation='relu',padding='same'))
#model.add(Dropout(0.25))
#model.add(BatchNormalization())

#model.add(Conv2D(512, (5, 5), activation='relu',padding='same'))
#model.add(MaxPooling2D(pool_size=(5, 5),padding='same'))

#model.add(Conv2D(512, (5, 5), activation='relu',padding='same'))
#model.add(MaxPooling2D(pool_size=(5, 5),padding='same'))

model.add(Conv2D(16, (5, 5), activation='relu',padding='same'))
model.add(MaxPooling2D(pool_size=(5, 5),padding='same'))
model.add(Dropout(0.25))

model.add(Conv2D(32, (5, 5), activation='relu',padding='same'))
#model.add(Conv2D(128, (5, 5), activation='relu',padding='same'))
model.add(BatchNormalization())
model.add(Dropout(0.25))
#model.add(Conv2D(64, (5, 5), activation='relu',padding='same'))
#model.add(Conv2D(64, (5, 5), activation='relu',padding='same'))

model.add(Flatten())
model.add(Dense(32, activation='relu'))
#model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])

model.summary()


model_run = model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          verbose=1,
          validation_data=(x_test, y_test))
plot_graphs(model_run, 'acc')
plot_graphs(model_run, 'loss')

score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])<jupyter_output><empty_output><jupyter_text>CNN with 9*9 Kernel wth 15 conv layers with maxpooling and drop out<jupyter_code>from keras.layers.normalization import BatchNormalization
model = Sequential()
model.add(Conv2D(32, kernel_size=(7, 7),
                 activation='relu',
                 input_shape=input_shape,padding='same'))
model.add(Conv2D(32, (7, 7), activation='relu',padding='same'))
#model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))
model.add(Dropout(0.25))

model.add(Conv2D(16, (7, 7), activation='relu',padding='same'))
#model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))
#model.add(Dropout(0.25))
model.add(BatchNormalization())

model.add(Conv2D(32, (7, 7), activation='relu',padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))
#model.add(Dropout(0.25))

model.add(Conv2D(32, (7, 7), activation='relu',padding='same'))
#model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))
model.add(Dropout(0.25))

model.add(Conv2D(64, (7, 7), activation='relu',padding='same'))
#model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))
model.add(Dropout(0.25))
model.add(BatchNormalization())

model.add(Conv2D(16, (7, 7), activation='relu',padding='same'))
#model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))
#model.add(Dropout(0.25))

#model.add(Conv2D(128, (9, 9), activation='relu',padding='same'))
#model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))
#model.add(Dropout(0.25))

#model.add(Conv2D(64, (9, 9), activation='relu',padding='same'))
#model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))
#model.add(Dropout(0.25))
#model.add(BatchNormalization())

#model.add(Conv2D(64, (9, 9), activation='relu',padding='same'))
#model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))
#model.add(Dropout(0.25))

model.add(Conv2D(32, (7, 7), activation='relu',padding='same'))
#model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))
model.add(BatchNormalization())
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(32, activation='relu'))
#model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))


model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])

model.summary()
model_run = model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          verbose=1,
          validation_data=(x_test, y_test))
plot_graphs(model_run, 'acc')
plot_graphs(model_run, 'loss')


score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17919222903885482, 'fraction_numerical': 0.040516359918200406, 'mean_word_length': 6.403027436140019, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '167474', 'license': 'Apache-2.0', 'n_tokens_mistral': 3119, 'n_tokens_neox': 2967, 'n_words': 451, 'path': 'suny_sn1_gmail_com_Assignment_15_CNN_MNIST.ipynb', 'repo_name': 'sunneysood/deeplearning', 'size': '7824'}