1.
Implement multilayer perceptron algorithm for MNIST Hand written Digit
Classification.
DESCRIPTION:
MNIST stands for “Modified National Institute of Standards and Technology”. It is a dataset of
70,000 handwritten images. Each image is of 28x28 pixels i.e. about 784 features. Each feature
represents only one pixel’s intensity i.e. from 0(white) to 255(black). This database is further
divided into 60,000 training and 10,000 testing images.
We imported TensorFlow which is an open-source free library that is used for machine learning
applications such as neural networks etc. Further, we imported pyplot function, which is
basically used for plotting, from the matplotlib library which is used for visualisation purposes.
After that, we imported NumPy i.e. Numerical Python which is used to perform various
mathematical [Link] MNIST dataset is also part of it. So, we imported it
from [Link] and loaded it into variable “objects”. The objects.load_data() method returns
us the training data(train_img), its labels(train_lab) and also the testing data(test_img) and its
labels(test_lab). Out of the 70,000 images provided in the dataset, 60,000 are given for training
and 10,000 are given for testing.
Algorithm:
Steps to Implement MLP for MNIST Classification:
Step1: Import libraries
Step2: Load and preprocess the MNIST dataset
Step3: Define the MLP model
Step4: Compile the model
Step5: Train the model
Step6: Evaluate the model
Step7: Make predictions
PROGRAM:
import numpy as np
import [Link] as plt
from [Link] import mnist
from [Link] import Sequential
from [Link] import Dense, Flatten
from [Link] import to_categorical
# Load MNIST dataset
(X_train, y_train), (X_test, y_test) = mnist.load_data()
[Link](X_train[2], cmap='Greys')
[Link](figsize=(5,5))
for k in range(12):
[Link](3, 4, k+1)
[Link](X_train[k], cmap='Greys')
[Link]('off')
plt.tight_layout()
[Link]()
# Preprocess the data
X_train = X_train / 255.0
X_test = X_test / 255.0
# Flatten the images
X_train_flatten = X_train.reshape(X_train.shape[0], -1)
X_test_flatten = X_test.reshape(X_test.shape[0], -1)
# Convert labels to one-hot encoding
y_train_onehot = to_categorical(y_train, num_classes=10)
y_test_onehot = to_categorical(y_test, num_classes=10)
# Define the MLP model
model = Sequential([
Dense(128, activation='relu', input_shape=(784,)),
Dense(64, activation='relu'),
Dense(10, activation='softmax')
])
# Compile the model
[Link](optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# Train the model
history = [Link](X_train_flatten, y_train_onehot, epochs=10, batch_size=128,
validation_data=(X_test_flatten, y_test_onehot))
# Plot accuracy
[Link]([Link]['accuracy'], label='accuracy')
[Link]([Link]['val_accuracy'], label='val_accuracy')
[Link]('Epoch')
[Link]('Accuracy')
[Link]()
[Link]()
# Evaluate the model on the test data
test_loss, test_accuracy = [Link](X_test_flatten, y_test_onehot)
print(f'Test accuracy: {test_accuracy*100}')