Five PyTorch Tensors to Know About

Kunal Singh
5 min readJun 13, 2021

PyTorch library is one of the most sought skill when it comes to Data Science and as it is Python library which is mostly used when it comes to Deep learning. As it is one of the part of Machine learning it is widely used library.

now let’s talk about “why not use Numpy in place of PyTorch”?

For Deep learning, we would need to compute the derivative of elements of the data and only PyTorch has the ability to compute derivatives automatically, which id not possible in Numpy it is called Auto grad. PyTorch is GPU based i.e Graphical Processing Unit which helps in faster execution of models with tens of thousands of Data.

so in this lets check out some important tensor functions which helps user in Deep learning models.

  • torch.from_numpy()
  • torch.linspace()
  • torch.eye()
  • torch.cat()
  • torch.unbind()

Before we begin, let’s install and import PyTorch:

Function 1 — torch.from_numpy()

As you know a lot of times when we get our data it is generally in the form of Numpy and in order to process it for Deep learning models it need to be changed in PyTorch so this function is the starting point of a model which is quite important.

It provides support for the conversion of a Numpy array into a tensor in PyTorch. it expects the input as a Numpy array. The output type is tensor. The returned tensor and the ndarray share the same memory.

so, from the above example we can say the Numpy array is successfully converted to a PyTorch Tensor. It is also given by seeing their data-types.

Firstly we took those Numpy arrays which contained (weight) and (temperature) of fruits which are to be imported. we converted the elements of the tuple to a tensor by for loop, and after seeing the datatype we can confirm that it has changed into tensor.

String elements cannot be converted to a tensor.

So, after using multiple example we can say its extremely useful for people who are using numpy array and after that need to change it into PyTorch tensor. It also demonstrate interoperability of Numpy array to what of Deep learning PyTorch Tensor.

Function 2 — torch.linespace()

In this function values are evenly spaced from the start to end(including both numbers).

It is generally used to create a 1D equally spaced tensor between the values start and end. We can also specify the size of the tensor with the steps parameter. Default is steps=100.

In the above example we can interpret that user has taken two values i.e. start=2, end=50 and as user hasn’t mentioned any number of steps so the value is 100 by default.

In the above example what user has done is that he took two values i.e. start=5 and end=40 and this time he also specified the number of steps too. and hence the result is correct.

so, in this particular instance what user has done is that he has tried to take steps in the form of upon power of 10 and as steps can only be taken in the form of int not float.

torch.linespace() is a particular function which has a variety of use in a script and has a above meaning which is used in deep neural network and natural language processing purpose.

Function 3 — torch.eye()

torch.eye() returns a 2D tensor with the values of diagonals as 1 and other values as 0.

The function expects two parameters i.e. n and m. If m is not defined then it returns a 2D tensor of size n x n.

In the above example we can see user has used torch.eye() function with two parameters as generally required and we have got a 2D tensor.

In the above example what user has done is that he did not take the m parameter rather he only took n parameter and represented the 2D tensor and yet he got the result.

so in the above example what user has done is that he has taken the combination of arguments wrong and this has lead to an error.

torch.eye() is one of the required library function for a user in order to tackle advance problems and it helps user to write the code in a better manner.

Function 4 — torch.cat()

torch.cat() concatenates a sequence of tensors over the specified dimensions. All tensors must be of the same shape(except in the concatenating dimension).

In the above example we can interpret that user has taken two tensors in the form of zeros and ones and he concatenated them with the help of torch.cat() function and we can see the result.

In the example user has taken his set of tensor using another function called torch.full() and then he concatenated the two tensors.

Here what happened is that user took two tensors and tried to concatenate them using dimension as 1 but as user tensors doesn’t have similar size dimensions it didn’t got concatenated.

torch.cat() function is one of the important PyTorch function which is generally used to concatenate different classes or different dimensions in a program in order to solve problems much faster.

Function 5 — tensor.unbind()

tensor.unbind() function removes a tensor dimension and returns slices of tensors and generally two arguments are taken:

In the above example what user did that he took a tensor with the help of torch.eye() function and then after that he printed it and after that he used torch.unbind() function and we can see the result, tensor is unbinded.

In example 2 what user has done is that he took a tensor with the help of torch.linspace() function and after that he unbinded it with the help of torch.unbind() function and we got the result and this sliced data can be used for further processing.

what user did in the above example that he created a 2D tensor and then the next step he tried to unbind the tensor with the dimension as 1 as it gave an error saying “Dimension out of range”.

This torch.unbind() function is one of the powerful PyTorch function present which can be used to slice the particular data when required.

Conclusion

This completes our survey to look at some important PyTorch functions which can be used in any set of codes or Deep learning models. We took some basic functions and some advance ones too in order to understand their specific uses and how they can help a user to formulate better code. PyTorch provides hundreds of such functions to make job much easier. This was just a tip of the ice berg, there are many such functions present in PyTorch documentation, do check them out.

Reference Links

Provide links to your references and other interesting articles about tensors

Deep learning is a vast subject with multiple possibilities it only needs right mind to understand its worth. Happy Learning.

--

--

Kunal Singh

I'm a Data Science aspirant and currently working in the field of Data Science and i sleep best when i work the Data in the best way possible.