0% found this document useful (0 votes)
21 views

Untitled0.ipynb - Colaboratory

This document loads and preprocesses the MNIST handwritten digits dataset. It then builds and trains a convolutional neural network (CNN) model on the data, achieving 99% test accuracy. Feature vectors from a hidden layer of the CNN are extracted and used to train a support vector machine (SVM) classifier, which also achieves 99% accuracy, demonstrating that the CNN learned meaningful features.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Untitled0.ipynb - Colaboratory

This document loads and preprocesses the MNIST handwritten digits dataset. It then builds and trains a convolutional neural network (CNN) model on the data, achieving 99% test accuracy. Feature vectors from a hidden layer of the CNN are extracted and used to train a support vector machine (SVM) classifier, which also achieves 99% accuracy, demonstrating that the CNN learned meaningful features.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

9/30/23, 11:13 AM Untitled0.

ipynb - Colaboratory

from tensorflow.keras.datasets import mnist

# Load MNIST dataset


(x_train, y_train), (x_test, y_test) = mnist.load_data()

# 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)

Training data shape: (60000, 28, 28)


Training labels shape: (60000,)
Test data shape: (10000, 28, 28)
Test labels shape: (10000,)

from tensorflow.keras.datasets import mnist


import matplotlib.pyplot as plt

# Load MNIST dataset


(x_train, y_train), (x_test, y_test) = mnist.load_data()

# Display a few sample images


plt.figure(figsize=(12, 4))
for i in range(10):
plt.subplot(2, 5, i+1)
plt.imshow(x_train[i], cmap='gray')
plt.title(f"Label: {y_train[i]}")
plt.axis('off')
plt.show()

output

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense

# Load MNIST dataset


(x_train, y_train), (x_test, y_test) = mnist.load_data()

# Normalize the pixel values to be between 0 and 1


x_train = x_train.reshape((-1, 28, 28, 1)).astype('float32') / 255.0
x_test = x_test.reshape((-1, 28, 28, 1)).astype('float32') / 255.0

# Convert labels to one-hot encoding


y_train = tf.keras.utils.to_categorical(y_train, 10)
y_test = tf.keras.utils.to_categorical(y_test, 10)

# Create a custom CNN model


model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
MaxPooling2D((2, 2)),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D((2, 2)),
Flatten(),
Dense(128, activation='relu'),
Dense(10, activation='softmax')
])

# Compile the model

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'])

# Print the summary of the model


model.summary()

# Train the model


model.fit(x_train, y_train, epochs=5, batch_size=64, validation_split=0.2)

# Evaluate the model on test data


test_loss, test_accuracy = model.evaluate(x_test, y_test)
print(f'Test accuracy: {test_accuracy}')

Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 26, 26, 32) 320

max_pooling2d (MaxPooling2 (None, 13, 13, 32) 0


D)

conv2d_1 (Conv2D) (None, 11, 11, 64) 18496

max_pooling2d_1 (MaxPoolin (None, 5, 5, 64) 0


g2D)

flatten (Flatten) (None, 1600) 0

dense (Dense) (None, 128) 204928

dense_1 (Dense) (None, 10) 1290

=================================================================
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

# Load MNIST dataset


(x_train, y_train), (x_test, y_test) = mnist.load_data()

# Preprocess the data


x_train = x_train.reshape(x_train.shape[0], 28, 28, 1).astype('float32') / 255
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1).astype('float32') / 255
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)

# Define the CNN model


model = Sequential([
Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)),
MaxPooling2D(pool_size=(2, 2)),
Conv2D(64, kernel_size=(3, 3), activation='relu'),
MaxPooling2D(pool_size=(2, 2)),
Flatten(),
Dense(128, activation='relu'),
Dense(10, activation='softmax')
])

# Compile the model


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

# Train the CNN model


model.fit(x_train, y_train, epochs=5, batch_size=128, validation_split=0.2)

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

# Extract features from the CNN model


cnn_features_train = feature_extraction_model.predict(x_train)
cnn_features_test = feature_extraction_model.predict(x_test)

# Print the shape of the extracted features


