Introduction to PyTorch: Features, Structure & Examples

Updated on 23/10/20263,222 Views

Key Highlights

  • PyTorch is an open-source framework used to build and train deep learning models. You can design the model entirely according to you.
  • Its syntax feels like regular Python, so anyone who already knows Python can start writing code without learning a new style.
  • The same code runs on a CPU, a single GPU, or many GPUs, so a project can grow without a rewrite.
  • The hard part of learning PyTorch is the theory. Concepts like loss, gradients, and optimizers take more time to understand.
  • Older concerns about PyTorch’s slower speed, weak deployment, and fewer pre-trained models are not right today. 
  • In this Pytorch tutorial, you will learn Pytorch's current circumstances, its strong highlights, examples, pillars, history and more.

What is Pytorch?

PyTorch is an open-source library for building and training machine learning models, especially neural networks. It started at Facebook’s AI research lab (now Meta) and is written mainly for Python, with the heavy lifting done underneath in fast C++ and CUDA code.

At its core, PyTorch gives you two big things. 

  1. Tensors: These are grids of numbers, like NumPy arrays. They can run on a GPU, which makes the math faster.
  2. Computes Gradients Automatically: Gradients tell a model how to improve as it learns.

Also, the deep learning models are made and prepared utilizing the open-source AI structure Pytorch. Building, preparing, and sending neural networks is simplified by the flexible and easy-to-use interface it gives. Pytorch offers liquid interaction for exploring different avenues regarding elective deep learning structures because of its dynamic computational charts.

Core Pillars of Pytorch

PyTorch has four main pillars, and these pillars are built to close the gap between quick research and real production use.

1. Eager and Graph Modes: Eager mode is the default, built for flexibility and ease of use. Your code runs line by line, as ordinary Python. You can debug with pdb or a simple print.

Graph mode is for production. TorchScript turns your model into a fixed, optimized graph. That graph can run without Python at all. This makes it a good fit for C++ environments.

2. Distributed Training: PyTorch lets you spread work across many GPUs and machines.

  • DP (DataParallel): A simple, single-process tool for multi-GPU training on a single machine.
  • DDP (DistributedDataParallel): The standard choice. It runs one process per GPU. This avoids Python’s GIL bottleneck.
  • FSDP (Fully Sharded Data Parallel): This is built for huge models. It splits parameters, gradients, and optimizer states across workers. So, if a model is too big for one GPU, it can still be trained.

3. Hardware Independence: You write your code once. It runs on many kinds of hardware: NVIDIA (CUDA), Apple Silicon (MPS), Intel (oneDNN), AMD (ROCm), and Google TPUs (XLA).

Switching is often one line, like .to('cuda') or .to('mps').

4. Robust Deployment: A model built in research should reach production without a rewrite.

  • TorchScript: Saves the model in a format that C++ can load. No Python needed.
  • ExecuTorch: Made for on-device AI. It targets phones, wearables, and embedded devices. Its memory footprint is small.

Newer PyTorch versions are moving toward torch.compile and torch.export. ExecuTorch is also the preferred route for edge devices. TorchScript still works, but it is no longer the main focus.

History of Pytorch

In 2016, Facebook's computer-based intelligence Exploration group made PyTorch to replace Light, another deep learning system. The primary goal was to foster an AI and logical processing device that was more versatile and compelling. Because of its dynamic computational organization, which empowers more instinctive code and less complex investigation, PyTorch immediately acquired notoriety.

The prerequisite for a structure that could easily connect with Python, which is widely utilized in the AI field, likewise affected the production of PyTorch. By using PyTorch's deep learning abilities, specialists and engineers can utilize the broad environment of Python modules.

Due to the thorough PyTorch documentation and its intuitive UI, this AI system is currently generally used by the two tenderfoots and experts nearby. Its dynamic computational organization simplifies its ability to try different things, troubleshoot models, and apply novel ideas and strategies.

