DEV Community

Saqib Jamil
Saqib Jamil

Posted on

Python Machine Learning - Handwritten Digit Recognition with TensorFlow

Machine learning has witnessed remarkable advancements in recent years, enabling a wide range of applications. One such fascinating application is Handwritten Digit Recognition. In this article, we will explore how to build a digit recognition model using TensorFlow, a popular deep learning library in Python. We will create a neural network that can identify handwritten digits from the famous MNIST dataset.

To follow along with this tutorial, you should have a basic understanding of Python programming and some familiarity with machine learning concepts. Additionally, ensure that you have TensorFlow and its dependencies installed on your system.

The MNIST Dataset:
The MNIST dataset is a classic dataset widely used for training and testing machine learning models. It consists of 28x28 grayscale images of handwritten digits (0 to 9) and their corresponding labels. The dataset contains 60,000 training images and 10,000 testing images.

Getting Started:
First, let’s install TensorFlow and numpy if you haven’t already:

pip install tensorflow
pip install numpy

//or  If you are using Python 3
pip3 install tensorflow
pip3 install numpy
Enter fullscreen mode Exit fullscreen mode

TensorFlow and NumPy are both popular and powerful libraries in Python, but they serve different purposes in the field of data science and machine learning.

TensorFlow: TensorFlow is an open-source machine learning library developed by the Google Brain team. It is primarily used for building and training machine learning and deep learning models. TensorFlow allows developers to define and train complex mathematical computations involving multi-dimensional arrays, known as tensors. It provides a flexible and efficient ecosystem for various machine learning tasks, including neural networks, natural language processing, computer vision, and more.

NumPy: NumPy is a fundamental library for numerical computing in Python. It stands for “Numerical Python” and provides support for large, multi-dimensional arrays and matrices, along with a collection of high-level mathematical functions to operate on these arrays. NumPy is the foundation of many other scientific computing libraries in Python, including pandas, SciPy, and scikit-learn.

Import the required libraries:

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

//or you can use it manually in the code without importing like this

Enter fullscreen mode Exit fullscreen mode

Load and Preprocess the Data:
We will load the MNIST dataset and preprocess the data by normalizing the pixel values to a range between 0 and 1:

Load the MNIST dataset

(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

Enter fullscreen mode Exit fullscreen mode

Normalize the pixel values to [0, 1]

train_images, test_images = train_images / 255.0, test_images / 255.0
Enter fullscreen mode Exit fullscreen mode

Building the Neural Network:
Now, let’s create a simple neural network using TensorFlow’s Sequential API. Our model will have one input layer, one hidden layer, and one output layer:

model = Sequential([
    Flatten(input_shape=(28, 28)),  # Flatten the 28x28 input images to a 1D array
    Dense(128, activation='relu'),   # Hidden layer with 128 neurons and ReLU activation
    Dense(10, activation='softmax')  # Output layer with 10 neurons and softmax activation
Enter fullscreen mode Exit fullscreen mode

Compile the Model:
Before training the model, we need to compile it by specifying the optimizer, loss function, and metrics:

Enter fullscreen mode Exit fullscreen mode

Training the Model:
It’s time to train our digit recognition model on the training data:, train_labels, epochs=5, batch_size=32)
Enter fullscreen mode Exit fullscreen mode

Evaluating the Model:
Once the model is trained, we can evaluate its performance on the test data:

test_loss, test_accuracy = model.evaluate(test_images, test_labels)
print(f"Test Accuracy: {test_accuracy}")
Enter fullscreen mode Exit fullscreen mode

Making Predictions:
Finally, we can use our trained model to make predictions on new images:

import numpy as np
Enter fullscreen mode Exit fullscreen mode

Choose a random test image

random_test_image = np.expand_dims(test_images[0], axis=0)
Enter fullscreen mode Exit fullscreen mode

Make a prediction on the random test image

prediction = model.predict(random_test_image)
Enter fullscreen mode Exit fullscreen mode

Get the predicted digit

predicted_digit = np.argmax(prediction)

print(f"Predicted Digit: {predicted_digit}")
Enter fullscreen mode Exit fullscreen mode

In this article, we have explored how to build a digit recognition model using TensorFlow in Python. We leveraged the MNIST dataset to train and test the model’s accuracy. Handwritten digit recognition is just one example of the endless possibilities of machine learning. With TensorFlow’s capabilities, we can develop sophisticated models to tackle a wide range of real-world challenges.

Machine learning opens up a world of opportunities for automation and pattern recognition. As you continue your journey in the field of AI and machine learning, keep exploring different datasets, architectures, and optimization techniques to enhance your models’ performance. Happy coding!

(Note: This article provides a basic example of digit recognition using TensorFlow. For real-world applications, consider using more complex models and larger datasets for better accuracy.)

Top comments (0)