<jupyter_start><jupyter_text>Deep Neural Network for MNIST ClassificationThe dataset is called MNIST and refers to handwritten digit recognition.The dataset provides 70,000 images (28x28 pixels) of handwritten digits (1 digit per image). The goal is to write an algorithm that detects which digit is written. Since there are only 10 digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9), this is a classification problem with 10 classes. The goal would be to build a neural network with 2 or 3 hidden layers. and test the model using the test dataset.   Import the relevant packages<jupyter_code>import numpy as np
import tensorflow as tf
import tensorflow_datasets as tfds<jupyter_output><empty_output><jupyter_text>DataThat's where we load and preprocess our data.<jupyter_code>mnist_dataset, mnist_info = tfds.load(name='mnist', with_info=True, as_supervised=True)
mnist_dataset
mnist_train , mnist_test = mnist_dataset['train'] , mnist_dataset['test']
train_size = sum(1 for _ in mnist_train)
test_size = sum(1 for _ in mnist_test)

print('The size of the train dataset is: ', train_size)
print('The size of the test dataset is: ', test_size)
# I want to have a validation sample
# which is 10% of the training dataset
minst_validation = 0.1 * mnist_info.splits['train'].num_examples
# to make sure the validations are integers:
num_minst_validation = tf.cast(minst_validation, tf.int64)
#I am going to make sure the same for the test sample
num_test_samples = mnist_info.splits['test'].num_examples
num_test_samples = tf.cast(num_test_samples, tf.int64)
"""
here I want to scale the inputs
each image has between 0 to 255 shades of gery
"""
def scale(image,label):
    image = tf.cast(image, tf.float32)
    image = image /255.
    return image , label

scaled_train_validation = mnist_train.map(scale)
scaled_test = mnist_test.map(scale)
#I want to shuffle the data 
BUFFER_SIZE = 5000 # 5000 samples at a time

shuffled_train_validation = scaled_train_validation.shuffle(BUFFER_SIZE)

validation_data = shuffled_train_validation.take(num_minst_validation)
train_data = shuffled_tran_validation.skip(num_minst_validation)
#Batching
BATCH_SIZE = 100
train_data = train_data.batch(BATCH_SIZE)
validation_data = validation_data.batch(num_minst_validation) # actually it is only one batch cuz we do not need to batch validation and test data
test_data = scaled_test.batch(num_test_samples)
validation_inputs , validation_targets = next(iter(validation_data))<jupyter_output><empty_output><jupyter_text>MODEL<jupyter_code>input_size = 784 # 28 * 28 * 1
output_size = 10  # 0,1,...,9
hidden_layer_size = 200

model = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape = (28,28,1)), #the input layer
    tf.keras.layers.Dense(hidden_layer_size, activation = 'relu'), #the first hidden layer
    tf.keras.layers.Dense(hidden_layer_size, activation = 'relu'), #the second hidden layer
    tf.keras.layers.Dense(hidden_layer_size, activation = 'relu'), #the third hidden layer
    tf.keras.layers.Dense(output_size, activation = 'softmax') # the output is probabilities
    
])<jupyter_output><empty_output><jupyter_text>Choosing the optimizer & loss function<jupyter_code>#as the result would be in one_hot encoding format I used sparse_categorical_crossentropy

model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy',metrics = ['accuracy'])<jupyter_output><empty_output><jupyter_text>Training the Model<jupyter_code>NUM_EPOCHS = 10

model.fit(train_data, epochs = NUM_EPOCHS, validation_data = (validation_inputs, validation_targets), verbose = 2)<jupyter_output>Epoch 1/10
540/540 - 8s - loss: 0.2675 - accuracy: 0.9204 - val_loss: 0.1308 - val_accuracy: 0.9598
Epoch 2/10
540/540 - 7s - loss: 0.0987 - accuracy: 0.9692 - val_loss: 0.0924 - val_accuracy: 0.9747
Epoch 3/10
540/540 - 7s - loss: 0.0664 - accuracy: 0.9790 - val_loss: 0.0757 - val_accuracy: 0.9780
Epoch 4/10
540/540 - 7s - loss: 0.0484 - accuracy: 0.9847 - val_loss: 0.0684 - val_accuracy: 0.9792
Epoch 5/10
540/540 - 7s - loss: 0.0397 - accuracy: 0.9873 - val_loss: 0.0658 - val_accuracy: 0.9808
Epoch 6/10
540/540 - 7s - loss: 0.0327 - accuracy: 0.9894 - val_loss: 0.0564 - val_accuracy: 0.9820
Epoch 7/10
540/540 - 7s - loss: 0.0267 - accuracy: 0.9914 - val_loss: 0.0696 - val_accuracy: 0.9798
Epoch 8/10
540/540 - 7s - loss: 0.0238 - accuracy: 0.9922 - val_loss: 0.0560 - val_accuracy: 0.9850
Epoch 9/10
540/540 - 7s - loss: 0.0227 - accuracy: 0.9925 - val_loss: 0.0394 - val_accuracy: 0.9888
Epoch 10/10
540/540 - 7s - loss: 0.0225 - accuracy: 0.9924 - val_loss: 0.0414 - val_accuracy: 0.9875[...]<jupyter_text>Testing the model<jupyter_code>test_loss , test_accuracy = model.evaluate(test_data)<jupyter_output>1/1 [==============================] - 1s 934ms/step - loss: 0.0820 - accuracy: 0.9786
{'fraction_non_alphanumeric': 0.10813637314369379, 'fraction_numerical': 0.08303702154361012, 'mean_word_length': 4.9037037037037035, 'pattern_counts': {'":': 0, '<': 23, '<?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': '566992', 'license': 'MIT', 'n_tokens_mistral': 1970, 'n_tokens_neox': 1675, 'n_words': 475, 'path': 'TensorFlow on MNIST.ipynb', 'repo_name': 'MSadriAghdam/Neural-Network-on-MNIST-dataset', 'size': '4781'}