Moreover, many pre-built layers, activation capabilities, misfortune capabilities, and streamlining methods are accessible in PyTorch. This ensures that researchers and designers have the opportunity to choose the best parts for the specific jobs needing to be done.

Why Use Pytorch?

PyTorch has developed into a top choice for AI and deep learning responsibilities for various reasons.

1. Dynamic Computational Graph

PyTorch builds its computation graph while your code runs. Each operation executes immediately, so the code reads like normal Python. The computational graph makes it easy to follow and debug. Also, you can change the model at any point.

2. Python Integration

Python, which is widely utilized in the AI field, coordinates with PyTorch. Through this mix, designers and specialists can exploit Python’s huge environment of libraries, including SciPy and NumPy, for logical registration and information handling. Because of this, Python code can be coordinated all the more effectively, and advancement can continue all the more rapidly.

3. Rich Tools and Utilities

In PyTorch, you have to create and train the deep learning models with the help of building blocks. It includes ready-made layers, activation functions, loss functions, and optimizers. 

Also, you will get the tools for loading and batching data. No need to write these from scratch, but you can create custom versions if your project requires. 

4. Active Community

A large developer community supports PyTorch. They discuss it on forums, write tutorials, and contribute to the framework. So, if you are stuck in the learning process, you will get the help quickly.

5. Learning Resources and Documentation

The complete PyTorch courses and documentation are available for beginners to start utilizing deep learning. The documentation includes code models that tell the best way to use different elements and philosophies. Also, this simplifies it for software engineers to comprehend the thoughts and start making their own deep learning models.

Structure of Pytorch

Since PyTorch has a modular plan, software engineers can join different modules and layers to make modern deep learning models. The tensor, which looks like a multi-layered exhibit and fills in as the fundamental structure block for estimations, lies at the core of PyTorch.

Take PyTorch's neural network plan as an example. Our PyTorch model can be portrayed as a class that gets from the light. Module class: nn

import torch

import torch.nn as nn

class SimpleNet(nn.Module):

    def __init__(self):

        super(SimpleNet, self)._init_()

The 'nn. Module' class, the essential class for all neural network modules in PyTorch, is utilized to introduce the 'SimpleNet' class. We can exploit the highlights presented by PyTorch's deep learning system because of this legacy.

We can give the components of our neural network plan the '__init__' capability. These parts, frequently referred to as layers, direct specific estimations on the information.

The SimpleNet class is instated in the constructor capability, which likewise acquires the torch's credits and methods.Module class nn. We can determine the various layers of our neural network inside the constructor.

Consider a neural network with two totally associated layers that have ReLU enactment capabilities. Inside the constructor, we can indicate these levels as follows:

        self.fc1 = nn.Linear(in_features, hidden_size)

        self.relu = nn.ReLU()

        self.fc2 = nn.Linear(hidden_size, out_features)

The two contentions 'in_features' and 'out_features' that are shipped off 'nn.Linear' comprise a totally connected layer for this situation.

The quantity of elements that are input into the layer is indicated by the 'in_features' boundary, while the quantity of highlights that are yielded is determined by the 'out_features' boundary.

'fc1' and 'fc2' are two totally connected layers that we have laid out in our PyTorch example. The first gets a contribution with 'in_features' number of aspects and produces a tensor with 'hidden_size' aspects. From that point onward, we use self.relu() to apply the ReLU actuation capability, which makes our model non-direct. The second completely associated layer, or "fc2," which acknowledges input with hidden_size aspects and results with out_features aspects, is where the result from the principal layer is at last moved

The forward pass capability of our SimpleNet class can be carried out once the layers have been proclaimed. The genuine calculations of our neural network are done by means of the forward-pass approach.

    def forward(self, x):

        x = self fc1(x)

        x = self.relu(x)

        x = self.fc2(x)

        return x

