Handwritten Digit Recognition in PyTorch

PyTorch (https://pytorch.org/) is an open-source machine learning framework developed at Facebook. It provides different components such as optimizers, loss functions, fully connected layers, activation function, etc. to build deep learning architectures. 

In this lab, we will learn how to recognize handwritten digits (0-9) in the MNIST Dataset using a simple network in PyTorch.

Obtaining the Dataset

MNIST handwritten digits dataset contains images of handwritten digits at a grey scale. Grey Scale images are ones that can be understood as black and white images. So while loading the dataset we need to convert these images to some datatype that PyTorch can understand. PyTorch uses data as a Tensor type. A Tensor can be understood as a container that stores data in some n dimensions. It also stores some useful data, such as the relationship between its elements and the relationship with other elements. But we can ignore these for now. We also normalize each image with a mean and standard deviation of 0.5. 

transform = transforms.Compose([transforms.ToTensor(),
                              transforms.Normalize((0.5,), (0.5,)),
                              ])
train_data = datasets.MNIST('PATH_TO_STORE_TRAINSET', download=True, train=True, transform=transform)
test_data = datasets.MNIST('PATH_TO_STORE_TRAINSET', download=True, train=False, transform=transform)
Discussion