EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PyTorch MNIST

Home » Data Science » Data Science Tutorials » Machine Learning Tutorial » PyTorch MNIST

PyTorch MNIST

Introduction to PyTorch MNIST

The following article provides an outline for PyTorch MNIST. The Modified National Institute of Standards and Technology database or MNIST has all the useful details corresponding to image processing systems in various use cases. This database is helpful in many cases of machine learning, and deep learning as image processing details can be fetched directly from the database without any difficulties. In addition, we have digits from 0 to 9 where a baseline is available to test all the image processing systems.

What is PyTorch MNIST?

  • The database is a reliable source for all data scientists as it is considered as the welcome note of machine learning.
  • If any new architecture or framework is built, data scientists can train the algorithm on the MNIST to check whether the framework is working fine.
  • We have a training and test dataset in MNIST with 60000 and 10000 examples, respectively, in each dataset.
  • The images present inside the dataset are of the same size where the digits are present and normalized.

PyTorch MNIST Model

We are downloading MNIST dataset and using it in the PyTorch model.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Code:

from torchvision import datasets
from torchvision.transforms import ToTensor
train_dataset = datasets.MNIST(
root = 'datasets',
train = True,
transform = ToTensor(),
download = True,
)
test_dataset = datasets.MNIST(
root = 'datasets',
train = False,
transform = ToTensor()
)
print(train_dataset)
print(test_dataset)
print(train_dataset.data.size())
print(test_dataset.data.size())
import matplotlib.pyplot as plot
plot.imshow(train_dataset.data[0], cmap='gray')
plot.title('%i' % train_dataset.targets[0])
plot.show()
images = plot.figure(figsize=(15, 10))
columns, rows = 7, 7
for p in range(2, columns * rows + 1):
sample_idx = torch.randint(len(train_dataset), size=(1,)).item()
img, labels = train_dataset[sample_idx] figure.add_subplot(rows, columns, p)
plot.title(label)
plot.axis("off")
plot.imshow(img.squeeze(), cmap="gray")
plot.show()
class CNNetwork(nn.Module):
def __init__(self):
super(CNNetwork, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(
input_channels=2,
output_channels=8,
kernel_size=4,
stride=2,
padding=4,
),
nn.ReLU(),
nn.MaxPool2d(kernel_size=4),
)
self.conv2 = nn.Sequential(
nn.Conv2d(26, 44, 4, 2, 3),
nn.ReLU(),
nn.MaxPool2d(5),
)
self.output = nn.Linear(44 * 8 * 8, 5)
def forward(self, a):
a = self.conv1(a)
a = self.conv2(a)
a = a.view(a.size(0), -1)
result = self.out(a)
return result, a
from torch.autograd import Variable
number_epochs = 8
def train_dataset(number_epochs, cnn, loaders):
cnn.train_dataset()
total_step = len(loaders['train_dataset'])
for epoch in range(number_epochs):
for p, (images, labels) in enumerate(loaders['train_dataset']):
w_x = Variable(images)
w_y = Variable(labels)
output = cnn(w_x)[0] loss = loss_func(output, w_y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (p+1) % 100 == 0:
print ('Epoch'
.format(epoch + 1, number_epochs, i + 1, total_step, loss.item()))
pass
pass
pass
train(number_epochs, cnn, loaders)

Prerequisites

  • We must install Pytorch binaries when we have the system with NVIDIA GPU. All the inbuilt binaries are supported in PyTorch libraries, and hence this will be a handful to start with PyTorch. If needed, a simple program in CUDA will explain whether the import of PyTorch is working with the system. If we do not have NVIDIA GPU, then CUDA installation is needed before importing the PyTorch module for which documentation are provided.
  • Now to start with MNIST, we must install the determined cluster to start with. Multiple GPU servers can be used for on-premise deployments where we can start the cluster with a single command. Now, we have to import the model in PyTorch to MNIST dataset so that we can check the architecture is working well. A built-in training loop is present inside the module where we can use batches of data into the forward pass to do all the calculations.
  • The next steps to perform are as follows: initializing the code, building the model, followed by optimizer definition, and defining the forward pass. The final step is to load the training dataset and validate the same.

Using PyTorch on MNIST Dataset

  • It is easy to use PyTorch in MNIST dataset for all the neural networks. DataLoader module is needed with which we can implement a neural network, and we can see the input and hidden layers. Activation functions need to be applied with loss and optimizer functions so that we can implement the training loop. Now we can see the model and test it for accuracy of the model.
  • All the parameters for the model must be defined first after importing the needed libraries. The next step is to load the MNIST dataset and dataloader, where we can specify the same batch size. Then, since we have hidden layers in the network, we must use the ReLu activation function and the PyTorch neural network module. Finally, we must look for a feed-forward method in the dataset and apply the changes to the layers.
  • Softmax is not needed here as cross-entropy will function automatically to all the layers. After setting the loss and optimizer function in the dataset, a training loop must be created. All the images required for processing are reshaped so that input size and loss are calculated easily. We can do the final testing now, and gradients need not be computed here.

Example of PyTorch MNIST

Given below is the example mentioned:

The first step is to set up the environment by importing a torch and torchvision.

Code:

import torch
import torchvision
import matplotlib.pyplot as plot
num_epochs = 5
train_size_batch = 32
test_size_batch = 5000
lr_rate = 0.05
momentum = 0.75
log_intervals = 5
seeds = 2
torch.backends_enabled = False
torch.manual_seed(seeds)
train_load = torch.utils.data.DataLoader(
torchvision.datasets.MNIST('/filesaved/', train=True, download=True,
transform=torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize(
(0.1567,), (0.3791,))
])),
batch = train_size_batch, shuffle=True)
test_load = torch.utils.data.DataLoader(
torchvision.datasets.MNIST('/filesave/', train=False, download=True,
transform=torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize(
(0.1567,), (0.3791,))
])),
batch_size= test_size_batch, shuffle=True)
example_dataset = enumerate(test_load)
batch_idx, (example_log, example_results) = next(example_dataset)
example_log.shape
images = plot.figure()
for x in range(5):
plot.subplot(2,2,x+1)
plot.tight_layout()
plot.imshow(example_data[x][0], cmap='blue', interpolation='none')
plot.title("Facts: {}".format(example_results[x]))
plot.xticks([])
plot.yticks([])
images
import torch.nn as netnn
import torch.nn.functional as Fun
import torch.optim as optimnet
class Network(netn.Module):
def __init__(self):
super(Network, self).__init__()
self.conv1 = netn.Conv2d(1, 20, kernel_size=10)
self.conv2 = nn.Conv2d(10, 40, kernel_size=10)
self.conv2_drop = nn.Dropout2d()
self.fc1 = nn.Linear(390, 70)
self.fc2 = nn.Linear(60, 20)
def forwardnetwork(self, a):
a = Fun.relu(Fun.max_pool2d(self.conv1(a), 2))
a = Fun.relu(Fun.max_pool2d(self.conv2_drop(self.conv2(a)), 2))
a = a.view(-1, 320)
a = Fun.relu(self.fc1(a))
a = Fun.dropout(a, training=self.training)
a = self.fc2(a)
return Fun.log_softmax(a)

