<jupyter_start><jupyter_text>1.Load MNIST Database<jupyter_code>import keras
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets(".", one_hot=True, reshape=False)<jupyter_output>Extracting .\train-images-idx3-ubyte.gz
Extracting .\train-labels-idx1-ubyte.gz
Extracting .\t10k-images-idx3-ubyte.gz
Extracting .\t10k-labels-idx1-ubyte.gz<jupyter_text>2.Define the model architecture<jupyter_code>mnist.validation.images.shape[1:]
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Activation, Dropout, AveragePooling2D
model = Sequential()
model.add(Conv2D(filters=16, kernel_size=2, padding='same', activation='relu', input_shape=(28,28,1)))
model.add(MaxPooling2D(pool_size=2, strides=2, padding='same'))
model.add(Dropout(0.1))
model.add(Conv2D(filters=32, kernel_size=2, padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=2, strides=2, padding='same'))
model.add(Dropout(0.1))
model.add(Conv2D(filters=64,kernel_size=2, padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=2, strides=2, padding='same'))
model.add(Dropout(0.1))
model.add(Flatten())
model.add(Dense(500, activation= 'relu'))
model.add(Dropout(0.1))
model.add(Dense(10, activation='softmax'))
model.summary()<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_17 (Conv2D)           (None, 28, 28, 16)        80        
_________________________________________________________________
max_pooling2d_16 (MaxPooling (None, 14, 14, 16)        0         
_________________________________________________________________
dropout_21 (Dropout)         (None, 14, 14, 16)        0         
_________________________________________________________________
conv2d_18 (Conv2D)           (None, 14, 14, 32)        2080      
_________________________________________________________________
max_pooling2d_17 (MaxPooling (None, 7, 7, 32)          0         
_________________________________________________________________
dropout_22 (Dropout)         (None, 7, 7, 32)          0         
_________________________________________________________________
conv2d_19 [...]<jupyter_text>3.Compile the model<jupyter_code>model.compile(loss='categorical_crossentropy',optimizer='rmsprop',metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>4.Train the model<jupyter_code>from keras.callbacks import ModelCheckpoint
checkpoint = ModelCheckpoint(filepath = 'MNISTweightsCNN.best.hdf5', verbose = 1, save_best_only = True)
history = model.fit(mnist.train.images,mnist.train.labels, epochs=10, batch_size=100, verbose=1,callbacks=[checkpoint], validation_data=(mnist.validation.images,mnist.validation.labels), shuffle=True)<jupyter_output>Train on 55000 samples, validate on 5000 samples
Epoch 1/10
55000/55000 [==============================] - 64s 1ms/step - loss: 0.2995 - acc: 0.9024 - val_loss: 0.0790 - val_acc: 0.9738

Epoch 00001: val_loss improved from inf to 0.07898, saving model to MNISTweightsCNN.best.hdf5
Epoch 2/10
55000/55000 [==============================] - 63s 1ms/step - loss: 0.0911 - acc: 0.9709 - val_loss: 0.0564 - val_acc: 0.9818

Epoch 00002: val_loss improved from 0.07898 to 0.05644, saving model to MNISTweightsCNN.best.hdf5
Epoch 3/10
55000/55000 [==============================] - 60s 1ms/step - loss: 0.0646 - acc: 0.9797 - val_loss: 0.0512 - val_acc: 0.9832

Epoch 00003: val_loss improved from 0.05644 to 0.05123, saving model to MNISTweightsCNN.best.hdf5
Epoch 4/10
55000/55000 [==============================] - 57s 1ms/step - loss: 0.0526 - acc: 0.9834 - val_loss: 0.0390 - val_acc: 0.9892

Epoch 00004: val_loss improved from 0.05123 to 0.03904, saving model to MNISTweightsCNN.best.hdf5
Epoch 5/10
[...]<jupyter_text>5.Load the model weights that yielded best validation accuracy<jupyter_code>model.load_weights('MNISTweightsCNN.best.hdf5')<jupyter_output><empty_output><jupyter_text>6.Evaluate model classification accuracy on test set<jupyter_code>score= model.evaluate(mnist.test.images,mnist.test.labels,verbose=0)
print(model.metrics_names)
print(score)
accuracy=100*score[1]
print('Test accuracy:',accuracy)<jupyter_output>Test accuracy: 98.99
{'fraction_non_alphanumeric': 0.15469613259668508, 'fraction_numerical': 0.08287292817679558, 'mean_word_length': 5.896825396825397, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '515754', 'license': 'MIT', 'n_tokens_mistral': 1655, 'n_tokens_neox': 1395, 'n_words': 272, 'path': 'Deep learning project/MNIST - Object Recognition in Images CNN.ipynb', 'repo_name': 'deep-hacker/cifar10', 'size': '4344'}