In the forward pass method, the info tensor, "x," is initially sent through the principal completely associated layer, "fc1," utilizing the layer's "forward" system. This yields a mediator yield by applying a straight change on the information tensor. From that point forward, we apply the ReLU initiation capability by taking care of the moderate result by means of "self.relu" to actuate non-linearity.

PyTorch Examples

To help you understand how to utilize the system to make neural networks, here are a couple of PyTorch code models.

1. Making a key neutral network design

import torch.nn as nn

class SimpleNet(nn. Module):

    def __init__(self, in_features, hidden_size, out_features):

        super(SimpleNet, self).__init__()

        self.fc1 = nn. Linear(in_features, hidden_size)

        self.relu = nn. ReLU()

        self.fc2 = nn. Linear(hidden_size, out_features)

    def forward(self, x):

        x = self.fc1(x) x = self.relu(x)

        x = self.fc2(x)

        return x

In the above case, PyTorch is utilized to characterize a direct neural network plan. The 'SimpleNet' class has three layers: ' fc1','relu', and 'fc2', and it gets from the base class 'nn. Module'. The class' constructor contains data about these levels.

2. Developing a custom loss capability

import torch.nn as nn

class CustomLoss(nn. Module):

    def __init__(self):

        super(CustomLoss, self).__init__()

    def forward(self, inputs, targets):

        loss = torch. Mean(torch. Pow(inputs - targets, 2))

#Mean squared mistake

        return loss

The next stage is to plan an interesting misfortune capability. The improvement of a class called "CustomLoss" is shown by the model code you provided. This class carries out the 'forward' capability and gets it from the 'nn.Module' base class. This approach utilizes the mean squared blunder computation to determine the misfortune. The 'forward' technique gets the sources of information, focuses on boundaries, and returns the misfortune esteem as the last result.

You have some control over how your neural network measures execution during preparation by defining a special misfortune capability. This empowers you to tailor your organization for specific assignments or objectives.

Advantages of Pytorch

PyTorch gives engineers and specialists various advantages. Coming up next are a few fundamental benefits:

1. Dynamic computational chart: PyTorch utilizes a unique computational diagram that gives manufacturers of confounded neural networks extra adaptability and comfort. The chart is made immediately as your code runs, making trial and error and investigating basic.

2. Pythonic syntax: PyTorch utilizes a Pythonic linguistic structure, which is basic and direct for Python engineers to fathom. It offers simple coordination with extra Python systems and bundles.

3. Basic debugging and visualization: PyTorch empowers you to troubleshoot your code utilizing the implicit Python investigating apparatuses rapidly. Besides, it offers representation devices for your model's exhibition and conduct, including TensorBoardX and Matplotlib.

4. Wide community and active development: Specialists and designers effectively partake in PyTorch's improvement in a wide and dynamic local area. This suggests that you can get an abundance of data, guidance, and help for any issues or requests you may have.

5. Consistent hardware accelerator integration: PyTorch effectively works with equipment gas pedals like GPUs, allowing you to utilize their handling ability to rapidly prepare your models. Because of this, it succeeds at large-scale, deep learning projects that require a great deal of processing power.

6. Automatic differentiation: The programmed separation element of PyTorch empowers you to register the slopes of the boundaries of your neural network regarding a particular misfortune capability. This makes computing and refreshing inclinations during backpropagation more straightforward, which is particularly useful when preparing enormous models with heaps of boundaries.

Disadvantages of Pytorch

Some of the disadvantages of PyTorch include:

1. Steeper learning curve: PyTorch offers a Pythonic syntax structure, yet it likewise requires exhaustive information on neural networks and deep learning standards. This could make it challenging for those simply beginning with AI.

2. Limited support for production deployment: PyTorch is more worried about examination and trial and error than organization in genuine organizations. The climate for conveying PyTorch models at scale isn't generally so evolved as a few different systems, notwithstanding the accessibility of arrangements like TorchServe.