Conclusion

We can use MNIST in supervised learning where classifiers can be trained. This dataset, along with the machine learning dataset, helps data scientists in many aspects to discover different modes of training and give a broad description of the data being used in the dataset. A labelled dataset is preferred in these cases.

Recommended Articles

This is a guide to PyTorch MNIST. Here we discuss the introduction, PyTorch MNIST model, prerequisites, and example, respectively. You may also have a look at the following articles to learn more –

Popular Course in this category
Sale
Machine Learning Training (19 Courses, 29+ Projects)19 Online Courses | 29 Hands-on Projects | 178+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.7 (13,865 ratings)
Course Price

View Course

Related Courses
Deep Learning Training (16 Courses, 24+ Projects)Artificial Intelligence Training (5 Courses, 2 Project)
  1. PyTorch Versions
  2. torch.nn Module
  3. Tensorflow Basics
  4. Introduction to Tensorflow

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

50+ projects

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Machine Learning Tutorial
  • PyTorch
    • PyTorch Tensors
    • What is PyTorch?
    • PyTorch MSELoss()
    • PyTorch NLLLOSS
    • PyTorch MaxPool2d
    • PyTorch Pretrained Models
    • PyTorch Squeeze
    • PyTorch Reinforcement Learning
    • PyTorch zero_grad
    • PyTorch norm
    • PyTorch VAE
    • PyTorch Early Stopping
    • PyTorch requires_grad
    • PyTorch MNIST
    • PyTorch Conv2d
    • Dataset Pytorch
    • PyTorch tanh
    • PyTorch bmm
    • PyTorch profiler
    • PyTorch unsqueeze
    • PyTorch adam
    • PyTorch backward
    • PyTorch concatenate
    • PyTorch Embedding
    • PyTorch Tensor to NumPy
    • PyTorch Normalize
    • PyTorch ReLU
    • PyTorch Autograd
    • PyTorch Transpose
    • PyTorch Object Detection
    • PyTorch Autoencoder
    • PyTorch Loss
    • PyTorch repeat
    • PyTorch gather
    • PyTorch sequential
    • PyTorch U-NET
    • PyTorch Sigmoid
    • PyTorch Neural Network
    • PyTorch Quantization
    • PyTorch Ignite
    • PyTorch Versions
    • PyTorch TensorBoard
    • PyTorch Dropout
    • PyTorch Model
    • PyTorch optimizer
    • PyTorch ResNet
    • PyTorch CNN
    • PyTorch Detach
    • Single Layer Perceptron
    • PyTorch vs Keras
    • torch.nn Module
  • Basic
    • Introduction To Machine Learning
    • What is Machine Learning?
    • Uses of Machine Learning
    • Applications of Machine Learning
    • Naive Bayes in Machine Learning
    • Dataset Labelling
    • DataSet Example
    • Dataset ZFS
    • Careers in Machine Learning
    • What is Machine Cycle?
    • Machine Learning Feature
    • Machine Learning Programming Languages
    • What is Kernel in Machine Learning
    • Machine Learning Tools
    • Machine Learning Models
    • Machine Learning Platform
    • Machine Learning Libraries
    • Machine Learning Life Cycle
    • Machine Learning System
    • Machine Learning Datasets
    • Top 7 Useful Benefits Of Machine Learning Certifications
    • Machine Learning Python vs R
    • Optimization for Machine Learning
    • Types of Machine Learning
    • Machine Learning Methods
    • Machine Learning Software
    • Machine Learning Techniques
    • Machine Learning Feature Selection
    • Ensemble Methods in Machine Learning
    • Support Vector Machine in Machine Learning
    • Decision Making Techniques
    • Restricted Boltzmann Machine
    • Regularization Machine Learning
    • What is Regression?
    • What is Linear Regression?
    • Dataset for Linear Regression
    • Decision tree limitations
    • What is Decision Tree?
    • What is Random Forest
  • Algorithms
    • Machine Learning Algorithms
    • Apriori Algorithm in Machine Learning
    • Types of Machine Learning Algorithms
    • Bayes Theorem
    • AdaBoost Algorithm
    • Classification Algorithms
    • Clustering Algorithm
    • Gradient Boosting Algorithm
    • Mean Shift Algorithm
    • Hierarchical Clustering Algorithm
    • Hierarchical Clustering Agglomerative
    • What is a Greedy Algorithm?
    • What is Genetic Algorithm?
    • Random Forest Algorithm
    • Nearest Neighbors Algorithm
    • Weak Law of Large Numbers
    • Ray Tracing Algorithm
    • SVM Algorithm
    • Naive Bayes Algorithm
    • Neural Network Algorithms
    • Boosting Algorithm
    • XGBoost Algorithm
    • Pattern Searching
    • Loss Functions in Machine Learning
    • Decision Tree in Machine Learning
    • Hyperparameter Machine Learning
    • Unsupervised Machine Learning
    • K- Means Clustering Algorithm
    • KNN Algorithm
    • Monty Hall Problem
  • Supervised
    • What is Supervised Learning
    • Supervised Machine Learning
    • Supervised Machine Learning Algorithms
    • Perceptron Learning Algorithm
    • Simple Linear Regression
    • Polynomial Regression
    • Multivariate Regression
    • Regression in Machine Learning
    • Hierarchical Clustering Analysis
    • Linear Regression Analysis
    • Support Vector Regression
    • Multiple Linear Regression
    • Linear Algebra in Machine Learning
    • Statistics for Machine Learning
    • What is Regression Analysis?
    • Clustering Methods
    • Backward Elimination
    • Ensemble Techniques
    • Bagging and Boosting
    • Linear Regression Modeling
    • What is Reinforcement Learning
  • Classification
    • Kernel Methods in Machine Learning
    • Clustering in Machine Learning
    • Machine Learning Architecture
    • Automation Anywhere Architecture
    • Machine Learning C++ Library
    • Machine Learning Frameworks
    • Data Preprocessing in Machine Learning
    • Data Science Machine Learning
    • Classification of Neural Network
    • Neural Network Machine Learning
    • What is Convolutional Neural Network?
    • Single Layer Neural Network
    • Kernel Methods
    • Forward and Backward Chaining
    • Forward Chaining
    • Backward Chaining
  • Deep Learning
    • What Is Deep learning
    • Overviews Deep Learning
    • Application of Deep Learning
    • Careers in Deep Learnings
    • Deep Learning Frameworks
    • Deep Learning Model
    • Deep Learning Algorithms
    • Deep Learning Technique
    • Deep Learning Networks
    • Deep Learning Libraries
    • Deep Learning Toolbox
    • Types of Neural Networks
    • Convolutional Neural Networks
    • Create Decision Tree
    • Deep Learning for NLP
    • Caffe Deep Learning
    • Deep Learning with TensorFlow
  • RPA
    • What is RPA
    • What is Robotics?
    • Benefits of RPA
    • RPA Applications
    • Types of Robots
    • RPA Tools
    • Line Follower Robot
    • What is Blue Prism?
    • RPA vs BPM
  • UiPath
    • What is UiPath
    • UiPath Action Center
    • UiPath?Orchestrator
    • UiPath web automation
    • UiPath Orchestrator API
    • UiPath Delay
    • UiPath Careers
    • UiPath Architecture
    • UiPath version
    • Uipath Reframework
    • UiPath Studio
  • Interview Questions
    • Deep Learning Interview Questions And Answer
    • Machine Learning Cheat Sheet

Related Courses

Machine Learning Training

Deep Learning Training

Artificial Intelligence Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

Special Offer - Machine Learning Training Learn More