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:
Introduction in deep learning with julia
Next article icon

Deep Learning with PyTorch | An Introduction

Last Updated : 19 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

PyTorch in a lot of ways behaves like the arrays we love from Numpy. These Numpy arrays, after all, are just tensors. PyTorch takes these tensors and makes it simple to move them to GPUs for the faster processing needed when training neural networks. It also provides a module that automatically calculates gradients (for backpropagation) and another module specifically for building neural networks. All together, PyTorch ends up being more flexible with Python and the Numpy stack compared to TensorFlow and other frameworks. 

Neural Networks: Deep Learning is based on artificial neural networks which have been around in some form since the late 1950s. The networks are built from individual parts approximating neurons, typically called units or simply “neurons.” Each unit has some number of weighted inputs. These weighted inputs are summed together (a linear combination) then passed through an activation function to get the unit’s output. Below is an example of a simple

PyTorch is an open-source machine learning library based on the Torch library, developed by Facebook’s AI Research lab. It is widely used in deep learning, natural language processing, and computer vision applications. PyTorch provides a dynamic computational graph, which allows for easy and efficient modeling of complex neural networks.

Here’s a general overview of how to use PyTorch for deep learning:

Define the model: PyTorch provides a wide range of pre-built neural network architectures, such as fully connected networks, convolutional neural networks (CNNs), and recurrent neural networks (RNNs). The model can be defined using PyTorch’s torch.nn module.

Define the loss function: The loss function is used to evaluate the model’s performance and update its parameters. PyTorch provides a wide range of loss functions, such as cross-entropy loss and mean squared error loss.

Define the optimizer: The optimizer is used to update the model’s parameters based on the gradients of the loss function. PyTorch provides a wide range of optimizers, such as Adam and SGD.

Train the model: The model is trained on a dataset using the defined loss function and optimizer. PyTorch provides a convenient data loading and processing library, torch.utils.data, that can be used to load and prepare the data.

Evaluate the model: After training, the model’s performance can be evaluated on a test dataset.

Deploy the model: The trained model can be deployed in a wide range of applications, such as computer vision and natural language processing.

 neural net.  

Tensors: It turns out neural network computations are just a bunch of linear algebra operations on tensors, which are a generalization of matrices. A vector is a 1-dimensional tensor, a matrix is a 2-dimensional tensor, an array with three indices is a 3-dimensional tensor. The fundamental data structure for neural networks are tensors and PyTorch is built around tensors. It’s time to explore how we can use PyTorch to build a simple neural network. 

Python3

# First, import PyTorch
import torch
                      
                       

Define an activation function(sigmoid) to compute the linear output 

Python3

def activation(x):
    """ Sigmoid activation function
 
            Arguments
            ---------
            x: torch.Tensor
    """
    return 1/(1 + torch.exp(-x))
                      
                       

Python3

# Generate some data
# Features are 3 random normal variables
features = torch.randn((1, 5))
 
# True weights for our data, random normal variables again
weights = torch.randn_like(features)
 
# and a true bias term
bias = torch.randn((1, 1))
                      
                       

features = torch.randn((1, 5)) creates a tensor with shape (1, 5), one row and five columns, that contains values randomly distributed according to the normal distribution with a mean of zero and standard deviation of one. weights = torch.randn_like(features) creates another tensor with the same shape as features, again containing values from a normal distribution. Finally, bias = torch.randn((1, 1)) creates a single value from a normal distribution.

Now we calculate the output of the network using matrix multiplication. 

Python3

y = activation(torch.mm(features, weights.view(5, 1)) + bias)
                      
                       

That’s how we can calculate the output for a single neuron. The real power of this algorithm happens when you start stacking these individual units into layers and stacks of layers, into a network of neurons. The output of one layer of neurons becomes the input for the next layer. With multiple input units and output units, we now need to express the weights as a matrix. We define the structure of neural network and initialize the weights and biases. 

Python3

# Features are 3 random normal variables
features = torch.randn((1, 3))
 
# Define the size of each layer in our network
 
# Number of input units, must match number of input features
n_input = features.shape[1]    
n_hidden = 2                # Number of hidden units
n_output = 1                # Number of output units
 
# Weights for inputs to hidden layer
W1 = torch.randn(n_input, n_hidden)
 
# Weights for hidden layer to output layer
W2 = torch.randn(n_hidden, n_output)
 
# and bias terms for hidden and output layers
B1 = torch.randn((1, n_hidden))
B2 = torch.randn((1, n_output))
                      
                       

Now we can calculate the output for this multi-layer network using the weights W1 & W2, and the biases, B1 & B2. 

Python3

h = activation(torch.mm(features, W1) + B1)
output = activation(torch.mm(h, W2) + B2)
print(output)
                      
                       

ADVANTAGES AND DISADVANTAGES:

Advantages of using PyTorch for deep learning:

Dynamic computational graph: PyTorch’s dynamic computational graph allows for easy and efficient modeling of complex neural networks. This makes it well-suited for tasks such as natural language processing and computer vision.

User-friendly API: PyTorch has a user-friendly and intuitive API, which makes it easy to implement, debug, and experiment with different models.

Active community: PyTorch has a large and active community, which means that you can find a lot of tutorials, pre-trained models, and other resources to help you get started.

