<jupyter_start><jupyter_text><jupyter_code>#importing

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow import keras
#dataset
from keras.datasets import mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
train_images.shape
#train_images=train_images.reshape(train_images.shape[0],28,28,1)
train_images.shape
test_images.shape
train_images[0]
train_images=np.array(train_images)
train_images.shape
train_images[0].shape
train_images=train_images/255.
train_images[0]
test_images=test_images/255.
np.isnan(train_labels)<jupyter_output><empty_output><jupyter_text>Model  ANN model<jupyter_code>model=keras.Sequential([
                        keras.layers.Flatten(input_shape=(28,28)),
                        keras.layers.Dense(392, activation = 'relu'),
                        keras.layers.Dense(196, activation='relu'),
                        keras.layers.Dense(98, activation='relu'),
                        keras.layers.Dense(49, activation='relu'),
                        keras.layers.Dense(24, activation='relu'),
                        keras.layers.Dense(10, activation='relu'),                  
])
model.summary()
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=10)
test_loss, test_acc=model.evaluate(test_images, test_labels, verbose =1)
test_acc
model=keras.Sequential([
                        keras.layers.Flatten(input_shape=(28,28)),
                        keras.layers.Dense(392, activation = 'relu'),
                        keras.layers.Dense(10, activation='relu'),                  
])
model.summary()
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=10)
test_loss, test_acc=model.evaluate(test_images, test_labels, verbose =1)
test_acc<jupyter_output><empty_output><jupyter_text>CNN model<jupyter_code>from tensorflow.keras import datasets, layers, models
model = keras.Sequential()
model.add(layers.Conv2D(64, (3, 3), padding='same', activation='sigmoid', kernel_initializer='he_uniform', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Flatten())
model.add(layers.Dense(100, activation='sigmoid', kernel_initializer='he_uniform'))
model.add(layers.Dense(10, activation='softmax'))
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=10)
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose = 1)
test_acc
model = keras.Sequential()
model.add(layers.Conv2D(64, (3, 3), padding='same', activation='relu', kernel_initializer='he_uniform', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Flatten())
model.add(layers.Dense(100, activation='relu', kernel_initializer='he_uniform'))
model.add(layers.Dense(10, activation='softmax'))
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy',metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=10)
test_loss, test_acc=model.evaluate(test_images, test_labels, verbose=1)
test_acc<jupyter_output><empty_output><jupyter_text>Prediction<jupyter_code>import random
num=random.randint(0,9999)
plt.imshow(test_images[num])
plt.show()
pred=model.predict(test_images)
pred[num]
np.argmax(pred[num])
test_labels[num]<jupyter_output><empty_output><jupyter_text>Validating<jupyter_code>n=random.randint(0,59999)
result=model.predict(train_images)
result=np.argmax(result[n])
plt.imshow(train_images[n])
plt.title(train_labels[n])
plt.show()
print("Guess : ",result)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14093959731543623, 'fraction_numerical': 0.027114093959731543, 'mean_word_length': 5.824175824175824, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '359527', 'license': 'MIT', 'n_tokens_mistral': 1344, 'n_tokens_neox': 1254, 'n_words': 172, 'path': 'Handwritten_Digit_Recognition.ipynb', 'repo_name': 'AditiRastogi250701/Handwritten-Digit-Recognition', 'size': '3725'}