<jupyter_start><jupyter_text>MNIST Implementaiton with ANN in Keras<jupyter_code># Load necessary libraries
import numpy as np
import matplotlib.pyplot as plt
from keras.layers import Dense, Flatten
from keras.models import Sequential
from keras.utils import to_categorical
from keras.datasets import mnist
# Load MNIST handwritten digit data
(X_train, y_train), (X_test, y_test) = mnist.load_data()
#Lets see how does one look like. We see that data is between zero & 255
print(X_train[0])
#Printing min & max
print("X_train.min",X_train.min(),"X_train.max",X_train.max())
print("X_test.min",X_test.min(),"X_test.max",X_test.max())
#Lets do a min max scaling to make it between 0 & 1. This will be good for us in training.
X_train=(X_train - X_train.min()) /(X_train.max() - X_train.min())
X_test=(X_test - X_test.min()) /(X_test.max() - X_test.min())
#Printing min & max
print("X_train.min",X_train.min(),"X_train.max",X_train.max())
print("X_test.min",X_test.min(),"X_test.max",X_test.max())
#Lets see how does one look like now. We see that data is between zero & 1
print(X_train[0])
# Display some images (28 X 28 pixels)
fig, axes = plt.subplots(ncols=20, sharex=False, sharey=True, figsize=(15, 4))
for i in range(20):
    axes[i].set_title(y_train[i])
    axes[i].imshow(X_train[i])
    axes[i].get_xaxis().set_visible(False)
    axes[i].get_yaxis().set_visible(False)
plt.show()
# Create simple Neural Network model
model = Sequential()
model.add(Flatten(input_shape=(28,28)))      # Input layer 784 neurons
model.add(Dense(16, activation='sigmoid'))   # 1st Hidden layer 16 neurons
model.add(Dense(16, activation='sigmoid'))   # 2nd Hidden layer 16 neurons
model.add(Dense(10, activation='softmax'))   # Output layer 10 neurons

model.summary()

model.compile(loss='categorical_crossentropy', optimizer='adam',metrics=['acc'])
#Lets see how is our input & output data shapes currently.
print ("Input ----> X_train.shape:",X_train.shape,"X_test.shape:",X_test.shape)
print ("Output ----> y_train.shape:",y_train.shape,"y_test.shape:",y_test.shape)
# Means we will need to reshape the input to (n,784) as our input layer is 784 nodes.
# And we have already mentiion to flatten (28 x28) in the model. So, that will take care of the input layer flattening.
# But we will need to reshape on the output to (n,10) as our output layer is of 10 nodes
# For this we will do the one hot encoding technique

#Lets see how one is looking like currenlty.
print("y_train[0]:",y_train[0])
print("y_train[1]:",y_train[1])
print("y_train[2]:",y_train[2])<jupyter_output>y_train[0]: 5
y_train[1]: 0
y_train[2]: 4<jupyter_text>If you use sparse_categorical_crossentropy loss function, you don't need to do this. Keras will internally take care of the conversion.<jupyter_code># Lets do a one hot encoding to convert it to a (n,10) shape
# Convert y_train into one-hot format 
y_train_hot = to_categorical(y_train, num_classes=10)
y_test_hot = to_categorical(y_test, num_classes=10)
#Lets see how one is looking like currenlty.
print("y_train_hot[0]:",y_train_hot[0])
print("y_train_hot[1]:",y_train_hot[1])
print("y_train_hot[2]:",y_train_hot[2])
#Lets see how is our input & output data shapes currently. Looks good now
print ("Input ----> X_train.shape:",X_train.shape,"X_test.shape:",X_test.shape)
print ("Output ----> y_train.shape:",y_train.shape,"y_test.shape:",y_test.shape)
print ("Output ----> y_train_hot.shape:",y_train_hot.shape,"y_test_hot.shape:",y_test_hot.shape)
# Train the Neural Network model
hist=model.fit(X_train, y_train_hot, epochs=10, validation_data=(X_test,y_test_hot))<jupyter_output>Epoch 1/10
1875/1875 [==============================] - 3s 2ms/step - loss: 1.1906 - acc: 0.7069 - val_loss: 0.5479 - val_acc: 0.8774
Epoch 2/10
1875/1875 [==============================] - 3s 1ms/step - loss: 0.4068 - acc: 0.8980 - val_loss: 0.3208 - val_acc: 0.9111
Epoch 3/10
1875/1875 [==============================] - 3s 1ms/step - loss: 0.2883 - acc: 0.9196 - val_loss: 0.2678 - val_acc: 0.9238
Epoch 4/10
1875/1875 [==============================] - 3s 1ms/step - loss: 0.2468 - acc: 0.9308 - val_loss: 0.2429 - val_acc: 0.9297
Epoch 5/10
1875/1875 [==============================] - 3s 1ms/step - loss: 0.2221 - acc: 0.9370 - val_loss: 0.2261 - val_acc: 0.9348
Epoch 6/10
1875/1875 [==============================] - 3s 1ms/step - loss: 0.2034 - acc: 0.9420 - val_loss: 0.2156 - val_acc: 0.9378
Epoch 7/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.1901 - acc: 0.9456 - val_loss: 0.2070 - val_acc: 0.9404
Epoch 8/10
1875/1875 [==============================] - 3s 2ms[...]<jupyter_text>Got 94.21% accuracy with in 10 epochs<jupyter_code># list all data in history
print(hist.history.keys())
# summarize history for accuracy
plt.plot(hist.history['acc'])
plt.plot(hist.history['val_acc'])
plt.title('Model Accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()
# Making predictions using our trained model
y_predicted = model.predict(X_test)
print(y_predicted[0])
#The rpedictions are in (n,10). Meaning to say we will need to take the occurance for which max value has come
y_predicted_labels = np.argmax(y_predicted, axis=1)
print(y_predicted_labels[0])
# Display some predictions on test data
fig, axes = plt.subplots(ncols=20, sharex=False, sharey=True, figsize=(15, 4))
for i in range(20):
    axes[i].set_title(y_predicted_labels[i])
    axes[i].imshow(X_test[i])
    axes[i].get_xaxis().set_visible(False)
    axes[i].get_yaxis().set_visible(False)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18253400143163923, 'fraction_numerical': 0.06120257695060845, 'mean_word_length': 5.329558323895809, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 16, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '658749', 'license': 'MIT', 'n_tokens_mistral': 2315, 'n_tokens_neox': 2047, 'n_words': 536, 'path': 'Hand written digits identification/code/Hand written digits identification - Concept.ipynb', 'repo_name': 'JohnsonFrancis/ML_Projects', 'size': '5588'}