<jupyter_start><jupyter_text>Import required libraries<jupyter_code>import numpy as np
from tensorflow import keras
from tensorflow.keras import layers
import matplotlib.pyplot as plt
import pandas as pd
(X_train, y_train), (X_test, y_test) = keras.datasets.mnist.load_data()
X_train.shape
X_train[0].shape<jupyter_output><empty_output><jupyter_text>Normalization<jupyter_code>x_train = X_train.astype("float32") / 255
x_test = X_test.astype("float32") / 255

# Make sure images have shape (28, 28, 1)
X_train = np.expand_dims(X_train, -1)
X_test = np.expand_dims(X_test, -1)
print("X_train shape:", X_train.shape)
print(X_train.shape[0], "train samples")
print(X_test.shape[0], "test samples")
input_shape = X_train[0].shape
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)
y_train<jupyter_output><empty_output><jupyter_text>Creating model<jupyter_code>model = keras.Sequential(
    [
        keras.Input(shape=input_shape),
        layers.Conv2D(32, kernel_size=(3, 3), activation="relu"),
        layers.MaxPooling2D(pool_size=(2, 2)),
        layers.Conv2D(64, kernel_size=(3, 3), activation="relu"),
        layers.MaxPooling2D(pool_size=(2, 2)),
        layers.Flatten(),
        layers.Dropout(0.5),
        layers.Dense(10, activation="softmax"),
    ]
)

model.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_2 (Conv2D)            (None, 26, 26, 32)        320       
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 13, 13, 32)        0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 11, 11, 64)        18496     
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 5, 5, 64)          0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 1600)              0         
_________________________________________________________________
dropout_1 (Dropout)          (None, 1600)              0         
________________________________________________________[...]<jupyter_text>Compiling & training<jupyter_code>batch_size = 128 # how many training data are trained 
epochs = 50 # training iteration

early_stopping = keras.callbacks.EarlyStopping(patience=4)

model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])

history = model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.2, callbacks = early_stopping)<jupyter_output>Epoch 1/50
375/375 [==============================] - 2s 5ms/step - loss: 0.0453 - accuracy: 0.9860 - val_loss: 0.0303 - val_accuracy: 0.9914
Epoch 2/50
375/375 [==============================] - 2s 4ms/step - loss: 0.0378 - accuracy: 0.9876 - val_loss: 0.0362 - val_accuracy: 0.9898
Epoch 3/50
375/375 [==============================] - 2s 4ms/step - loss: 0.0394 - accuracy: 0.9875 - val_loss: 0.0366 - val_accuracy: 0.9908
Epoch 4/50
375/375 [==============================] - 2s 4ms/step - loss: 0.0359 - accuracy: 0.9883 - val_loss: 0.0389 - val_accuracy: 0.9909
Epoch 5/50
375/375 [==============================] - 2s 4ms/step - loss: 0.0354 - accuracy: 0.9887 - val_loss: 0.0400 - val_accuracy: 0.9902<jupyter_text>Evaluation<jupyter_code>df = pd.DataFrame(history.history)
df.plot()
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.16819407008086254, 'fraction_numerical': 0.07196765498652291, 'mean_word_length': 4.665648854961832, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '34868', 'license': 'Apache-2.0', 'n_tokens_mistral': 1362, 'n_tokens_neox': 1179, 'n_words': 232, 'path': 'Simple_MNIST_using_covnet.ipynb', 'repo_name': 'chunyip135/Collections-of-Applications-of-Deep-Learning', 'size': '3710'}