Untitled0.ipynb - Colaboratory
Untitled0.ipynb - Colaboratory
ipynb - Colaboratory
# Print the shape of the loaded data to verify it's loaded correctly
print("Training data shape:", x_train.shape)
print("Training labels shape:", y_train.shape)
print("Test data shape:", x_test.shape)
print("Test labels shape:", y_test.shape)
output
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
https://colab.research.google.com/drive/1uOafKc41edpKxQwJ0yxQxNUXZlNDJUD5#scrollTo=oGBZADsWh3_i&printMode=true 1/5
9/30/23, 11:13 AM Untitled0.ipynb - Colaboratory
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 26, 26, 32) 320
=================================================================
Total params: 225034 (879.04 KB)
Trainable params: 225034 (879.04 KB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
Epoch 1/5
750/750 [==============================] - 34s 43ms/step - loss: 0.1832 - accuracy: 0.9458 - val_loss: 0.0673 - val_accuracy: 0.979
Epoch 2/5
750/750 [==============================] - 27s 36ms/step - loss: 0.0555 - accuracy: 0.9822 - val_loss: 0.0469 - val_accuracy: 0.985
Epoch 3/5
750/750 [==============================] - 26s 35ms/step - loss: 0.0356 - accuracy: 0.9889 - val_loss: 0.0470 - val_accuracy: 0.986
Epoch 4/5
750/750 [==============================] - 27s 36ms/step - loss: 0.0274 - accuracy: 0.9914 - val_loss: 0.0439 - val_accuracy: 0.987
Epoch 5/5
750/750 [==============================] - 26s 35ms/step - loss: 0.0211 - accuracy: 0.9930 - val_loss: 0.0396 - val_accuracy: 0.989
313/313 [==============================] - 2s 6ms/step - loss: 0.0279 - accuracy: 0.9900
Test accuracy: 0.9900000095367432
import numpy as np
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
from tensorflow.keras.utils import to_categorical
https://colab.research.google.com/drive/1uOafKc41edpKxQwJ0yxQxNUXZlNDJUD5#scrollTo=oGBZADsWh3_i&printMode=true 2/5
9/30/23, 11:13 AM Untitled0.ipynb - Colaboratory
# Define a new model with the same input as the original model
feature_extraction_model = Model(inputs=model.input, outputs=model.layers[4].output) # Choose the appropriate layer for feature extractio
Epoch 1/5
375/375 [==============================] - 28s 72ms/step - loss: 0.2438 - accuracy: 0.9306 - val_loss: 0.0879 - val_accuracy: 0.972
Epoch 2/5
375/375 [==============================] - 24s 63ms/step - loss: 0.0643 - accuracy: 0.9805 - val_loss: 0.0529 - val_accuracy: 0.983
Epoch 3/5
375/375 [==============================] - 25s 67ms/step - loss: 0.0435 - accuracy: 0.9866 - val_loss: 0.0469 - val_accuracy: 0.986
Epoch 4/5
375/375 [==============================] - 26s 68ms/step - loss: 0.0346 - accuracy: 0.9892 - val_loss: 0.0431 - val_accuracy: 0.986
Epoch 5/5
375/375 [==============================] - 25s 67ms/step - loss: 0.0263 - accuracy: 0.9919 - val_loss: 0.0505 - val_accuracy: 0.985
1875/1875 [==============================] - 11s 6ms/step
313/313 [==============================] - 2s 6ms/step
Shape of CNN features (train): (60000, 1600)
Shape of CNN features (test): (10000, 1600)
# Assuming cnn_features_train and cnn_features_test are extracted features from the CNN model
# Calculate accuracy
svm_accuracy = accuracy_score(np.argmax(y_test, axis=1), svm_predictions)
print("SVM Classifier Accuracy:", svm_accuracy)
# Confusion matrix
svm_conf_matrix = confusion_matrix(np.argmax(y_test, axis=1), svm_predictions)
print("Confusion Matrix for SVM Classifier:")
print(svm_conf_matrix)
https://colab.research.google.com/drive/1uOafKc41edpKxQwJ0yxQxNUXZlNDJUD5#scrollTo=oGBZADsWh3_i&printMode=true 3/5
9/30/23, 11:13 AM Untitled0.ipynb - Colaboratory
[ 0 4 1 0 0 0 0 1023 0 0]
[ 3 0 2 0 0 0 0 1 965 3]
[ 0 1 0 1 8 5 0 2 0 992]]
313/313 [==============================] - 4s 13ms/step - loss: 0.0414 - accuracy: 0.9859
CNN Model Accuracy on Test Data: 0.9858999848365784
import numpy as np
from sklearn.metrics import confusion_matrix
# Assuming model is the trained CNN model and x_test, y_test are the test data
cnn_predictions = model.predict(x_test) # Get raw predictions
cnn_classes = np.argmax(cnn_predictions, axis=1) # Get predicted classes
# Assuming svm_conf_matrix and cnn_conf_matrix are the confusion matrices calculated previously
https://colab.research.google.com/drive/1uOafKc41edpKxQwJ0yxQxNUXZlNDJUD5#scrollTo=oGBZADsWh3_i&printMode=true 4/5
9/30/23, 11:13 AM Untitled0.ipynb - Colaboratory
https://colab.research.google.com/drive/1uOafKc41edpKxQwJ0yxQxNUXZlNDJUD5#scrollTo=oGBZADsWh3_i&printMode=true 5/5