<jupyter_start><jupyter_text>Downloading all Dependencies<jupyter_code># Importing Tensorflow 2.0
%tensorflow_version 2.x
import tensorflow as tf

!pip install mitdeeplearning
import mitdeeplearning as mdl

import matplotlib.pyplot as plt
import numpy as np
import random
from tqdm import tqdm

# Checking the GPU
assert len(tf.config.list_physical_devices("GPU")) > 0<jupyter_output>Requirement already satisfied: mitdeeplearning in /usr/local/lib/python3.6/dist-packages (0.1.2)
Requirement already satisfied: regex in /usr/local/lib/python3.6/dist-packages (from mitdeeplearning) (2019.12.20)
Requirement already satisfied: gym in /usr/local/lib/python3.6/dist-packages (from mitdeeplearning) (0.17.2)
Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from mitdeeplearning) (1.18.5)
Requirement already satisfied: tqdm in /usr/local/lib/python3.6/dist-packages (from mitdeeplearning) (4.41.1)
Requirement already satisfied: pyglet<=1.5.0,>=1.4.0 in /usr/local/lib/python3.6/dist-packages (from gym->mitdeeplearning) (1.5.0)
Requirement already satisfied: cloudpickle<1.4.0,>=1.2.0 in /usr/local/lib/python3.6/dist-packages (from gym->mitdeeplearning) (1.3.0)
Requirement already satisfied: scipy in /usr/local/lib/python3.6/dist-packages (from gym->mitdeeplearning) (1.4.1)
Requirement already satisfied: future in /usr/local/lib/python3.6/dist-packag[...]<jupyter_text>MNIST Dataset<jupyter_code># Loading the MNIST dataset
mnist = tf.keras.datasets.mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
train_images = (np.expand_dims(train_images, axis=-1)/255.).astype(np.float32)
train_labels = (train_labels).astype(np.int64)
test_images = (np.expand_dims(test_images, axis=-1)/255.).astype(np.float32)
test_labels = (test_labels).astype(np.int64)
# Visualizing some of the Images and corresponding labels
plt.figure(figsize=(10, 10))
random_inds = np.random.choice(60000, 36)
for i in range(36):
  plt.subplot(6, 6, i+1)
  plt.xticks([])
  plt.yticks([])
  plt.grid(False)
  image_ind = random_inds[i]
  plt.imshow(np.squeeze(train_images[image_ind]), cmap=plt.cm.binary)
  plt.xlabel(train_labels[image_ind])<jupyter_output><empty_output><jupyter_text>Neural Network for Handwritten Digit Classification<jupyter_code>def build_model():
  model = tf.keras.Sequential([
                               tf.keras.layers.Flatten(),
                               tf.keras.layers.Dense(128, activation=tf.nn.relu),
                               tf.keras.layers.Dense(10, activation=tf.nn.softmax)
  ])
  return model

model = build_model()
model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.01),
              loss="sparse_categorical_crossentropy",
              metrics=['accuracy'])

# Defining Batch_size and number of epochs
BATCH_SIZE = 64
EPOCHS = 5

model.fit(train_images, train_labels, batch_size=BATCH_SIZE, epochs=EPOCHS)
# Evaluating the Accuracy on the Test data
test_loss, test_accuracy = model.evaluate(test_images, test_labels)
print("Test accuracy: ", test_accuracy)<jupyter_output>313/313 [==============================] - 1s 2ms/step - loss: 0.2767 - accuracy: 0.9258
Test accuracy:  0.9258000254631042<jupyter_text>Convolutional Neural Network for Handwritten Digit Classification<jupyter_code># Defining the CNN model
def build_cnn_model():
  cnn_model = tf.keras.Sequential([
                                   tf.keras.layers.Conv2D(filters=24, kernel_size=(3, 3), activation=tf.nn.relu),
                                   tf.keras.layers.MaxPool2D(pool_size=(2, 2)),
                                   tf.keras.layers.Conv2D(filters=36, kernel_size=(3, 3), activation=tf.nn.relu),
                                   tf.keras.layers.MaxPool2D(pool_size=(2, 2)),
                                   tf.keras.layers.Flatten(),
                                   tf.keras.layers.Dense(900, activation=tf.nn.relu),
                                   tf.keras.layers.Dense(128, activation=tf.nn.relu),
                                   tf.keras.layers.Dense(10, activation=tf.nn.softmax)

  ])
  return cnn_model

