Learn practical skills, build real-world projects, and advance your career

Introduction to Tensors in PyTorch

Learning about some of the functions related to tensors.

PyTorch is an open source machine learning library based on the Torch library, used for applications such as computer vision and natural language processing. It's use can been found in many of the major ongoing machine learning endeavours in the world, for example: in the self-driving cars of Tesla.

The functions below have been chosen for this assignment after good amount of research and are quite useful as well as easy to understand. So, without wasting our time lets dive right in!

The functions are:

  • torch.argsort
  • torch.eq
  • expand
  • reshape
  • index_add_
# Import torch and other required modules
import torch

Function 1 - torch.argsort

The argsort function is quite an interesting and useful function. It is used to sort the element values in ascending(by default) or descending order. It outputs the index values of the arranged elements after sorting.

torch.argsort(input, dim=-1, descending=False)

The input is the input tensor that has to be sorted. dim is the dimension (called axis in numpy), descending is set to False by default but you can set it to True if you want to the output to be arranged in descending order.

# Example 1 
t = torch.tensor([[1,2],[4,3]])
torch.argsort(t,1)
tensor([[0, 1],
        [1, 0]])

Here we see that the dim is set to 1, so the values are arranged along the row and output of index is written along the row. So, in the first row 1<2 and in the second row 4>3 thus in the output the first row says [0,1] and the second row says [1,0] denoting the indices of the values arranged. As we did not mention "descending" argument, the values are set in ascending order by default.