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

#LOAD DATA
mnist=tf.keras.datasets.mnist

#SPLITTING DATA
(x_train,y_train),(x_test,y_test)=mnist.load_data()<jupyter_output><empty_output><jupyter_text>CHECKING FOR IMAGE CHANNELS. IS ALPHA CHANNEL IS AT FIRST OR LAST?<jupyter_code>if tf.keras.backend.image_data_format() == 'channels_first':
    x_train = x_train.reshape(x_train.shape[0], 1, x_train.shape[1],x_train.shape[2])
    x_test = x_test.reshape(x_test.shape[0], 1, x_train.shape[1],x_train.shape[2])
    input_shape = (1, x_train.shape[1],x_train.shape[2])
else:
    x_train = x_train.reshape(x_train.shape[0], x_train.shape[1],x_train.shape[2], 1)
    x_test = x_test.reshape(x_test.shape[0], x_train.shape[1],x_train.shape[2], 1)
    input_shape = (x_train.shape[1],x_train.shape[2], 1)<jupyter_output><empty_output><jupyter_text>SHOWING FIRST 3 IMAGES FROM TRAINING SAMPLE<jupyter_code>for i in range(3):
    plt.imshow(x_train[i].reshape(28,28))
    plt.show()<jupyter_output><empty_output><jupyter_text>FORMATTING DATA AND ENCODING LABELS<jupyter_code>x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train=x_train/255
x_test=x_test/255
y_train=tf.keras.utils.to_categorical(y_train,10)
y_test=tf.keras.utils.to_categorical(y_test,10)<jupyter_output><empty_output><jupyter_text>CREATING A SEQUENTIAL MODELMODEL ARCHITECTURE<jupyter_code>model=tf.keras.models.Sequential()

#INPUT NODES
model.add(tf.keras.layers.Conv2D(32,(5,5),input_shape=input_shape,activation=tf.nn.relu))

#HIDDEN NODES
model.add(tf.keras.layers.MaxPooling2D(pool_size=(3, 3)))
model.add(tf.keras.layers.Conv2D(32,(2,2),activation=tf.nn.relu))
model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(128,activation=tf.nn.relu))

#OUTPUT NODES
model.add(tf.keras.layers.Dense(10,activation=tf.nn.softmax))<jupyter_output><empty_output><jupyter_text>COMPILATION<jupyter_code>model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>TRAINING<jupyter_code>model.fit(x_train,y_train,epochs=10)<jupyter_output>Epoch 1/10
60000/60000 [==============================] - 36s 606us/sample - loss: 0.2232 - accuracy: 0.9316
Epoch 2/10
60000/60000 [==============================] - 36s 607us/sample - loss: 0.0782 - accuracy: 0.9758
Epoch 3/10
60000/60000 [==============================] - 36s 602us/sample - loss: 0.0596 - accuracy: 0.9813
Epoch 4/10
60000/60000 [==============================] - 36s 604us/sample - loss: 0.0480 - accuracy: 0.9847
Epoch 5/10
60000/60000 [==============================] - 38s 626us/sample - loss: 0.0432 - accuracy: 0.9862
Epoch 6/10
60000/60000 [==============================] - 38s 639us/sample - loss: 0.0376 - accuracy: 0.9880
Epoch 7/10
60000/60000 [==============================] - 36s 597us/sample - loss: 0.0325 - accuracy: 0.9896
Epoch 8/10
60000/60000 [==============================] - 36s 607us/sample - loss: 0.0307 - accuracy: 0.9902
Epoch 9/10
60000/60000 [==============================] - 36s 605us/sample - loss: 0.0283 - accuracy: 0.9911
Epoch 10/10
60000/6[...]<jupyter_text>EVALUATE MODEL WITH TEST DATA<jupyter_code>val_loss,val_acc=model.evaluate(x_test,y_test)
print(val_loss)
print(val_acc)<jupyter_output>10000/10000 [==============================] - 2s 244us/sample - loss: 0.0213 - accuracy: 0.9939
0.021331036648963346
0.9939<jupyter_text>PREDICING OUTPUT FROM TEST DATA<jupyter_code>prediction=model.predict(x_test)
for i in range(10):
    plt.imshow(x_test[i].reshape(28,28))
    print(prediction[i])
    num=np.argmax(prediction[i])
    print(num)
    plt.show()<jupyter_output>[4.72098249e-10 3.07094927e-09 4.68298913e-06 2.16135962e-08
 1.17090392e-13 5.80817394e-09 9.23680495e-17 9.99995351e-01
 2.22822732e-10 1.45665595e-08]
7<jupyter_text>FOR DOWNLOADING MODEL FOR TENSOFLOW JS<jupyter_code># !pip3 install tensorflowjs 
# !mkdir model
# model.save('conv.h5')
# !ls
# !tensorflowjs_converter --input_format keras \conv.h5 \model
# !zip -r model.zip model 
# !ls -l
# from google.colab import files
# files.download('model.zip')<jupyter_output><empty_output><jupyter_text>FOR SAVING KERAS MODEL TO GOOGLE DRIVE<jupyter_code># !pip install -U -q PyDrive
# from pydrive.auth import GoogleAuth
# from pydrive.drive import GoogleDrive
# from google.colab import auth
# from oauth2client.client import GoogleCredentials

# # 1. Authenticate and create the PyDrive client.
# auth.authenticate_user()
# gauth = GoogleAuth()
# gauth.credentials = GoogleCredentials.get_application_default()
# drive = GoogleDrive(gauth)

# # 2. Save Keras Model or weights on google drive
# # create on Colab directory  
# model_str = drive.CreateFile({'title' : 'convDigitRecognition.json'})
# model_str.SetContentFile('convDigitRecognition.json')
# model_file = drive.CreateFile({'title' : 'convDigitRecognition.h5'})
# model_file.SetContentFile('convDigitRecognition.h5')
# model_file.Upload()
# model_str.Upload()

# # download to google drive
# drive.CreateFile({'m_id': model_file.get('id'),'j_id': model_str.get('id')})<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1926361738470298, 'fraction_numerical': 0.09527424558739798, 'mean_word_length': 6.4645892351274785, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '356629', 'license': 'MIT', 'n_tokens_mistral': 2432, 'n_tokens_neox': 2067, 'n_words': 315, 'path': 'scripts/Digit_Recognition_Convolutional.ipynb', 'repo_name': 'tarun-bisht/DigitRecognition', 'size': '5269'}