Experiment - 3
CNN_on_Mnist_Dataset
import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
# Load and preprocess the MNIST dataset
(train_images, train_labels), (test_images, test_labels) =
mnist.load_data()
train_images = train_images.reshape((60000, 28, 28, 1))
test_images = test_images.reshape((10000, 28, 28, 1))
train_images = train_images.astype('float32') / 255
test_images = test_images.astype('float32') / 255
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
# Create a simple CNN model
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu',
input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
# Compile the model
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
print(model.summary())
# Train the model
model.fit(train_images, train_labels, epochs=5, batch_size=64,
validation_data=(test_images, test_labels))
# Evaluate the model on the test data
test_loss, test_acc = model.evaluate(test_images, test_labels)
print(f'Test accuracy: {test_acc * 100:.2f}%')
predictions = model.predict(test_images)
Output:-
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-
datasets/mnist.npz
11490434/11490434 [==============================] - 1s 0us/step
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)
conv2d_2 (Conv2D) (None, 3, 3, 64) 36928
flatten (Flatten) (None, 576) 0
dense (Dense) (None, 64) 36928
dense_1 (Dense) (None, 10) 650
=================================================================
Total params: 93322 (364.54 KB)
Trainable params: 93322 (364.54 KB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
None
Epoch 1/5
938/938 [==============================] - 56s 58ms/step - loss: 0.1783 -
accuracy: 0.9475 - val_loss: 0.0604 - val_accuracy: 0.9804
Epoch 2/5
938/938 [==============================] - 51s 54ms/step - loss: 0.0509 -
accuracy: 0.9837 - val_loss: 0.0352 - val_accuracy: 0.9875
Epoch 3/5
938/938 [==============================] - 53s 56ms/step - loss: 0.0365 -
accuracy: 0.9891 - val_loss: 0.0278 - val_accuracy: 0.9912
Epoch 4/5
938/938 [==============================] - 52s 55ms/step - loss: 0.0280 -
accuracy: 0.9912 - val_loss: 0.0315 - val_accuracy: 0.9904
Epoch 5/5
938/938 [==============================] - 51s 54ms/step - loss: 0.0228 -
accuracy: 0.9928 - val_loss: 0.0268 - val_accuracy: 0.9911
313/313 [==============================] - 3s 8ms/step - loss: 0.0268 -
accuracy: 0.9911
Test accuracy: 99.11%
313/313 [==============================] - 3s 10ms/step