# [pytroch series - 11]: pytorch basis - ordering of tensor tensor elements

2021-08-08 15:42:21

## The first 1 Chapter Tensor Operation Overview

### 1.1 summary

PyTorch Provides a large number of tensor operations , Basically, it can be benchmarked Numpy Operation of multidimensional array , To support various complex operations on tensors .

Most of these operations perform the same function operation on each element in the array , And obtain the result sequence of each element function operation , These sequences generate a new array of the same dimension .

https://www.runoob.com/numpy/numpy-linear-algebra.html

1.2 Operational classification

（1） Arithmetic operations ： Add 、 reduce 、 Factor times 、 Coefficient Division

（2） Function operation ：sin,cos

（3） Rounding operation ： Round up 、 Round down

（4） Statistical operation ： Maximum 、 minimum value 、 mean value

（5） Comparison operations ： Greater than , be equal to , Less than 、 Sort

（6） Linear algebra operations ： matrix 、 Point multiplication 、 Cross riding

### 1.3  “in place“ operation

“in place“ The operation is not a specific function operation , But each function has its own “in place“ The version of the operation .

xxx_()： Finish this operation , Directly modifying tensor Own value .

Basically , Each function has its own in place edition .

Such as

torch.cos() =》torch.cos_()

torch.floor() =》torch.floor_()

### 1.5 Environmental preparation

```import numpy as np
import torch

print("Hello World")
print(torch.__version__)
print(torch.cuda.is_available())

1.
2.
3.
4.
5.
6.
```

### 1.6 Overview of sorting operations

（1） sort： Sort

• It can be sorted according to different tensor dimensions , Yes dim Appoint .
• You can sort in ascending or descending order , Yes descending Sort .
• out： Store the tensor of the output result

（2）topk： Before acquisition K Elements .

• k： Number of elements to get
• dim： Appoint dim Direction , Not all the elements
• largest： Take the largest element or the smallest element
• sorted： Whether the vector has been sorted , If not ordered , This function sorts the tensors first .
• out： Store the tensor of the output result

（3）kthvalue： For the first K Minimum

• k： Specifies the sequence number of the element to be obtained
• dim： Specifies the tensor dimension direction of the operation
• out： Store the tensor of the output result

## The first 2 Chapter Code example ：sort（ Sort ）

```#  Sort sort

a = torch.Tensor([[5,4,1],[6,3,2]])
print (" Source data ：")
print (a)
print ('\n')

print(" Default sort ")
print(torch.sort(a))

print("\ndim=1 Sort ")
print(torch.sort(a,dim=1))

print("\ndim=0 Sort ")
print(torch.sort(a,dim=0))

print("\n Member function sort ")
print(a.sort(dim=1, descending = True))
print ("\n Source data ：")
print(a)

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
```
```# Output ：

Source data ：
tensor([[5., 4., 1.],
[6., 3., 2.]])

Default sort
torch.return_types.sort(
values=tensor([[1., 4., 5.],
[2., 3., 6.]]),
indices=tensor([[2, 1, 0],
[2, 1, 0]]))

dim=1 Sort
torch.return_types.sort(
values=tensor([[1., 4., 5.],
[2., 3., 6.]]),
indices=tensor([[2, 1, 0],
[2, 1, 0]]))

dim=0 Sort
torch.return_types.sort(
values=tensor([[5., 3., 1.],
[6., 4., 2.]]),
indices=tensor([[0, 1, 0],
[1, 0, 1]]))

Member function sort
torch.return_types.sort(
values=tensor([[5., 4., 1.],
[6., 3., 2.]]),
indices=tensor([[0, 1, 2],
[0, 1, 2]]))

Source data ：
tensor([[5., 4., 1.],
[6., 3., 2.]])

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
```

remarks ：

• The default is dim = 1 Sort in the direction of
• The default is to sort in ascending order
• After ordering , Data that does not affect the source tensor
• Can pass torch.sort Sort , You can also use the member function of the tensor a.sort Sort

• dim = 1： Sort each row vector
• dim = 1： Sort each column vector

## The first 3 Chapter Code example ：topk（ Before acquisition K It's the best value ）