cnn_model = build_cnn_model()

# Initializing the model
cnn_model.predict(train_images[[0]])
# Summary of the model 
print(cnn_model.summary())<jupyter_output>Model: "sequential_10"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_12 (Conv2D)           multiple                  240       
_________________________________________________________________
max_pooling2d_12 (MaxPooling multiple                  0         
_________________________________________________________________
conv2d_13 (Conv2D)           multiple                  7812      
_________________________________________________________________
max_pooling2d_13 (MaxPooling multiple                  0         
_________________________________________________________________
flatten_10 (Flatten)         multiple                  0         
_________________________________________________________________
dense_26 (Dense)             multiple                  810900    
_____________________________________________________[...]<jupyter_text>Training and Testing the CNN model<jupyter_code># Compile
cnn_model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.01),
                  loss="sparse_categorical_crossentropy",
                  metrics=['accuracy'])
# Fitting the Training Data
cnn_model.fit(train_images, train_labels, batch_size=BATCH_SIZE, epochs=EPOCHS)
# Evaluating the accuracy
test_loss, test_accuracy = cnn_model.evaluate(test_images, test_labels)

print("Test accuracy: ", test_accuracy)<jupyter_output>313/313 [==============================] - 1s 3ms/step - loss: 0.0782 - accuracy: 0.9769
Test accuracy:  0.9768999814987183<jupyter_text>Making Predictions Using CNN Model<jupyter_code>predictions = cnn_model.predict(test_images)
predictions[4]
prediction = np.argmax(predictions[4])
print(prediction)<jupyter_output>4<jupyter_text>Visualizing<jupyter_code>print("Label of this digit is: ", test_labels[4])
plt.imshow(test_images[4,:,:,0], cmap=plt.cm.binary)<jupyter_output>Label of this digit is:  4<jupyter_text>Visualizing the Classification of MNIST Data<jupyter_code>image_index = 79
plt.subplot(1, 2, 1)
mdl.lab2.plot_image_prediction(image_index, predictions, test_labels, test_images)
plt.subplot(1, 2, 2)
mdl.lab2.plot_value_prediction(image_index, predictions, test_labels)
# Visualizing the several images
num_rows = 5
num_cols = 4
num_images = num_rows*num_cols
plt.figure(figsize=(2*2*num_cols, 2*num_rows))
for i in range(num_images):
  plt.subplot(num_rows, 2*num_cols, 2*i+1)
  mdl.lab2.plot_image_prediction(i, predictions, test_labels, test_images)
  plt.subplot(num_rows, 2*num_cols, 2*i+2)
  mdl.lab2.plot_value_prediction(i, predictions, test_labels)<jupyter_output><empty_output><jupyter_text>Optional: Model using Gradient Tape<jupyter_code>cnn_model = build_cnn_model()
batch_size=12
loss_history = mdl.util.LossHistory(smoothing_factor=0.95)
# Plotter
plotter = mdl.util.PeriodicPlotter(sec=2,
                                   xlabel="Iterations",
                                   ylabel="Loss",
                                   scale="semilogy")
optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)
# Clear
if hasattr(tqdm, '_instances'):tqdm._instances.clear()

for idx in tqdm(range(0, train_images.shape[0], batch_size)):
  (images, labels) = (train_images[idx:idx+batch_size], train_labels[idx:idx+batch_size])
  images = tf.convert_to_tensor(images, dtype=tf.float32)
  with tf.GradientTape() as tape:
    logits = cnn_model(images)
    loss_value = tf.keras.backend.sparse_categorical_crossentropy(labels, logits)
  
  loss_history.append(loss_value.numpy().mean())
  plotter.plot(loss_history.get())
  grads = tape.gradient(loss_value, cnn_model.trainable_variables)
  optimizer.apply_gradients(zip(grads, cnn_model.trainable_variables))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12184873949579832, 'fraction_numerical': 0.03546712802768166, 'mean_word_length': 4.0235878336437, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '577509', 'license': 'MIT', 'n_tokens_mistral': 2781, 'n_tokens_neox': 2591, 'n_words': 452, 'path': 'MNIST Digit Classification.ipynb', 'repo_name': 'ThinamXx/MNIST-CNN', 'size': '8092'}