Integration with other libraries: PyTorch has great integration with other libraries such as CUDA, which allows for fast and efficient training of models on GPUs.

Easy to use for distributed training: PyTorch makes it easy to perform distributed training across multiple GPUs and machines, which can significantly speed up training time.

Disadvantages of using PyTorch for deep learning:

Less mature than TensorFlow: PyTorch is relatively new compared to TensorFlow, which means that it may have less mature libraries and fewer resources available.

Less production ready: PyTorch is more suited for research and development work, rather than for production-level deployment.

Less optimized for mobile: PyTorch does not have as good support for mobile deployment as TensorFlow Lite.

Less optimized for browser: PyTorch does not have as good support for browser deployment as TensorFlow.js

Less optimized for Autograd: PyTorch’s Autograd implementation is less optimized than TensorFlow’s, which can make training large models more memory-intensive.

REFERENCES:

Here are a few popular books on the topic of PyTorch and deep learning:

“PyTorch for Deep Learning: From Beginner to Pro” by Ankit Jain: This book provides a comprehensive introduction to PyTorch and deep learning. It covers the basics of PyTorch and deep learning, and provides hands-on examples of implementing various deep learning models using PyTorch.

“Deep Learning with PyTorch” by Eli Stevens, Luca Antiga, Thomas Viehmann: This book provides a hands-on approach to learning deep learning and PyTorch. It covers the basics of deep learning and PyTorch, and provides hands-on examples of implementing various deep learning models using PyTorch.

“Hands-On Machine Learning with PyTorch” by Gaurav Sharma : This book provides a hands-on approach to learning machine learning and PyTorch. It covers the basics of machine learning, deep learning and PyTorch, and provides hands-on examples of implementing various machine learning models using PyTorch.

“Programming PyTorch for Deep Learning” by Ian Pointer: This book provides a hands-on approach to learning deep learning with PyTorch. It covers the basics of PyTorch, deep learning, and provides hands-on examples of implementing various deep learning models using PyTorch.



Next Article
Introduction in deep learning with julia

A

aayushk61466
Improve
Article Tags :
  • AI-ML-DS
  • Deep Learning
  • Machine Learning
  • AI-ML-DS With Python
  • python
  • Python-PyTorch
Practice Tags :
  • Machine Learning
  • python

Similar Reads

  • Introduction in deep learning with julia
    A new transition in Data Science is Julia since it is fast and easy to learn and work with. Julia being a promising language is mainly focused on the scientific computing domain. It provides good execution speed which is comparable to C/C++. It also supports parallelism. Julia is good for writing co
    8 min read
  • Train a Deep Learning Model With Pytorch
    Neural Network is a type of machine learning model inspired by the structure and function of human brain. It consists of layers of interconnected nodes called neurons which process and transmit information. Neural networks are particularly well-suited for tasks such as image and speech recognition,
    6 min read
  • Swish activation function in Pytorch
    Activation functions are a fundamental component of artificial neural networks. They introduce non-linearity into the model, allowing it to learn complex relationships in the data. One such activation function, the Swish activation function, has gained attention for its unique properties and potenti
    6 min read
  • Implement Convolutional Autoencoder in PyTorch with CUDA
    Autoencoders are a type of neural network architecture used for unsupervised learning tasks such as data compression, dimensionality reduction, and data denoising. The architecture consists of two main components: an encoder and a decoder. The encoder portion of the network compresses the input data
    4 min read
  • Start learning PyTorch for Beginners
    Machine Learning helps us to extract meaningful insights from the data. But now, it is capable of mimicking the human brain. This is done using neural networks, which contain the various interconnected layers of nodes containing the data. This data is passed to forward layers. Subsequently, the mode
    15+ min read
  • Distributed Applications with PyTorch
    PyTorch, an open-source machine learning library developed by Facebook's AI Research lab, has become a favorite tool among researchers and developers for its flexibility and ease of use. One of the key features that enable PyTorch to scale efficiently across multiple devices and nodes is its distrib
    6 min read
  • Saving and Loading Weights in PyTorch Lightning
    In Machine learning models, it is important to save and load weights efficiently. This helps us preserve the state of our model during training, so we can resume later without starting from scratch. In this article, we are going to discuss how to save and load weights in PyTorch Lightning. PyTorch L
    8 min read
  • How to implement transfer learning in PyTorch?
    What is Transfer Learning?Transfer learning is a technique in deep learning where a pre-trained model on a large dataset is reused as a starting point for a new task. This approach significantly reduces training time and improves performance, especially when dealing with limited datasets. It is very
    15+ min read
  • Vanishing and Exploding Gradients Problems in Deep Learning
    In the realm of deep learning, the optimization process plays a crucial role in training neural networks. Gradient descent, a fundamental optimization algorithm, can sometimes encounter two common issues: vanishing gradients and exploding gradients. In this article, we will delve into these challeng
    14 min read
  • Deep Learning Interview Questions
    Deep learning is a part of machine learning that is based on the artificial neural network with multiple layers to learn from and make predictions on data. An artificial neural network is based on the structure and working of the Biological neuron which is found in the brain. This Deep Learning Inte
    15+ 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