3. Slower processing speed: Assuming we think about PyTorch vs. TensorFlow, the previous system cycles information more leisurely than other deep learning systems, like the last option when working with large datasets. This is on the grounds that it utilizes a powerful computational chart, which offers more prominent adaptability yet can likewise make execution somewhat slower than structures that utilize static diagrams.

Conclusion

PyTorch is a major area of strength for a learning system with various advantages. It is easy to utilize and flexible for convoluted models because of its dynamic computational organization and basic Pythonic language structure. PyTorch is a well-known tool for quick trial and error and prototyping. It is broadly utilized in business, research, and instructional fields.

FAQs

1. Is PyTorch AI or ML?

PyTorch is a machine learning framework. It is not AI itself. It is a tool for building and training models, especially neural networks. Those models are what power AI systems.

2. Is PyTorch free to use?

Yes. PyTorch is open source and free for personal and commercial use. It is governed by the PyTorch Foundation under the Linux Foundation.

3. Is PyTorch still relevant in 2026?

Yes. It is still one of the most widely used deep learning frameworks. Most new research papers use it. Many popular model libraries, such as Hugging Face Transformers, are built around it. It also keeps improving, with tools like torch.compile and ExecuTorch.

4. Is ChatGPT written in PyTorch?

Not entirely, and the full ChatGPT code is not public. But OpenAI has said PyTorch is its main deep learning framework. So, PyTorch is used to build and train models like those behind ChatGPT.

5. What to know before learning PyTorch?

You need basic Python. It also helps to know some NumPy, basic linear algebra, and the idea of gradients. You don’t need to be an expert in any of these. You can pick up the rest as you go.

6. How hard is it to learn PyTorch?

It is moderate. The code feels like normal Python, so the syntax is easy. The harder part is understanding deep learning concepts like loss, gradients, and optimizers. If you know Python, you can build a simple model in a few days. Getting comfortable takes a few weeks.

7. Which is harder, TensorFlow or PyTorch?

Most people find PyTorch easier. It runs like regular Python, so debugging is simple. TensorFlow 2 with Keras is also beginner-friendly, but its wider set of tools can feel heavier. For most learners, PyTorch is the gentler start.

8. Is a GPU required to use PyTorch?

No. PyTorch runs fine on a CPU. That is enough for learning and small models. A GPU becomes important when you train large models, because it makes training much faster. Free options like Google Colab give you GPU access too.

9. How appropriate is PyTorch for production deployment?

Instead of being used for creation sending, PyTorch is to a great extent zeroed in on exploration and trial and error. The climate for conveying PyTorch models at scale isn't really that created of a few different structures, in spite of the fact that there are devices accessible for serving PyTorch models underway.

10. What is the code execution speed of PyTorch compared with competing systems?

PyTorch could run somewhat slower, particularly while managing large datasets. This is on the grounds that it utilizes a unique computational chart, which offers more noteworthy adaptability yet can make execution somewhat slower than systems that utilize static diagrams. However, PyTorch's flexibility and ease of use make it a top choice for some students and developers, and the presentation contrast can't be significant much of the time.

11. What is the accessibility of pre-prepared models in PyTorch?

PyTorch has a more modest client base than structures like TensorFlow, which can mean there are fewer pre-built models that are effectively accessible for specific purposes. However, PyTorch is turning out to be more well-known, and there are numerous pre-prepared models open.

image
Pavan Vadapalli

Author|868 articles published

Pavan Vadapalli is the Director of Engineering , bringing over 18 years of experience in software engineering, technology leadership, and startup innovation. Holding a B.Tech and an MBA from the India....

Discover Success Stories from Our Learners

logo image
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
advertise-arrow

Free Courses

Explore Our Free Software Tutorials

upGrad Learner Support

Talk to our experts. We are available 7 days a week, 10 AM to 7 PM

text

Indian Nationals

text

Foreign Nationals