Getting started with PyTorch

Getting to grips with PyTorch basics

Navaneeth Dinesh
Analytics Vidhya
4 min readAug 5, 2020

--

PyTorch Logo

Deep Learning and Artificial Intelligence have been one of the hottest topics of 21st century. While there are many out there like TensorFlow, PyTorch has always stood out as one of the best open source machine learning framework for research prototyping. To put it formally, PyTorch is a scientific computing framework with wide support for machine learning algorithms that put GPUs first. It was primarily developed by Facebook’s artificial intelligence research group.

If you are new to PyTorch, this is the prefect blog for you to get started (provided you have a basic understanding of Python) as we will cover the very fundamentals of PyTorch here.

PyTorch and Imperative programming

If you have some background in coding, imperative programming may not be a new concept to you. To distinguish imperative programming from its counterpart symbolic programming we will look at some examples.

The above code indicates Imperative programming paradigm of computer programming where the program itself describes steps that change the state of the computer. Here the computation is defined as we type it.

This code snippet portrays symbolic programming paradigm where the computation is defined first and then executed.

As you might have noticed, PyTorch uses imperative programming paradigm which may be relatively easier to follow.

PyTorch Tensors

To give you an overview of tensors let me use the same explanation as provided in the official documentation page of PyTorch.

A PyTorch Tensor is basically the same as a numpy array: it does not know anything about deep learning or computational graphs or gradients, and is just a generic n-dimensional array to be used for arbitrary numeric computation.

If you did not understand what a numpy array is, don’t worry. It is nothing more than an array data structure but more computationally efficient. PyTorch Tensors are similar and can be used as a replacement for numpy arrays.

To declare a Pytorch tensor first we should import the torch module. After that we can provide a python list as an input to the torch.tensor() method

This would declare an tensor ‘a’ of type ‘torch.LongTensor ’ with a data type ‘torch.int64’. You can verify this by typing the following command:

You can also explicitly define a Tensor of a certain type with the following command:

This would return a ‘torch.FloatTensor’ type. You can learn more about Tensor types and datatypes here.

You can also declare a Tensor with the torch.linspace() method

Tensor dimension and size

A Tensor may be uni-dimensional or multidimensional. To see the number of dimensions in a Tensor we could use the ndimension() method :

Unlike ndimension(), the size() method returns the shape of the tensor :

To reshape a Tensor we can use the view() method and pass the required shape of the tensor as input to the same.

As stated earlier a PyTorch tensor is similar to a numpy array and can be used as a replacement for the same but that doesn’t restrict you from converting a Torch tensor to numpy array and vice versa. To convert a numpy array to a tensor and vice versa you can use the following code.

A pandas series object can also be converted to a torch tensor similarly.

Indexing and Slicing

Indexing and slicing in torch tensors can be done similar to that in regular python lists and numpy arrays. The following code snippet would give you an overview of indexing and slicing in torch tensors

Basic Operations in PyTorch

To perform basic tensor operations like addition, multiplication, and subtraction we can do the following:

For one-dimensional tensors a dot operation can be performed using the following code:

In the above example a dot operation does the following :

1*4 + 2*5 + 3*6 = 32

Two matrices can also be multiplied using the following code:

Derivatives in PyTorch

We can also apply derivatives on functions and yield corresponding results using PyTorch. We can start off by declaring a tensor variable x with requires_grad property of tensor() method set to True

The requires_grad is set to True to store all operations associated with that variable.

Then we define our function, say y=x**2

Now we use the backward() method on y to find the derivative of y with respect to x.

Once the derivative is calculated we then find the value of derivative with respect to x at x = 2 by applying grad on x.

Conclusion

Congratulation, now that you have covered the basics you are all set to dive into the world of Deep Learning with PyTorch. Good Luck.

--

--

Analytics Vidhya
Analytics Vidhya

Published in Analytics Vidhya

Analytics Vidhya is a community of Generative AI and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Navaneeth Dinesh
Navaneeth Dinesh

Written by Navaneeth Dinesh

Artificial Intelligence | Machine learning | Quantum Computing

No responses yet