<jupyter_start><jupyter_text><jupyter_code>import tensorflow as tf
import numpy as np
from tensorflow import keras
class myCallback(tf.keras.callbacks.Callback):
  def on_epoch_end(self, epoch, logs={}):
    if(logs.get('accuracy') >= 0.99):
      print("\nReached 99% accuracy so cancelling training!")
      self.model.stop_training = True
callbacks = myCallback()
mnist = tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()

import matplotlib.pyplot as plt
plt.imshow(x_train[2]) #To show image
#print(x_train[0])
print(y_train[2])
##To see the pixels and colors of the images and labels
x_train = x_train / 255.0
x_test = x_test / 255.0
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)])

model.compile(optimizer = tf.keras.optimizers.Adam(),
              loss = 'sparse_categorical_crossentropy',
              metrics = ['accuracy'])
model.fit(x_train, y_train, epochs = 10, callbacks = [callbacks])
model.evaluate(x_test, y_test)
classifications = model.predict(x_test)
plt.imshow(x_test[6]) #To show image
print(classifications[6])
print(y_test[6])<jupyter_output>[3.3832603e-12 2.7064018e-10 4.6898784e-08 6.5356849e-09 9.9987078e-01
 2.0895502e-07 2.1394226e-09 2.8383211e-05 9.3328403e-05 7.2237513e-06]
4
{'fraction_non_alphanumeric': 0.13319386331938632, 'fraction_numerical': 0.08856345885634588, 'mean_word_length': 3.783333333333333, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '215147', 'license': 'MIT', 'n_tokens_mistral': 619, 'n_tokens_neox': 537, 'n_words': 101, 'path': 'Handwritten_Number_Predictor.ipynb', 'repo_name': 'noorhaq/Machine-Learning-Projects', 'size': '1434'}