Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Data Science
  • Data Science Projects
  • Data Analysis
  • Data Visualization
  • Machine Learning
  • ML Projects
  • Deep Learning
  • NLP
  • Computer Vision
  • Artificial Intelligence
Open In App
Next Article:
Convolutional Neural Network (CNN) in Tensorflow
Next article icon

Building a Convolutional Neural Network using PyTorch

Last Updated : 11 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Convolutional Neural Networks (CNNs) are deep learning models used for image processing tasks. They automatically learn spatial hierarchies of features from images through convolutional, pooling and fully connected layers. In this article we'll learn how to build a CNN model using PyTorch. This includes defining the network architecture, preparing the data, training the model and evaluating its performance.

Implementation of Building a Convolutional Neural Network in PyTorch

Step 1: Import necessary libraries

In this Python code block, we are importing essential modules from the PyTorch library, which is a popular open-source machine learning framework.

Python
import torch import torch.nn as nn import torch.optim as optim import torchvision import torchvision.transforms as transforms import torch.nn.functional as F 

Step 2: Prepare the dataset

  • This code sets up the CIFAR-10 dataset for training and testing a neural network using PyTorch.
  • It defines a sequence of image transformations, including converting images to PyTorch tensors and normalizing them. Then, it creates dataset objects for both the training and test sets of CIFAR-10, specifying the root directory, that it's for training or testing, and the transformation sequence.
  • Next, it creates data loaders for both sets, which help in loading the data in batches, shuffling it, and using multiple processes for faster data loading.
  • Finally, it defines the class labels for CIFAR-10, representing the 10 different object classes in the dataset. Overall, this code prepares the CIFAR-10 dataset for use in training and evaluating neural network models.
Python
transform = transforms.Compose(     [transforms.ToTensor(),      transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])  trainset = torchvision.datasets.CIFAR10(root='./data', train=True,                                         download=True, transform=transform) trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,                                           shuffle=True, num_workers=2)  testset = torchvision.datasets.CIFAR10(root='./data', train=False,                                        download=True, transform=transform) testloader = torch.utils.data.DataLoader(testset, batch_size=4,                                          shuffle=False, num_workers=2)  classes = ('plane', 'car', 'bird', 'cat',            'deer', 'dog', 'frog', 'horse', 'ship', 'truck') 

Step 3: Define the CNN architecture

  • This code defines a neural network architecture using the nn.Module class from PyTorch. The Net class inherits from nn.Module and defines the layers of the network in its __init__ method.
  • It has two convolutional layers (conv1 and conv2) with ReLU activation functions, followed by max pooling layers (pool). The fully connected layers (fc1, fc2, and fc3) process the output of the convolutional layers.
  • The forward method defines the forward pass of the network, where input x is passed through each layer sequentially. The view method reshapes the output of the second convolutional layer to be compatible with the fully connected layers. Finally, an instance of the Net class is created as net, representing the neural network model.
Python
class Net(nn.Module):     def __init__(self):         super(Net, self).__init__()         self.conv1 = nn.Conv2d(3, 6, 5)         self.pool = nn.MaxPool2d(2, 2)         self.conv2 = nn.Conv2d(6, 16, 5)         self.fc1 = nn.Linear(16 * 5 * 5, 120)         self.fc2 = nn.Linear(120, 84)         self.fc3 = nn.Linear(84, 10)      def forward(self, x):         x = self.pool(F.relu(self.conv1(x)))         x = self.pool(F.relu(self.conv2(x)))         x = x.view(-1, 16 * 5 * 5)         x = F.relu(self.fc1(x))         x = F.relu(self.fc2(x))         x = self.fc3(x)         return x  net = Net() 

Step 4: Define loss function and optimizer

  • In this code , the nn.CrossEntropyLoss() is used as the loss function (criterion) for training the neural network. CrossEntropyLoss is commonly used for classification tasks and calculates the loss between the predicted class probabilities and the actual class labels.
  • The optimizer (optim.SGD) is used to update the weights of the neural network during training. Stochastic Gradient Descent (SGD) is the chosen optimization algorithm, with a learning rate of 0.001 and momentum of 0.9.
