# What is PyTorch and how to use it

In this Python tutorial, we will learn What is PyTorch in Python and we will also cover different examples related to PyTorch. Moreover, we also will cover the following topics.

• What is PyTorch
• Installation of PyTorch using Pip
• How to install PyTorch using conda
• How to use PyTorch
• Components of PyTorch
• Applications of PyTorch

## What is PyTorch

In this section, we will learn about What is PyTorch and the implementation of PyTorch in Python.

• PyThon is an open-source and free machine learning library. PyTorch is released under the modified BSD license.
• PyTorch is used for computer vision and natural language processing applications.
• Computer vision is defined as a process of how our computer can easily understand and get the significant information of the image.
• PyTorch is used in computer version and it is developed by the Facebook AI research lab and has a C++ interface.

Example:

In this example, we will learn the implementation of the PyTorch.

The PyTorch gives the two important features.

• It provides the n dimension tensor which is similar to numpy.
• It also provides differences in building and training neural network.
``````import numpy as num
import math

X = num.linspace(-math.pi, math.pi, 2500)
y = num.sin(X)

# Randomly initialize weights
i = num.random.randn()
j = num.random.randn()
k = num.random.randn()
l = num.random.randn()

learning_rate = 1e-6
for t in range(2500):
# Forward pass: compute predicted y

y_pred = i + j * X + k * X ** 2 + l * X ** 3

loss = num.square(y_pred - y).sum()
if t % 100 == 99:
print(t, loss)

# Backprop to compute gradients of i, j, k, l with respect to loss
gradient_y_pred = 2.0 * (y_pred - y)
gradient_c = (gradient_y_pred * X ** 2).sum()
gradient_d = (gradient_y_pred * X ** 3).sum()

# Update weights
i -= learning_rate * gradient_a
j -= learning_rate * gradient_b
k -= learning_rate * gradient_c
l -= learning_rate * gradient_d

print(f'Result: y = {i} + {j} X + {k} X^2 + {l} 5^3')``````

In the above code, we will import NumPy and the math library to increase the list of mathematical functions.

• X = num.linspace(-math.pi, math.pi, 2500) is used to create input and output data.
• math.pi function is used to return the value of PI as 3.14.
• loss = num.square(y_pred – y).sum() is used to compute the loss.
• print(t, loss) is used to print the loss.

Output:

In the following output, we can see that the updated weight result is printed on the screen.

Also, check: Tensorflow in Python

## Installation of PyTorch using Pip

In this section, we will learn how to install PyTorch using the Pip command in Python.

PyTorch is used for the computer version and natural language processing application.

Before installing PyTorch we need to check the Pip is installed or not if already installed not need to install it again.

Simply check the version of the Pip using this command.

``pip --version``

After checking the version of the Pip we want to install the latest version of PyTorch.

``pip install torch``

After installing the Pytorch we can check the Pytorch is successfully installing by using the following command.

``pip show torch``

## How to install PyTorch using conda

In this section, we will learn about How to install PyTorch using conda in Python.

• Conda is an open-source environment management system that runs on Windows, Linux, and Mac.

Here we can use the conda command to install PyTorch in windows.

• Firstly we will start the anaconda command prompt to run out conda command.
• After starting the anaconda cmd then activate the conda activate pytorch.
• After that navigate to the folder where you create the folder.
• After arriving at the folder simply type jupyter-Notebook and run the conda command to install pytorch.
``conda install -c pytorch pytorch``

In this section, we will learn about the advantages and disadvantages of PyTorch in Python.

1. PyTorch is a library that is easy to learn and code.
2. It is faster and provides improvement.
3. PyTorch supports both GPU and CPU.
4. It is easy to debug using the debugging tool.
5. It has computational graph support.
6. It supports the cloud platform.

1. PyTorch library is not known by everyone it has few users who use this library.
2. The community of the Pytorch developer is very small compared to the other communities.
3. In PyTorch there is the absence of a monitoring tool like the tensor board.

Read: PyTorch Save Model

## How to use PyTorch

In this section, we will learn about how to use and implement PyTorch in Python.

• PyTorch is an open-source machine learning library is used for computer vision and natural language processing.
• PyTorch is a library used for processing n dimension tensor which is similar to the NumPy.
• Tensor is defined as a fundamental unit of data which can be number, matrix, vector, etc.

For use of the PyTorch library, we will first import the torch module.

``import torch``

After importing the torch module we will create the tensor with a single number.

``````t = torch.tensor(7.)
print(t)
print(t.dtype)``````

After creating the single number as data now we are creating a PyTorch with a 2d tensor.

``````t = torch.tensor([[2., 3, 4],
[4, 6, 7],
[8, 9, 10]])
print(t)`````` Use of PyTorch with 2d tensor

After creating the 2d tensor we will create a PyTorch with a 3d tensor.

``````t = torch.tensor([
[[15. , 16, 17],
[18, 19, 20]],
[[25, 26, 27],
[28, 29, 30]]
])
print(t)`````` Use of PyTorch with 3d tensor

## Components of PyTorch

In this section, we will learn about the component of PyTorch in Python.

In PyTorch, there are a few components that are most important and help the torch in implementation.

The five most important components of PyTorch are:

• Tensor
• Parameters
• Modules
• Variables
• Functions
1. Tensor: Tensor is an n dimensional array which is similar to the numpy and also an fundamental unit of data which can be number,matrix etc.
2. Parameters: It is used when we want parameter as a tensor of some of the components which is not possible when we use varible.Parameter are basicaly enclosed around the variable.
3. Module: It is defined as component which can carry other modules, parameters etc.Module is also known as a base class of all the neural networks.
4. Variables: Variable is defined as component which is enclosed around the tensor for carrying the gradient.It is present under the torch.autograd.Variable.
5. Functions: It is defines as an expression and the relationship between the tewo variables.It has no memory to store any state.

## Applications of PyTorch

In this section, we will learn about the Application of Pytorch in Python.

As we know the PyTorch is used for computer vision and natural language processing applications.

The three most important applications of Pytorch are:

• Computer vision
• Natural Language Processing
• Reinforcement Learning
1. Computer Vision: It is defined as the process of how our computer can easily understand and get significant information from images. The programmer can proceed with images to expand the highly accurate computer version model.
2. Natural Language Processing: It is defined as a process that gives computer the capability to understand text or words as much the same way a human being. It is used to develop a chatbox and language translator.
3. Reinforcement Learning: It is defined as a feedback-based machine learning technique. An agent learns to behave in an environment by performing the actions and seeing the results of actions. It is used to develop robotics for automation.

You may also like to read the following tutorials.

So, in this tutorial, we discussed What is PyTorch, How it is used and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.

• What is PytTrch
• Installation of PyTorch using Pip
• How to install PyTorch using conda