<jupyter_start><jupyter_text>CLASSIFY HANDWRITTEN DIGITS USING CONVOLUTIONAL NEURAL NETWORK<jupyter_code>#Description:This program uses convolutional neural network (CNN) to classify handwritten digits as 0 - 9
#Importing required libraries
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten , MaxPooling2D
from keras.datasets import mnist
from keras.utils import to_categorical
import matplotlib.pyplot as plt
import numpy as np


#Load the data and split it into train and test sets
(X_train,y_train), (X_test,y_test)=mnist.load_data()
print(X_train.shape)
print(X_test.shape)
#first image
X_train[0]

#print hte image label
y_train[0]

#show the image as picture 
plt.imshow(X_train[0])
plt.imshow(X_train[1])
#reshape the data to fit the model
X_train = X_train.reshape(60000,28,28,1)#depth of 1 could be grayscale
X_test = X_test.reshape(10000,28,28,1)
#one_hot_encoding
y_train_one_hot = to_categorical(y_train)
y_test_one_hot = to_categorical(y_test)

#print the new label
print(y_train_one_hot[0])
#Bulid a CNN model inspired by LeNet architecture
model=Sequential()
#add model layers
#layer1 combination
model.add(Conv2D(64,(3,3),input_shape=(28,28,1),padding='same',activation='relu'))
#model.add(Conv2D(64,(3,3),padding='same',activation='relu'))
#MaxPooling layer1
model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
#layer2 combination
model.add(Conv2D(32,(3,3),padding='same',activation='relu'))
#model.add(Conv2D(128,(3,3),padding='same',activation='relu'))
#model.add(Conv2D(128,(3,3),padding='same',activation='relu'))
#MaxPooling layer2
model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
model.add(Flatten())
#model.add(Dense(4096,activation='relu'))
#model.add(Dense(4096,activation='relu'))
model.add(Dense(10,activation='softmax'))

#Compile the model
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
history=model.fit(X_train,y_train_one_hot,validation_data=(X_test,y_test_one_hot),epochs=3)
#plot model accuracy
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('Model Accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epochs')
plt.legend(['Train','Val'],loc='upper left')
plt.show()
#prediction as probabilities for first 4 images in test set
predictions=model.predict(X_test[:4])
print(predictions)
#predictions as first 4 labels for test set
print(np.argmax(predictions,axis=1))
print(y_test[:4])
#print first fours images in test set
for i in range(5):
  image=X_test[i]
  image=np.array(image,dtype='float')
  pixels=image.reshape((28,28))
  plt.imshow(pixels,cmap='gray')
  plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14555765595463138, 'fraction_numerical': 0.03667296786389414, 'mean_word_length': 9.3359375, 'pattern_counts': {'":': 0, '<': 5, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '657335', 'license': 'MIT', 'n_tokens_mistral': 1042, 'n_tokens_neox': 978, 'n_words': 192, 'path': 'MNISTconv.ipynb', 'repo_name': 'NightmareNight-em/Handwritten-Digit-Recognition', 'size': '2645'}