Python
criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9) 

Step 5: Train the network

This code trains a neural network (net) using the CIFAR-10 dataset with a specified loss function (criterion) and optimizer (optimizer) for 2 epochs, printing the average loss every 2000 mini-batches.

Python
for epoch in range(2):        running_loss = 0.0     for i, data in enumerate(trainloader, 0):         inputs, labels = data          optimizer.zero_grad()          outputs = net(inputs)         loss = criterion(outputs, labels)         loss.backward()         optimizer.step()          running_loss += loss.item()         if i % 2000 == 1999:              print('[%d, %5d] loss: %.3f' %                   (epoch + 1, i + 1, running_loss / 2000))             running_loss = 0.0  print('Finished Training') 

Step 6: Testing the network

This code calculates the accuracy of the neural network (net) on the test dataset (testloader) by comparing the predicted labels with the actual labels. It iterates over the test dataset, computes the outputs of the network for each image, and compares the predicted labels with the actual labels.

Python
correct = 0 total = 0 with torch.no_grad():     for data in testloader:         images, labels = data         outputs = net(images)         _, predicted = torch.max(outputs.data, 1)         total += labels.size(0)         correct += (predicted == labels).sum().item()  print('Accuracy of the network on the 10000 test images: %d %%' % (     100 * correct / total)) 

Complete Code to Build CNN using PyTorch

Python
import torch import torch.nn as nn import torch.optim as optim import torchvision import torchvision.transforms as transforms import torch.nn.functional as F  transform = transforms.Compose(     [transforms.ToTensor(),      transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])  trainset = torchvision.datasets.CIFAR10(root='./data', train=True,                                         download=True, transform=transform) trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,                                           shuffle=True, num_workers=2)  testset = torchvision.datasets.CIFAR10(root='./data', train=False,                                        download=True, transform=transform) testloader = torch.utils.data.DataLoader(testset, batch_size=4,                                          shuffle=False, num_workers=2)  classes = ('plane', 'car', 'bird', 'cat',            'deer', 'dog', 'frog', 'horse', 'ship', 'truck')  class Net(nn.Module):     def __init__(self):         super(Net, self).__init__()         self.conv1 = nn.Conv2d(3, 6, 5)         self.pool = nn.MaxPool2d(2, 2)         self.conv2 = nn.Conv2d(6, 16, 5)         self.fc1 = nn.Linear(16 * 5 * 5, 120)         self.fc2 = nn.Linear(120, 84)         self.fc3 = nn.Linear(84, 10)      def forward(self, x):         x = self.pool(F.relu(self.conv1(x)))         x = self.pool(F.relu(self.conv2(x)))         x = x.view(-1, 16 * 5 * 5)         x = F.relu(self.fc1(x))         x = F.relu(self.fc2(x))         x = self.fc3(x)         return x  net = Net()  criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)  for epoch in range(2):       running_loss = 0.0     for i, data in enumerate(trainloader, 0):         inputs, labels = data          optimizer.zero_grad()          outputs = net(inputs)         loss = criterion(outputs, labels)         loss.backward()         optimizer.step()          running_loss += loss.item()         if i % 2000 == 1999:              print('[%d, %5d] loss: %.3f' %                   (epoch + 1, i + 1, running_loss / 2000))             running_loss = 0.0  print('Finished Training')  correct = 0 total = 0 with torch.no_grad():     for data in testloader:         images, labels = data         outputs = net(images)         _, predicted = torch.max(outputs.data, 1)         total += labels.size(0)         correct += (predicted == labels).sum().item()  print('Accuracy of the network on the 10000 test images: %d %%' % (     100 * correct / total)) 

Output:

[1, 2000] loss: 2.279
[1, 4000] loss: 1.992
[1, 6000] loss: 1.718
[1, 8000] loss: 1.589
[1, 10000] loss: 1.513
[1, 12000] loss: 1.492
[2, 2000] loss: 1.410
[2, 4000] loss: 1.375
[2, 6000] loss: 1.366
[2, 8000] loss: 1.343
[2, 10000] loss: 1.325
[2, 12000] loss: 1.263
Finished Training
Accuracy of the network on the 10000 test images: 55 %

The model's accuracy of 55% shows that it is underperforming due to simple network architecture. To improve this we can experiment with adjusting the learning rate and momentum or can use better optimization techniques like Adam optimizer. These optimizations can help model achieve higher accuracy.


Next Article
Convolutional Neural Network (CNN) in Tensorflow

A

agarwalyoge6kqa
Improve
Article Tags :
  • Blogathon
  • Deep Learning
  • AI-ML-DS
  • Neural Network
  • Python-PyTorch
  • AI-ML-DS With Python
  • Data Science Blogathon 2024

Similar Reads

  • Cat & Dog Classification using Convolutional Neural Network in Python
    Convolutional Neural Networks (CNNs) are a type of deep learning model specifically designed for processing images. Unlike traditional neural networks CNNs uses convolutional layers to automatically and efficiently extract features such as edges, textures and patterns from images. This makes them hi
    5 min read
  • How to Define a Simple Convolutional Neural Network in PyTorch?
    In this article, we are going to see how to  Define a Simple Convolutional Neural Network in PyTorch using Python. Convolutional Neural Networks(CNN) is a type of Deep Learning algorithm which is highly instrumental in learning patterns and features in images. CNN has a unique trait which is its abi
    5 min read
  • Convolutional Neural Networks (CNNs) in R
    Convolutional Neural Networks (CNNs) are a specialized type of neural network designed to process and analyze visual data. They are particularly effective for tasks involving image recognition and classification due to their ability to automatically and adaptively learn spatial hierarchies of featur
    11 min read
  • Training Neural Networks using Pytorch Lightning
    Introduction: PyTorch Lightning is a library that provides a high-level interface for PyTorch. Problem with PyTorch is that every time you start a project you have to rewrite those training and testing loop. PyTorch Lightning fixes the problem by not only reducing boilerplate code but also providing
    7 min read
  • Convolutional Neural Network (CNN) in Tensorflow
    Convolutional Neural Networks (CNNs) have revolutionized the field of computer vision by automatically learning spatial hierarchies of features from images. In this article we will explore the basic building blocks of CNNs and show you how to implement a CNN model using TensorFlow. Building Blocks o
    5 min read
  • Working of Convolutional Neural Network (CNN) in Tensorflow
    In this article, we are going to see the working of convolution neural networks with TensorFlow a powerful machine learning library to create neural networks. Now to know, how a convolution neural network lets break it into parts. the 3 most important parts of this convolution neural networks are, C
    5 min read
  • Applying Convolutional Neural Network on mnist dataset
    CNN is a model known to be a Convolutional Neural Network and in recent times it has gained a lot of popularity because of its usefulness. CNN uses multilayer perceptrons to do computational work. CNN uses relatively little pre-processing compared to other image classification algorithms. This means
    6 min read
  • Vision Transformers vs. Convolutional Neural Networks (CNNs)
    In recent years, the landscape of computer vision has evolved significantly with the introduction of Vision Transformers (ViTs), which challenge the dominance of traditional Convolutional Neural Networks (CNNs). While CNNs have been the backbone of many state-of-the-art image classification models,
    5 min read
  • Training of Convolutional Neural Network (CNN) in TensorFlow
    In this article, we are going to implement and train a convolutional neural network CNN using TensorFlow a massive machine learning library. Now in this article, we are going to work on a dataset called 'rock_paper_sissors' where we need to simply classify the hand signs as rock paper or scissors.
    5 min read
  • Math Behind Convolutional Neural Networks
    Convolutional Neural Networks (CNNs) are designed to process data that has a known grid-like topology, such as images (which can be seen as 2D grids of pixels). The key components of a CNN include convolutional layers, pooling layers, activation functions, and fully connected layers. Each of these c
    8 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences