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

Handwritten Digit Recognition Using a Neural Network (2)

The document outlines a mini project aimed at developing a deep learning model to recognize handwritten digits using the MNIST dataset. It details the steps involved, including data loading, normalization, model building, training, and evaluation, along with Python code for implementation. The final result shows that the model achieved an accuracy of approximately 98% on the test dataset.
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)
14 views

Handwritten Digit Recognition Using a Neural Network (2)

The document outlines a mini project aimed at developing a deep learning model to recognize handwritten digits using the MNIST dataset. It details the steps involved, including data loading, normalization, model building, training, and evaluation, along with Python code for implementation. The final result shows that the model achieved an accuracy of approximately 98% on the test dataset.
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/ 4

DATE : MINI PROJECT

HANDWRITTEN DIGIT RECOGNITION USING A NEURAL NETWORK

AIM:
To develop a simple deep learning model using Python to recognize handwritten digits
from the MNIST dataset.

ALGORITHM:

STEP 1: Load the MNIST dataset from Keras datasets.


STEP 2: Normalize the data to have values between 0 and 1 and reshape it.
STEP 3: Build a Sequential neural network model with input, hidden, and output layers.

STEP 4: Compile the model with an optimizer (e.g., Adam) and a loss function (e.g.,
sparse categorical crossentropy).

STEP 5: Train the model using the training dataset.


STEP 6: Evaluate the model's performance on the test dataset.

STEP 7: Test the model with sample inputs to verify predictions.


Python Code:

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.datasets import mnist
import matplotlib.pyplot as plt

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


x_train, x_test = x_train / 255.0, x_test / 255.0
model = Sequential([
Flatten(input_shape=(28, 28)), Dense(128, activation='relu'),
Dense(10, activation='softmax')])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5, batch_size=32, verbose=2)
test_loss, test_accuracy = model.evaluate(x_test, y_test)
print(f"Test Accuracy: {test_accuracy:.2f}")
predictions = model.predict(x_test)
plt.imshow(x_test[0], cmap='gray')
plt.title(f"Predicted: {predictions[0].argmax()}, Actual: {y_test[0]}")
plt.show()
OUTPUT:

Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz


11490434/11490434 ━━━━━━━━━━━━━━━━━━━━ 0s 0us/step
/usr/local/lib/python3.10/dist-packages/keras/src/layers/reshaping/flatten.py:37:
UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using
Sequential models, prefer using an `Input(shape)` object as the first layer in the model
instead.
super().__init__(**kwargs)
Epoch 1/5
1875/1875 - 9s - 5ms/step - accuracy: 0.9253 - loss: 0.2619
Epoch 2/5
1875/1875 - 4s - 2ms/step - accuracy: 0.9666 - loss: 0.1147
Epoch 3/5
1875/1875 - 6s - 3ms/step - accuracy: 0.9764 - loss: 0.0781
Epoch 4/5
1875/1875 - 8s - 5ms/step - accuracy: 0.9828 - loss: 0.0575
Epoch 5/5
1875/1875 - 8s - 5ms/step - accuracy: 0.9861 - loss: 0.0453
313/313 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.9746 -
loss: 0.0812
Test Accuracy: 0.98
313/313 ━━━━━━━━━━━━━━━━━━━━ 1s 3ms/step
RESULT:
A simple deep learning model is successfully implemented to recognize handwritten
digits with an accuracy of approximately 98% on the MNIST dataset.

You might also like