Deep Learning with?PyTorch
MNIST Datasets

Deep Learning with?PyTorch

Using MNIST Datasets to learn PyTorch Deep Learning

After several projects using TensorFlow as a machine learning tool, I focused on Pytorch this time to run the project using the MNIST database. I’m going to use Colaboratory (from google) to use Python on the Online Jupyter.

PyTorch is an open-source machine learning library for Python, based on Torch, used for applications such as natural language processing. It is primarily developed by Facebook’s artificial-intelligence research group, and Uber’s “Pyro” software for probabilistic programming is built on it.

The MNIST dataset

The MNIST dataset was constructed from two datasets of the US National Institute of Standards and Technology (NIST). The training set consists of handwritten digits from 250 different people, 50 percent high school students, and 50 percent employees from the Census Bureau. Note that the test set contains handwritten digits from different people following the same split.

The MNIST dataset is publicly available here, at and consists of the following four parts: 

  • Training set images: train-images-idx3-ubyte.gz (9.9 MB, 47 MB unzipped, and 60,000 samples) 
  • Training set labels: train-labels-idx1-ubyte.gz (29 KB, 60 KB unzipped, and 60,000 labels) 
  • Test set images: t10k-images-idx3-ubyte.gz (1.6 MB, 7.8 MB, unzipped and 10,000 samples)
  • Test set labels: t10k-labels-idx1-ubyte.gz (5 KB, 10 KB unzipped, and 10,000 labels)

PyTorch provides two high-level features:

a) Tensor computation (like NumPy) with strong GPU acceleration

b) Deep Neural Networks built on a tape-based autodiff system

PyTorch consists of 4 main packages:

  1. torch: a general purpose array library similar to Numpy that can do computations on GPU when the tensor type is cast to (torch.cuda.TensorFloat)
  2. torch.autograd: a package for building a computational graph and automatically obtaining gradients
  3. torch.nn: a neural net library with common layers and cost functions
  4. torch.optim: an optimization package with common optimization algorithms like SGD,Adam, etc

PyTorch Tensors:

In terms of programming, Tensors can simply be considered multidimensional arrays. Tensors in PyTorch are similar to NumPy arrays, with the addition being that Tensors can also be used on a GPU that supports CUDA. PyTorch supports various types of Tensors.

References:

PyTorch

Example with MNIST Datasets

Git Hsaghir

This complete work in python by being consulted on GitHub.

Let’s go. Using online Colaboratory, we nedd install some packages (torch and torchvision):

We need declare the packages below:

Read the MNIST datasets:

In my test i have 60000 records for training and 10000 records to test dataset. See below.

After reading the datasets, i can see one or two records for test. Here i look for record number 5, initiated on index_image.

The system say the we have number 2 on the image index number 5. Printing some records below.

At index 9 we have the image of number 4.

We can print some number images, only for test purpose. The first 18 images are printed below (the code you can see on GitHub):

Lets training the model. I trained with 10 epochs:

We can verify the accuracity of my test, and we have 94%.

Very good, since I only used 10 epoches. A test that I did with 20 epoches the system showed a accuracy of 98%

Conclusion:

PyThorch algorithm it’s easy to implement, accurate, and very reliable to implement using Collaborative platform.

要查看或添加评论,请登录

Manuel Robalinho的更多文章

社区洞察

其他会员也浏览了