```# topk

a = torch.Tensor([[2,4,6,12,8,10],[1,1,5,11,11,9]])
print (" Source data ：")
print (a)

print ("\n Default parameters ：")
print(torch.topk(a,2))

print ("\n take K A maximum of ：")
b = torch.topk(a,k=2, largest=True)
print(b)

print ("\n take K Minimum ：")
torch.topk(a,k=2, largest=False, out=b)
print(b)

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
```
``` Output ：

Source data ：
tensor([[ 2.,  4.,  6., 12.,  8., 10.],
[ 1.,  1.,  5., 11., 11.,  9.]])

Default parameters ：
torch.return_types.topk(
values=tensor([[12., 10.],
[11., 11.]]),
indices=tensor([[3, 5],
[3, 4]]))

take K A maximum of ：
torch.return_types.topk(
values=tensor([[12., 10.],
[11., 11.]]),
indices=tensor([[3, 5],
[3, 4]]))

take K Minimum ：
torch.return_types.topk(
values=tensor([[2., 4.],
[1., 1.]]),
indices=tensor([[0, 1],
[0, 1]]))

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
```

remarks ：

• For elements of the same value , Still involved in sorting , Elements with the same value are not excluded .
• The default is k A maximum of
• After the operation , It has no effect on the source tensor data

## The first 4 Chapter Code example ：kthvalue（ Take the first place K Minimum ）

```# kthvalue： Take the first place K A sorted element

a = torch.Tensor([[2,4,6,12,8,0],[3,7,5,11,1,9]])
print (" Source data ：")
print (a)

print ("\n The sorted value of the source data ：")
print (torch.sort(a))

print ("\n The default parameter is no 3 Minimum ：：")
print(torch.kthvalue(a,3))

print ("\n Press dim=1 Take the second direction 3 Minimum ：")
b = torch.kthvalue(a,k=3, dim=1)
print(b)

print ("\n Press dim=0 Take the second direction 1 Minimum ：")
torch.kthvalue(a, k=1, dim=0, out=b)
print(b)

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
```
``` Output ：

Source data ：
tensor([[ 2.,  4.,  6., 12.,  8.,  0.],
[ 3.,  7.,  5., 11.,  1.,  9.]])

The sorted value of the source data ：
torch.return_types.sort(
values=tensor([[ 0.,  2.,  4.,  6.,  8., 12.],
[ 1.,  3.,  5.,  7.,  9., 11.]]),
indices=tensor([[5, 0, 1, 2, 4, 3],
[4, 0, 2, 1, 5, 3]]))

The default parameter is no 3 Minimum ：：
torch.return_types.kthvalue(
values=tensor([4., 5.]),
indices=tensor([1, 2]))

Press dim=1 Take the second direction 3 Minimum ：
torch.return_types.kthvalue(
values=tensor([4., 5.]),
indices=tensor([1, 2]))

Press dim=0 Take the second direction 1 Minimum ：
torch.return_types.kthvalue(
values=tensor([ 2.,  4.,  5., 11.,  1.,  0.]),
indices=tensor([0, 0, 1, 1, 1, 0]))

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
```
``` Output ：

Source data ：
tensor([[ 2.,  4.,  6., 12.,  8.,  0.],
[ 3.,  7.,  5., 11.,  1.,  9.]])

The sorted value of the source data ：
torch.return_types.sort(
values=tensor([[ 0.,  2.,  4.,  6.,  8., 12.],
[ 1.,  3.,  5.,  7.,  9., 11.]]),
indices=tensor([[5, 0, 1, 2, 4, 3],
[4, 0, 2, 1, 5, 3]]))

The default parameter is no 3 Minimum ：：
torch.return_types.kthvalue(
values=tensor([4., 5.]),
indices=tensor([1, 2]))

Press dim=1 Take the second direction 3 Minimum ：
torch.return_types.kthvalue(
values=tensor([4., 5.]),
indices=tensor([1, 2]))

Press dim=0 Take the second direction 1 Minimum ：
torch.return_types.kthvalue(
values=tensor([ 2.,  4.,  5., 11.,  1.,  0.]),
indices=tensor([0, 0, 1, 1, 1, 0]))

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
```

remarks ：

• The default is dim = 1 The direction of , That is, compare from the sequence of row vectors .
• stay dim In the sequence of specified directions , Take the first place K The lowest value after sorting . This means that the operation is to arrange the sequence in ascending order .

https://chowdera.com/2021/08/20210808153459099S.html