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

Ex_5

The document outlines a procedure for implementing a neural network model using TensorFlow and Keras to classify handwritten digits from the MNIST dataset. It details the steps for data preprocessing, model building, training, evaluation, and visualization of results. The final output indicates a test accuracy of 0.98, demonstrating the model's effectiveness in digit classification.

Uploaded by

Narenkumar. N
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Ex_5

The document outlines a procedure for implementing a neural network model using TensorFlow and Keras to classify handwritten digits from the MNIST dataset. It details the steps for data preprocessing, model building, training, evaluation, and visualization of results. The final output indicates a test accuracy of 0.98, demonstrating the model's effectiveness in digit classification.

Uploaded by

Narenkumar. N
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

EXP NO 5 Handwritten Digit Classification using

DATE Neural Networks

AIM :
To implement a neural network model using TensorFlow and Keras for
classifying handwritten digits from the MNIST dataset.

PROCEDURE :
1.Import necessary libraries (tensorflow, numpy, matplotlib).
2.Load and preprocess the MNIST dataset (normalize pixel values,
apply one-hot encoding).
3.Display sample images from the training set.
4.Build a neural network with three layers (Flatten, Dense with ReLU,
and Softmax output).
5.Compile and train the model using Adam optimizer and categorical
cross-entropy loss.
6.Evaluate the model on test data and display accuracy.
7.Predict and visualize results for five random test images.

PROGRAM :
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.utils import to_categorical
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
y_train, y_test = to_categorical(y_train, 10), to_categorical(y_test, 10)

fig, axes = plt.subplots(1, 5, figsize=(10, 2))


for i, ax in enumerate(axes):
ax.imshow(x_train[i], cmap='gray')
ax.axis('off')
plt.show()

model = Sequential([
Flatten(input_shape=(28, 28)),
Dense(128, activation='relu'),
Dense(64, activation='relu'),
Dense(10, activation='softmax')
])

model.compile(optimizer='adam', loss='categorical_crossentropy',
metrics=['accuracy'])
history = model.fit(x_train, y_train, epochs=10, validation_split=0.2,
verbose=0)

test_loss, test_acc = model.evaluate(x_test, y_test)


print(f"Test Accuracy: {test_acc:.2f}")

sample_idx = np.random.choice(len(x_test), 5, replace=False)


x_sample, y_sample = x_test[sample_idx], y_test[sample_idx]
predictions = model.predict(x_sample)
fig, axes = plt.subplots(1, 5, figsize=(10, 2))
for i, ax in enumerate(axes):
ax.imshow(x_sample[i], cmap='gray')
ax.axis('off')
ax.set_title(f"P: {np.argmax(predictions[i])}", color='green')
plt.show()

OUTPUT :

313/313 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step -


accuracy: 0.9721 - loss: 0.1119
Test Accuracy: 0.98
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 61ms/step

RESULT :
The neural network successfully classified handwritten digits from the
MNIST dataset with high accuracy.

You might also like