print("Shape of CNN features (train):", cnn_features_train.shape)
print("Shape of CNN features (test):", cnn_features_test.shape)

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)

from sklearn.svm import SVC


from sklearn.metrics import accuracy_score, confusion_matrix

# Assuming cnn_features_train and cnn_features_test are extracted features from the CNN model

# Initialize and train the SVM classifier


svm_classifier = SVC(kernel='linear')
svm_classifier.fit(cnn_features_train, np.argmax(y_train, axis=1))

# Predict using the trained SVM classifier


svm_predictions = svm_classifier.predict(cnn_features_test)

# 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)

SVM Classifier Accuracy: 0.9915


Confusion Matrix for SVM Classifier:
[[ 978 0 0 0 0 0 0 1 1 0]
[ 0 1134 0 0 0 0 0 1 0 0]
[ 1 1 1020 1 1 0 0 5 2 1]
[ 0 0 3 1002 0 3 0 1 1 0]
[ 0 0 0 0 977 0 1 0 0 4]
[ 1 0 1 6 0 880 2 1 1 0]
[ 3 3 1 0 1 4 944 0 2 0]
[ 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]]

# Evaluate the trained SVM classifier


svm_accuracy = accuracy_score(np.argmax(y_test, axis=1), svm_predictions)
svm_conf_matrix = confusion_matrix(np.argmax(y_test, axis=1), svm_predictions)
print("SVM Classifier Accuracy:", svm_accuracy)
print("Confusion Matrix for SVM Classifier:")
print(svm_conf_matrix)

# Evaluate the trained CNN model


cnn_test_loss, cnn_test_accuracy = model.evaluate(x_test, y_test)
print("CNN Model Accuracy on Test Data:", cnn_test_accuracy)

SVM Classifier Accuracy: 0.9915


Confusion Matrix for SVM Classifier:
[[ 978 0 0 0 0 0 0 1 1 0]
[ 0 1134 0 0 0 0 0 1 0 0]
[ 1 1 1020 1 1 0 0 5 2 1]
[ 0 0 3 1002 0 3 0 1 1 0]
[ 0 0 0 0 977 0 1 0 0 4]
[ 1 0 1 6 0 880 2 1 1 0]
[ 3 3 1 0 1 4 944 0 2 0]

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

# Calculate and print the confusion matrix for CNN predictions


cnn_conf_matrix = confusion_matrix(np.argmax(y_test, axis=1), cnn_classes)
print("Confusion Matrix for CNN Model:")
print(cnn_conf_matrix)

313/313 [==============================] - 2s 8ms/step


Confusion Matrix for CNN Model:
[[ 974 0 0 0 0 0 2 1 3 0]
[ 0 1117 4 0 0 0 5 0 9 0]
[ 2 0 1027 0 0 0 0 1 2 0]
[ 0 0 2 1000 0 5 0 1 2 0]
[ 0 0 0 0 963 0 1 0 1 17]
[ 2 0 1 9 0 868 3 0 5 4]
[ 1 2 1 0 1 1 947 0 5 0]
[ 1 3 11 1 0 0 0 994 2 16]
[ 2 0 2 1 0 0 0 0 966 3]
[ 1 0 0 0 1 0 0 0 4 1003]]

import seaborn as sns


import matplotlib.pyplot as plt

# Assuming svm_conf_matrix and cnn_conf_matrix are the confusion matrices calculated previously

# Generate heatmap for SVM Classifier


plt.figure(figsize=(8, 6))
sns.heatmap(svm_conf_matrix, annot=True, fmt='d', cmap='Blues')
plt.xlabel('Predicted Labels')
plt.ylabel('True Labels')
plt.title('Confusion Matrix for SVM Classifier')
plt.show()

# Generate heatmap for CNN Model


plt.figure(figsize=(8, 6))
sns.heatmap(cnn_conf_matrix, annot=True, fmt='d', cmap='Blues')
plt.xlabel('Predicted Labels')
plt.ylabel('True Labels')
plt.title('Confusion Matrix for CNN Model')
plt.show()

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy