Cyberithub

How to use PyTorch in Python [Complete Tutorial]

Advertisements

PyTorch is a framework of deep learning, and it is a Python machine learning package based on Torch. PyTorch originally created by Meta AI and now a part of the Linux Foundation is a machine learning framework built on the Torch library and used for applications like computer vision and natural language processing. It is open-source software that is available for free under a modified BSD license.

 

Important Features

  • It provides tensor computation with significant acceleration thanks to the Graphics Processing Unit (GPU).
  • It offers a tape-based auto diff system-based Deep Neural Network.
  • It is supported by all major Cloud platforms such as Amazon Web Services (AWS), Google Cloud Platform (GCP), Microsoft Azure, and Alibaba Cloud.
  • It is matured and stable. It is maintained by large community of developers and forums.
  • It also supports a frontend C++ interface to build high performance, low latency and bare metal applications.
  • It supports the Open Neural Network Exchange (ONNX) format natively.

 

How to use PyTorch in Python [Complete Tutorial]

How to use PyTorch in Python [Complete Tutorial]

Also Read: How to use TensorFlow in Python [Complete Tutorial]

PyTorch is a tensor library that has been designed for deep learning on CPUs and GPUs. A wide range of packages in PyTorch are used to implement deep learning techniques. These tools support us with optimization, conversion, and loss estimation, among other things. Let's learn a little bit about these packages.

  • Torch: The torch package includes data structure for multi-dimensional tensors and mathematical operation over these are defined.
  • torch.Tensor: It is a multi-dimensional matrix which contains an element of a single data type.
  • torch.onnx: The ONNX exporter is a trace-based exporter, which means that it operates by executing your model once and exporting the operators which were actually run during this run
  • torch.utils.dlpack :  It will use to decode the Dlpack into tensor.

 

Installation

You must first select your preference before executing the install command. Installation might begin locally or through a cloud partner. The version of PyTorch (1.1) that is currently supported and tested by Stable is suitable for many users, as shown in the figure below. You must select Preview if you want the most recent 1.1 builds but not ones that have been completely tested and maintained (Nightly). To install PyTorch, you have to install python first, and then you have to follow the following steps.

a) In our system, we are installing PyTorch and other required tools by running conda install pytorch torchvision cudatoolkit=9.0 pytorch command on the terminal as shown below.

How to use PyTorch in Python [Complete Tutorial] 2

b) Then we need to type y to confirm that we want to download all packages.

How to use PyTorch in Python [Complete Tutorial] 3

c) It will take some time to download packages so wait until all packages get downloaded.

How to use PyTorch in Python [Complete Tutorial] 4

 

Importing

You can use pytorch in your Python program by importing it using import torch at the beginning of your program as shown below.

How to use PyTorch in Python [Complete Tutorial] 5

 

Tensors

Tensors are also known as matrices. The essential elements of Pytorch are tensors. We can claim that Tensors are the sole foundation of PyTorch. A metrics is the term used in mathematics to describe a rectangular array of numbers. These metrics have the name "ndarray" in the Numpy library. Tensor is the name given to it in PyTorch. An n-dimensional data container is known as a tensor. The 1d-Tensor, 2d-Tensor, 3d-Tensor, and 4d-Tensor in PyTorch, for instance, are vectors, metrics, cubes, and vectors of cubes, respectively.

For example:-

How to use PyTorch in Python [Complete Tutorial] 6

Tensor can be created in three different methods. Each method has the unique way to produce tensor. Those are:-

  • Make an array of PyTorch Tensors.
  • Make a Tensor with a random number and all ones.
  • Tensor from numpy array created

 

PyTorch Tensor as an Array

In this method, we have first to define the array and then pass that array in our Tensor method of the torch as an argument.

How to use PyTorch in Python [Complete Tutorial] 7

The output tensor will be:-

How to use PyTorch in Python [Complete Tutorial] 8

 

Tensor with Random Number

We can use the rand() function to produce a Tensor of random numbers, and the ones() method of the torch to build a Tensor of all ones. One additional torch technique, manual seed with 0 parameters, will be used with rand to produce random numbers.

How to use PyTorch in Python [Complete Tutorial] 9

The output will be:-

How to use PyTorch in Python [Complete Tutorial] 10

First matrix is with all ones and the 2nd one is created by rand() function.

 

Tensor from Numpy Array

We must first generate a numpy array in order to create a Tensor from it. You must provide your numpy array as an argument to from numpy() once it has been formed. The numpy array is converted to a Tensor using this method. Let’s see how we can do this:-

How to use PyTorch in Python [Complete Tutorial] 11

The generated tensor as output will be:-

How to use PyTorch in Python [Complete Tutorial] 12

 

Tensors Data Types

There are five different types of Tensor data types:-

  • IntTensor
  • FloatTensor
  • DoubleTensor
  • LongTensor
  • HalfTensor

 

 

Tensors Operations

a) Resizing a Tensor

Our first job is to reshape(). It first introduced in version 0.4. It actually gives a tensor with the same data as the input but a different shape. When we use torch.reshape(), the resulting tensor could be a new tensor or a view of the original tensor. We will create our first tensor(random).

How to use PyTorch in Python [Complete Tutorial] 13

The output will show the matrix as output.

How to use PyTorch in Python [Complete Tutorial] 14

Let’s now reshape random generated tensor.

How to use PyTorch in Python [Complete Tutorial] 15

Our new reshaped matrix with 6 rows and 2 columns will be:-

How to use PyTorch in Python [Complete Tutorial] 16

You might have noticed that we can specify row x column shape as we want. You can also see that product of all the shapes needs to be the same as the total number of elements in the tensor.

How to use PyTorch in Python [Complete Tutorial] 17

The output will be:-

How to use PyTorch in Python [Complete Tutorial] 18

In the above snapshot, the rank of the tensor is increased to 4. There are 3 channels, where each row has 4 columns.

 

 

b) Multiplication

The function torch.mul() performs the element-wise multiplication of two tensors. Let’s define new two tensors first.

Our first tensor is:-

How to use PyTorch in Python [Complete Tutorial] 19

The output will be:-

How to use PyTorch in Python [Complete Tutorial] 20

Our second tensor is:-

How to use PyTorch in Python [Complete Tutorial] 21

The output will be:-

How to use PyTorch in Python [Complete Tutorial] 22

Finally, let’s multiply two tensors:-

How to use PyTorch in Python [Complete Tutorial] 23

The output matrix or tensor will be:-

How to use PyTorch in Python [Complete Tutorial] 24

 

c) Addition

We can also add two tensors. Let’s add a and b tensors.

How to use PyTorch in Python [Complete Tutorial] 25

A and B are same matrices we used in multiplications. You can use any tensors or matrices.

 

d) Mean and Standard deviation

One or more dimensions can be used to compute the tensor's standard deviation. We must first determine the mean in our mathematical computation before applying the next formula to the mean-containing data.

How to use PyTorch in Python [Complete Tutorial] 26

But in Tensor, we can use Tensor.mean() and Tensor.std() to find the deviation and mean of the given Tensor. Let see an example of how it performed.

How to use PyTorch in Python [Complete Tutorial] 27

The output matrix with standard deviation is:-

How to use PyTorch in Python [Complete Tutorial] 28

We can calculate mean as well by using torch as well. Let see an example of how it performed.

How to use PyTorch in Python [Complete Tutorial] 29

The output matrix with mean is:-

How to use PyTorch in Python [Complete Tutorial] 30

 

e) Variables and Gradient

autograd Variable is the package's main class. Wrapping a Tensor is its main task. Nearly all of the operations defined on it are supported. Calling backward() will only compute the gradient once your computation is complete. The row Tensor can be accessed via the data property, and the gradient for this variable is gathered into .grad.

In Deep learning, gradient calculation is the key point. Variables are used to calculate the gradient in PyTorch. In simple words, variables are just a wrapper around Tensors with gradient calculation functionality. Below is the python code which is used to manage variables.

How to use PyTorch in Python [Complete Tutorial] 31

The output will be:-

How to use PyTorch in Python [Complete Tutorial] 32

Let see how we can calculate the gradient in PyTorch.

How to use PyTorch in Python [Complete Tutorial] 33

The gradient of tensor x will be :-

How to use PyTorch in Python [Complete Tutorial] 34

 

Conclusion

In this PyTorch lesson, We learned about pytorch introduction , installation and importing . We have also reviewed basics and main features of pytorch .we have also seen what are data types of tensors and its main  operations that are used mainly. we learned about the std() function and how to apply it on a tensor to return the standard deviation across the columns and rows.

Leave a Comment