<jupyter_start><jupyter_text>Import Libraries<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf

print(tf.__version__)<jupyter_output>2.3.0<jupyter_text>Loading Data<jupyter_code>mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()<jupyter_output><empty_output><jupyter_text>Checking Data<jupyter_code>np.set_printoptions(linewidth=200)

plt.imshow(x_train[0])
print(y_train[0])
print(x_train[0])<jupyter_output>5
[[  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   3  18  18  18 126 136 175  26 166 255 247 127   0   0   0   0]
 [  0   0   0   0   0   0   0   0  30  36  94 154 170 253 253 253 253 253 225 172 253 242 195  64   0   0   0   0]
 [  0   0   0   0   0   0   0  49 238 253 253 253 253 253 253 253 253 251  93  82  82  56  39   0   0   0   0   0]
 [  0   0   0   0   0   0   0  18 219 253 253 253 253 253 198 182 247 241   0 [...]<jupyter_text>Callback Function<jupyter_code>class myCallback(tf.keras.callbacks.Callback):
  def on_epoch_end(self, epoch, logs = {}):
    if(logs.get('accuracy') > 0.95):
      print('\naccuracy threshold reached')
      self.model.stop_training = True
                
callbacks = myCallback()<jupyter_output><empty_output><jupyter_text>Normalizing<jupyter_code>x_train = x_train / 255.0
x_test  = x_test  / 255.0<jupyter_output><empty_output><jupyter_text>Sequential creation of Neural Network Layer<jupyter_code>model = tf.keras.models.Sequential([
                                    tf.keras.layers.Flatten(),
                                    tf.keras.layers.Dense(128, activation = tf.nn.relu),
                                    tf.keras.layers.Dense(10, activation = tf.nn.softmax)
                                    ])<jupyter_output><empty_output><jupyter_text>Data Compilation<jupyter_code>model.compile(optimizer = 'adam',
              loss = 'sparse_categorical_crossentropy',
              metrics = ['accuracy']
              )<jupyter_output><empty_output><jupyter_text>Fitting the trained data into the model<jupyter_code>model.fit(x_train, y_train, epochs = 3, callbacks = [callbacks])<jupyter_output>Epoch 1/3
1875/1875 [==============================] - 4s 2ms/step - loss: 0.2563 - accuracy: 0.9272
Epoch 2/3
1858/1875 [============================>.] - ETA: 0s - loss: 0.1124 - accuracy: 0.9674
accuracy threshold reached
1875/1875 [==============================] - 3s 2ms/step - loss: 0.1121 - accuracy: 0.9674<jupyter_text>First predicted value<jupyter_code>predictions = model.predict(x_test)

print("real value :",y_test[0])
print("predicted value :",np.argmax(predictions[0]))<jupyter_output>real value : 7
predicted value : 7<jupyter_text>Checking the loss on the Test Data<jupyter_code>test_loss, test_accuracy = model.evaluate(x_test, y_test, batch_size=64)
print('Test loss: %.4f accuracy: %.4f' % (test_loss, test_accuracy))
from keras.utils import to_categorical

num_classes = 10;
test_labels_cat = to_categorical(y_test,num_classes)
test_labels_cat[0]<jupyter_output><empty_output><jupyter_text>Checking the first 20 Predictions and matching it with the Test Data<jupyter_code>first20_preds = np.argmax(predictions, axis=1)[:20]
first20_true = np.argmax(test_labels_cat,axis=1)[:20]
print(first20_preds)
print(first20_true)<jupyter_output>[7 2 1 0 4 1 4 9 6 9 0 6 9 0 1 5 9 7 3 4]
[7 2 1 0 4 1 4 9 5 9 0 6 9 0 1 5 9 7 3 4]<jupyter_text>Total Error / 10,000<jupyter_code>error = (np.argmax(predictions, axis=1) != np.argmax(test_labels_cat,axis=1)).sum()<jupyter_output><empty_output><jupyter_text>Error %<jupyter_code>error_percent = error/x_test.shape[0] * 100
print(error_percent)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11608194019307747, 'fraction_numerical': 0.1186720037673652, 'mean_word_length': 2.5223880597014925, 'pattern_counts': {'":': 0, '<': 48, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '354961', 'license': 'MIT', 'n_tokens_mistral': 1917, 'n_tokens_neox': 1660, 'n_words': 453, 'path': 'Basic/Basic Handwritten Digits Recognition MNIST.ipynb', 'repo_name': 'anishmo99/DL-Codes', 'size': '4247'}