<jupyter_start><jupyter_text>Training and Predicting Without scaling<jupyter_code># Without the hidden layer
model = keras.Sequential([
    keras.layers.Dense(10, input_shape = (784,), activation='sigmoid') 
])

model.compile(
    optimizer = 'adam',
    loss = 'sparse_categorical_crossentropy',
    metrics = ['accuracy']
)

model.fit(flatten_X_train, y_train, epochs=5)<jupyter_output>Train on 60000 samples
Epoch 1/5
60000/60000 [==============================] - 5s 90us/sample - loss: 2.2200 - accuracy: 0.4191
Epoch 2/5
60000/60000 [==============================] - 5s 79us/sample - loss: 1.5906 - accuracy: 0.4645
Epoch 3/5
60000/60000 [==============================] - 5s 79us/sample - loss: 1.5712 - accuracy: 0.4795
Epoch 4/5
60000/60000 [==============================] - 5s 79us/sample - loss: 1.5738 - accuracy: 0.4759
Epoch 5/5
60000/60000 [==============================] - 5s 81us/sample - loss: 1.5213 - accuracy: 0.4928<jupyter_text>Training and Predicting with scaling<jupyter_code># Scaling
X_train = X_train/255
X_test = X_test/255
# Flattening the shape of X_train and X_test
flatten_X_train = X_train.reshape(len(X_train),28*28)
flatten_X_test = X_test.reshape(len(X_test),28*28)
# Without the hidden layer
model_scaled = keras.Sequential([
    keras.layers.Dense(10, input_shape = (784,), activation='sigmoid') 
])

model_scaled.compile(
    optimizer = 'adam',
    loss = 'sparse_categorical_crossentropy',
    metrics = ['accuracy']
)

model_scaled.fit(flatten_X_train, y_train, epochs=5)<jupyter_output>Train on 60000 samples
Epoch 1/5
60000/60000 [==============================] - 5s 81us/sample - loss: 0.4849 - accuracy: 0.8791
Epoch 2/5
60000/60000 [==============================] - 5s 75us/sample - loss: 0.3063 - accuracy: 0.9159
Epoch 3/5
60000/60000 [==============================] - 4s 75us/sample - loss: 0.2856 - accuracy: 0.9211
Epoch 4/5
60000/60000 [==============================] - 4s 74us/sample - loss: 0.2748 - accuracy: 0.9247
Epoch 5/5
60000/60000 [==============================] - 4s 75us/sample - loss: 0.2678 - accuracy: 0.9259<jupyter_text>_*We can see that scaling increased the accuracy of the model*_<jupyter_code># Verifying the test prediction
plt.matshow(X_test[0])
y_predicted = model_scaled.predict(flatten_X_test)
y_predicted[0]
np.argmax(y_predicted[0])<jupyter_output><empty_output><jupyter_text>Building the confusion matrix<jupyter_code>y_predicted_labels = [np.argmax(i) for i in y_predicted]
y_predicted_labels[:5]
y_test[:5]
confusion_matrix = tf.math.confusion_matrix(labels=y_test, predictions=y_predicted_labels)
confusion_matrix
plt.figure(figsize = (10,7))
sns.heatmap(confusion_matrix, annot=True, fmt='d')
plt.xlabel("Predicted")
plt.ylabel("Truth")<jupyter_output><empty_output><jupyter_text>Adding the Hidden Layer<jupyter_code># With Hidden layer
model_scaled = keras.Sequential([
    keras.layers.Dense(100, input_shape = (784,), activation='relu'),
    keras.layers.Dense(10,activation='sigmoid')
])

model_scaled.compile(
    optimizer = 'adam',
    loss = 'sparse_categorical_crossentropy',
    metrics = ['accuracy']
)

model_scaled.fit(flatten_X_train, y_train, epochs=5)
y_predicted = model_scaled.predict(flatten_X_test)
y_predicted_labels = [np.argmax(i) for i in y_predicted]

confusion_matrix = tf.math.confusion_matrix(labels=y_test, predictions=y_predicted_labels)

plt.figure(figsize = (10,7))
sns.heatmap(confusion_matrix, annot=True, fmt='d')
plt.xlabel("Predicted")
plt.ylabel("Truth")<jupyter_output><empty_output><jupyter_text>Training and Predicting Using the inbuilt Flatten function<jupyter_code># Adding the flatten layer in Keras and also with the other Hidden layer
model_scaled = keras.Sequential([
    keras.layers.Flatten(input_shape=(28,28)),
    keras.layers.Dense(100, activation='relu'),
    keras.layers.Dense(10,activation='sigmoid')
])

model_scaled.compile(
    optimizer = 'adam',
    loss = 'sparse_categorical_crossentropy',
    metrics = ['accuracy']
)

model_scaled.fit(X_train, y_train, epochs=5)
y_predicted = model_scaled.predict(X_test)
y_predicted_labels = [np.argmax(i) for i in y_predicted]

confusion_matrix = tf.math.confusion_matrix(labels=y_test, predictions=y_predicted_labels)
plt.figure(figsize = (10,7))
sns.heatmap(confusion_matrix, annot=True, fmt='d')
plt.xlabel("Predicted")
plt.ylabel("Truth")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1902037079423209, 'fraction_numerical': 0.07301441977569238, 'mean_word_length': 6.211221122112211, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '40294', 'license': 'MIT', 'n_tokens_mistral': 1852, 'n_tokens_neox': 1596, 'n_words': 296, 'path': 'Projects/Hand_Written_Digits_Classification.ipynb', 'repo_name': 'KarthikKaiplody/Deep_Learning', 'size': '4369'}