<jupyter_start><jupyter_text>MNIST Handwritten Digit Classification Model<jupyter_code>import sys

if not sys.warnoptions:
    import warnings
    warnings.simplefilter("ignore")

import numpy as np
import matplotlib.pyplot as plt

from hyperopt import Trials, STATUS_OK, tpe

from tensorflow import keras
from keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Input, Dense, Dropout, Activation, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D, BatchNormalization
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
from keras.utils import np_utils
from hyperas import optim
from hyperas.distributions import choice, uniform


def _set_seed(seed):
    np.random.seed(seed)  # for reproducibility
    
_set_seed(1337)<jupyter_output>Using TensorFlow backend.<jupyter_text>Load data<jupyter_code>def load_data(preview=True, verbose=True):
    """
    Loads and preps. MNIST dataset
    """
    nb_classes = 10
    
    # input image dimensions
    img_rows, img_cols = 28, 28
    
    # the data, shuffled and split between train and test sets
    (X_train, y_train), (X_test, y_test) = mnist.load_data()
    
    if verbose:
        print('----------------- Shape - BEFORE -----------------')
        print('Train: X={}, y={}'.format(X_train.shape, y_train.shape))
        print('Test: X={}, y={}'.format(X_test.shape, y_test.shape))
        
    if preview:
        for i in range(9):
            # Define subplot
            plt.subplot(330 + 1 + i)
            # Plot raw pixel data
            plt.imshow(X_train[i], cmap='gray')
        plt.show()
    
    # Re-shape, and convert to float
    X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 1).astype('float32')
    X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 1).astype('float32')
    
    # Normalize data
    X_train /= 255
    X_test /= 255
    
    # One-hot encoding
    y_train = np_utils.to_categorical(y_train, nb_classes)
    y_test = np_utils.to_categorical(y_test, nb_classes)
    
    if verbose:
        print('----------------- Shape - AFTER -----------------')
        print('Train: X={}, y={}'.format(X_train.shape, y_train.shape))
        print('Test: X={}, y={}'.format(X_test.shape, y_test.shape))
    
    return X_train, y_train, X_test, y_test

X_train, y_train, X_test, y_test = load_data()<jupyter_output>----------------- Shape - BEFORE -----------------
Train: X=(60000, 28, 28), y=(60000,)
Test: X=(10000, 28, 28), y=(10000,)<jupyter_text>Train CNN Model<jupyter_code>def create_model():
    model = Sequential()
    model.add(Conv2D(32, (3, 3), input_shape=X_train.shape[1:]))
    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(y_train.shape[1], activation='softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    model.summary()
    return model

model = create_model()<jupyter_output>WARNING:tensorflow:From /home/asura/miniconda3/envs/mnist-flask-app/lib/python3.7/site-packages/tensorflow/python/ops/init_ops.py:1251: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.
Instructions for updating:
Call initializer instance with the dtype argument instead of passing it to the constructor
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 26, 26, 32)        320       
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 24, 24, 32)        9248      
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 12, 12, 32)        0         
_____________________________________________________________[...]<jupyter_text>Add Callbacks and Early Stopping to Save Weights<jupyter_code># Create `model` directory if it doesn't exist
import os
checkpoint_directory = 'model'
if not os.path.isdir(checkpoint_directory):
    os.makedirs(checkpoint_directory)
    print(f'New directory created at $:->{checkpoint_directory}')
# Model saving callback
file_name ="weights-improvement-{epoch:02d}-{loss:.4f}.hdf5"
checkpoint_filename = os.path.join(checkpoint_directory, file_name)
checkpointer = ModelCheckpoint(filepath=checkpoint_filename, monitor='val_acc', verbose=1, save_best_only=True)

# Early stopping
early_stopping = EarlyStopping(monitor='val_acc', verbose=1, patience=5)

# Train
batch_size = 128
epochs = 100
history = model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1,
          callbacks=[checkpointer, early_stopping], 
          validation_data=(X_test, y_test))<jupyter_output>Train on 60000 samples, validate on 10000 samples
Epoch 1/100
59904/60000 [============================>.] - ETA: 0s - loss: 0.2511 - acc: 0.9229
Epoch 00001: val_acc improved from -inf to 0.98100, saving model to model/weights-improvement-01-0.2510.hdf5
60000/60000 [==============================] - 23s 381us/sample - loss: 0.2510 - acc: 0.9230 - val_loss: 0.0579 - val_acc: 0.9810
Epoch 2/100
59776/60000 [============================>.] - ETA: 0s - loss: 0.0979 - acc: 0.9712
Epoch 00002: val_acc improved from 0.98100 to 0.98510, saving model to model/weights-improvement-02-0.0977.hdf5
60000/60000 [==============================] - 22s 366us/sample - loss: 0.0977 - acc: 0.9713 - val_loss: 0.0449 - val_acc: 0.9851
Epoch 3/100
59904/60000 [============================>.] - ETA: 0s - loss: 0.0759 - acc: 0.9774
Epoch 00003: val_acc did not improve from 0.98510
60000/60000 [==============================] - 21s 348us/sample - loss: 0.0758 - acc: 0.9774 - val_loss: 0.0409 - val_acc: 0.9851
E[...]<jupyter_text>Evaluate Accuracy<jupyter_code>score = model.evaluate(X_test, y_test, verbose=0)
print('Test score:', score[0])
print('Test accuracy:', score[1])<jupyter_output>Test score: 0.034976938286282656
Test accuracy: 0.9905<jupyter_text>Serialize Model<jupyter_code># serialize model to JSON
model_json = model.to_json()
model_path = "model/model.json"
try:
    with open(model_path, "w") as json_file:
        json_file.write(model_json)
        print(f"Saved model at: {model_path}")
except Exception as e:
    print(f"Exception occured: {e}")<jupyter_output>Saved model at: model/model.json
{'fraction_non_alphanumeric': 0.16251126464403726, 'fraction_numerical': 0.06127966356263142, 'mean_word_length': 3.958302308265078, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 23, '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': '634033', 'license': 'MIT', 'n_tokens_mistral': 2479, 'n_tokens_neox': 2177, 'n_words': 504, 'path': 'notebooks/using_cnn_to_recognize_handwritten_digits_using_keras.ipynb', 'repo_name': 'hurshd0/mnist-flask-app', 'size': '6658'}