0% found this document useful (0 votes)
30 views2 pages

Experiment 1

This lab manual outlines the implementation of a Multilayer Perceptron (MLP) for classifying handwritten digits from the MNIST dataset using TensorFlow and Keras. It details the objectives, theory, program code, and steps involved in building, training, and evaluating the MLP model. The conclusion highlights the model's successful performance in digit classification and suggests potential improvements.

Uploaded by

gnanesh847
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views2 pages

Experiment 1

This lab manual outlines the implementation of a Multilayer Perceptron (MLP) for classifying handwritten digits from the MNIST dataset using TensorFlow and Keras. It details the objectives, theory, program code, and steps involved in building, training, and evaluating the MLP model. The conclusion highlights the model's successful performance in digit classification and suggests potential improvements.

Uploaded by

gnanesh847
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Deep Learning with TensorFlow – Lab Manual

Experiment 1: Implement Multilayer Perceptron Algorithm for MNIST


Handwritten Digit Classification
Title:
Multilayer Perceptron (MLP) for MNIST Handwritten Digit Classification using TensorFlow and Keras.

Aim:
To design and implement a Multilayer Perceptron (MLP) for classifying handwritten digits from the MNIST dataset
using TensorFlow and Keras.

Objective:
● Understand the concept of Multilayer Perceptron (MLP).
● Learn how to use TensorFlow and Keras to build and train an MLP model.
● Perform digit classification on the MNIST dataset.
● Evaluate model performance using accuracy metrics.

Theory:
Multilayer Perceptron (MLP) is a type of artificial neural network (ANN) that consists of multiple layers:

● Input Layer: Accepts raw input features.


● Hidden Layers: Contain neurons with activation functions like ReLU to learn complex patterns.
● Output Layer: Produces class predictions using the Softmax activation function.

The MNIST dataset consists of 70,000 grayscale images of handwritten digits (0-9), with 60,000 for training and
10,000 for testing.

Program:
# Import necessary libraries
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt

# Load MNIST dataset


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

# Normalize pixel values to [0, 1]


x_train, x_test = x_train / 255.0, x_test / 255.0

# Define the MLP model


model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)), # Flatten 28x28 images to 1D array
keras.layers.Dense(128, activation='relu'), # Hidden layer with 128 neurons
keras.layers.Dense(10, activation='softmax') # Output layer with 10 classes
])

# Compile the model


model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# Train the model
model.fit(x_train, y_train, epochs=5)

# Evaluate on test data


test_loss, test_acc = model.evaluate(x_test, y_test)
print("Test Accuracy:", test_acc)

Explanation:
1. Import Libraries: We import TensorFlow, Keras, and other required libraries.
2. Load MNIST Dataset: The dataset is loaded and split into training and testing sets.
3. Normalize Data: The pixel values are scaled to [0,1] for better convergence.
4. Define Model:
○ Flatten Layer: Converts 2D images to a 1D array.
○ Dense Layer (128 neurons, ReLU activation): Learns features from input data.
○ Dense Layer (10 neurons, Softmax activation): Outputs probabilities for 10 digit classes.
5. Compile Model:
○ Optimizer: Adam optimizer is used for gradient descent.
○ Loss Function: Sparse Categorical Crossentropy is used for multi-class classification.
○ Metrics: Accuracy is used to measure model performance.
6. Train Model: The model is trained for 5 epochs using the training dataset.
7. Evaluate Model: The model's accuracy is tested on unseen test data.

Conclusion:
The MLP model successfully classifies handwritten digits from the MNIST dataset with high accuracy. The model
can be further improved by tuning hyperparameters or adding more hidden layers.

I will now proceed with Experiment 2. Let me know if you need modifications or additional explanations!

You might also like