This device is not compatible.


Recognize Handwritten Digits Using a Deep Neural Network

In this project, we'll use a deep neural network to recognize handwritten digits. We'll use the images of handwritten digits from the MNIST dataset.

Recognize Handwritten Digits Using a Deep Neural Network

You will learn to:

Load data from the MNIST website.

Build a deep neural network using PyTorch.

Plot the images using Matplotlib.

Train and test the deep neural network using PyTorch.


Machine Learning

Neural Networks

Deep Neural Networks


Intermediate knowledge of Python

Basic knowledge of Matplotlib

Basic knowledge of neural networks





Project Description

It might be easy for us to recognize the blurred image given below owing to the amazing functionality of the human brain, but it is not so simple for computers.

Sample image from MNIST

We can recognize it with such ease, possibly because we’ve seen so many different variants of the digit “6” that our brain has learned to recognize this digit in various forms. There is an area of machine learning called deep learning that mimics this learning mechanism of the human brain—learning by example.

Deep learning is based on neural networks that are made up of different layers of linked “neurons.” It is inspired by the structure and function of the neurons in the human brain. We need massive amounts of data and complex algorithms to train a neural network.

In this project, we will use PyTorch to implement a deep learning algorithm to recognize handwritten digits. We will use the MNIST dataset to complete this project. It is a collection of 70000 handwritten digits divided into a training set of 60000 images and a test set of 10000 images.

Project Tasks


Loading the Datasets

Task 1: Import Modules

Task 2: Create a Transformation

Task 3: Download and Load Datasets

Task 4: Visualize Images


Build the Neural Network

Task 5: Decide the Number of Layers and Their Sizes

Task 6: Build a Model

Task 7: Calculate Cross-Entropy Loss


Train and Test Model

Task 8: Obtain the Stochastic Gradient Descent Optimizer

Task 9: Train the Model

Task 10: Get the Predicted Label

Task 11: Test the Model