Torch sort

We will see how to sort all the elements in a PyTorch tensor in this PyTorch tutorial.

PyTorch is an open-source framework available with a Python programming language. We can process the data in PyTorch in the form of a Tensor. Tensor is a multidimensional array that is used to store the data. To use a tensor, we have to import the torch module. To create a tensor, the method used is tensor().

Syntax:

torch.tensor(data)

Where the data is a multi-dimensional array.

Torch.sort()

Torch.sort() in PyTorch is used to sort the elements in a tensor in the ascending order. If the tensor is two-dimensional, it sorts row-wise when we specify 1. And it sorts in column-wise when we specify 0.

Syntax: Row wise: torch.sort(two_demensional_tensor_object,1)

Column wise: torch.sort(two_demensional_tensor_object,0)

Read more: Dan mclaughlin annual salary

Parameter:

  1. Two_demensional_tensor_object is the tensor that has 2 dimensions.
  2. One (1) refers to row-wise sorting and 0 refers to column-wise sorting.

It sorts row-wise by default.

Return: It returns the sorted tensor along with the index positions in the actual tensor.

Example 1:

Let’s create a 2D tensor that has 5 rows and 5 columns. Then, we sort it row-wise without specifying a second parameter.

Output:

We can observe that elements are sorted row-wise in a tensor in ascending order and returned the indices of their positions in the actual tensor.

Example 2:

Let’s create a 2D tensor that has 5 rows and 5 columns. Then, we sort it row-wise by specifying a second parameter as 1.

Output:

Read more: How to make happy cards jail

We can observe that the elements are sorted row-wise in a tensor in ascending order and returned the indices of their positions in the actual tensor.

Example 3:

Let’s create a 2D tensor that has 5 rows and 5 columns. Then, we sort it column-wise by specifying a second parameter as 0.

Output:

We can observe that the elements are sorted column-wise in a tensor in ascending order and returned the indices of their positions in the actual tensor.

Example 4:

Let’s create a 1D tensor that has 5 values. Then, we sort it by using the sort() function.

Output:

We can observe that the elements are sorted in ascending order and returned the indices of their positions in the actual tensor.

Work with CPU

If you want to run a sort() function on the CPU, we have to create a tensor with a cpu() function. This will run on a CPU machine.

Read more: The emulator process for avd was terminated

When we create a tensor, this time, we can use the cpu() function.

Syntax:

torch.tensor(data).cpu()

Example:

Let’s create a 2D tensor that has 5 rows and 5 columns. Then, we sort it row-wise by specifying a second parameter as 1 and sort it column-wise by specifying a second parameter as 0.

Output:

We can observe that the elements are sorted row-wise & column-wise in a tensor in ascending order and returned the indices of their positions in the actual tensor.

Conclusion

In this PyTorch tutorial, we learned how to sort the elements in a tensor in ascending order using the torch.sort() function. If the tensor is two-dimensional, it sorts row-wise when we specify 1 and sorts column-wise when we specify 0. It returns the sorted tensor along with the index positions in the actual tensor.

We learned the different examples along with the cpu() function. The torch.sort() function don’t take any parameter while applying it on the 1D tensor.

Related Posts