sort() can get two of the 0D or more D tensors of zero or more sorted elements and their indices in ascending(Default) or descending order from the 0D or more D tensor of zero or more elements as shown below:
*Memos:
-
sort()can be used with torch or a tensor. - The 1st argument(
input) withtorchor using a tensor(Required-Type:tensorofint,floatorbool). *The 0D tensor of a complex number can be used. - The 2nd argument with
torchor the 1st argument with a tensor isdim(Optional-Defualt:-1-Type:int). - The 3rd argument with
torchor the 2nd argument with a tensor isdescending(Optional-Default:False-Type:bool). *Falseis ascending order andTrueis descending order. - The 4th argument with
torchor the 3rd argument with a tensor isstable(Optional-Default:False-Type:bool). *Memos:-
Truecan definitely sort the multiple same values whileFalsecannot but evenFalsebasically can sort the multiple same values. - You must use
stable=. *Settingstable,dimanddescendingalso needsdim=anddescending=respectively.
-
- There is
outargument withtorch(Optional-Default:None-Type:tuple(tensor,tensor) orlist(tensor,tensor)): *Memos:-
out=must be used. -
My post explains
outargument.
-
import torch
my_tensor = torch.tensor([7, 1, -5, 7, 9, -3, 0, -3])
torch.sort(my_tensor)
my_tensor.sort()
torch.sort(input=my_tensor, dim=0, descending=False, stable=False)
torch.sort(input=my_tensor, dim=-1, descending=False, stable=False)
torch.sort(input=my_tensor, dim=0, descending=False, stable=True)
torch.sort(input=my_tensor, dim=-1, descending=False, stable=True)
# torch.return_types.sort(
# values=tensor([-5, -3, -3, 0, 1, 7, 7, 9]),
# indices=tensor([2, 5, 7, 6, 1, 0, 3, 4]))
torch.sort(input=my_tensor, dim=0, descending=True, stable=False)
torch.sort(input=my_tensor, dim=-1, descending=True, stable=False)
# torch.return_types.sort(
# values=tensor([9, 7, 7, 1, 0, -3, -3, -5]),
# indices=tensor([4, 0, 3, 1, 6, 5, 7, 2]))
my_tensor = torch.tensor([[7, 1, -5, 7], [9, -3, 0, -3]])
torch.sort(input=my_tensor)
torch.sort(input=my_tensor, dim=-1, descending=False, stable=False)
torch.sort(input=my_tensor, dim=1, descending=False, stable=False)
torch.sort(input=my_tensor, dim=-1, descending=False, stable=True)
torch.sort(input=my_tensor, dim=1, descending=False, stable=True)
# torch.return_types.sort(
# values=tensor([[-5, 1, 7, 7], [-3, -3, 0, 9]]),
# indices=tensor([[2, 1, 0, 3], [1, 3, 2, 0]]))
torch.sort(input=my_tensor, dim=0, descending=False, stable=False)
torch.sort(input=my_tensor, dim=-2, descending=False, stable=False)
torch.sort(input=my_tensor, dim=0, descending=False, stable=True)
torch.sort(input=my_tensor, dim=-2, descending=False, stable=True)
# torch.return_types.sort(
# values=tensor([[7, -3, -5, -3], [9, 1, 0, 7]]),
# indices=tensor([[0, 1, 0, 1], [1, 0, 1, 0]]))
my_tensor = torch.tensor([[7., 1, -5., 7.], [9., -3., 0, -3.]])
torch.sort(input=my_tensor)
# torch.return_types.sort(
# values=tensor([[-5., 1., 7., 7.], [-3., -3., 0., 9.]]),
# indices=tensor([[2, 1, 0, 3], [1, 3, 2, 0]]))
my_tensor = torch.tensor([[True, False, True, False],
[False, True, False, True]])
torch.sort(input=my_tensor)
# torch.return_types.sort(
# values=tensor([[False, False, True, True],
# [False, False, True, True]]),
# indices=tensor([[1, 3, 0, 2],
# [0, 2, 1, 3]]))
my_tensor = torch.tensor(7.+0.j)
torch.sort(input=my_tensor)
# torch.return_types.sort(
# values=tensor(7.+0.j),
# indices=tensor(0))
argsort() can get the 0D or more D tensor of zero or more sorted elements' indices in ascending(Default) or descending order from the 0D or more D tensor of zero or more elements as shown below:
*Memos:
-
argsort()can be used withtorchor a tensor. - The 1st argument(
input) withtorchor using a tensor(Required-Type:tensorofint,floatorbool). *The 0D tensor of a complex number can be used. - The 2nd argument with
torchor the 1st argument with a tensor isdim(Optional-Defualt:-1Type:int). - The 3rd argument with
torchor the 2nd argument with a tensor isdescending(Optional-Default:False-Type:bool). *Falseis ascending order andTrueis descending order. - The 4th argument with
torchor the 3rd argument with a tensor isstable(Optional-Default:False-Type:bool). *Memos:-
Truecan definitely sort the multiple same values whileFalsecannot but evenFalsecan basically sort the multiple same values. - You must use
stable=. *When settingstable,dimanddescendingalso needdim=anddescending=respectively.
-
import torch
my_tensor = torch.tensor([7, 1, -5, 7, 9, -3, 0, -3])
torch.argsort(input=my_tensor)
my_tensor.argsort()
torch.argsort(input=my_tensor, dim=0, descending=False, stable=False)
torch.argsort(input=my_tensor, dim=-1, descending=False, stable=False)
torch.argsort(input=my_tensor, dim=0, descending=False, stable=True)
torch.argsort(input=my_tensor, dim=-1, descending=False, stable=True)
# tensor([2, 5, 7, 6, 1, 0, 3, 4])
torch.argsort(input=my_tensor, dim=0, descending=True, stable=False)
torch.argsort(input=my_tensor, dim=-1, descending=True, stable=False)
# tensor([4, 0, 3, 1, 6, 5, 7, 2])
my_tensor = torch.tensor([[7, 1, -5, 7], [9, -3, 0, -3]])
torch.argsort(input=my_tensor)
torch.argsort(input=my_tensor, dim=-1, descending=False, stable=False)
torch.argsort(input=my_tensor, dim=1, descending=False, stable=False)
torch.argsort(input=my_tensor, dim=-1, descending=False, stable=True)
torch.argsort(input=my_tensor, dim=1, descending=False, stable=True)
# tensor([[2, 1, 0, 3], [1, 3, 2, 0]])
torch.argsort(input=my_tensor, dim=0, descending=False, stable=False)
torch.argsort(input=my_tensor, dim=-2, descending=False, stable=False)
torch.argsort(input=my_tensor, dim=0, descending=False, stable=True)
torch.argsort(input=my_tensor, dim=-2, descending=False, stable=True)
# tensor([[0, 1, 0, 1], [1, 0, 1, 0]])
my_tensor = torch.tensor([[7., 1., -5., 7.], [9., -3., 0., -3.]])
torch.argsort(input=my_tensor)
# tensor([[2, 1, 0, 3], [1, 3, 2, 0]])
my_tensor = torch.tensor([[True, False, True, False],
[False, True, False, True]])
torch.argsort(input=my_tensor)
# tensor([[1, 3, 0, 2],
# [0, 2, 1, 3]])
my_tensor = torch.tensor(7.+0.j)
torch.argsort(input=my_tensor)
# tensor(0)
msort() can get the 0D or more D tensor of zero or more sorted elements in ascending order from the 0D or more D tensor of zero or more elements as shown below:
*Memos:
-
msort()can be used withtorchor a tensor. - The 1st argument(
input) withtorchor using a tensor(Required-Type:tensorofint,floatorbool). *The 0D tensor of a complex number can be used. -
msort()doesn't havedim,descendingandstableargument. -
msort()is equivalent tosort()[0]withdim=0. - There is
outargument withtorch(Optional-Default:None-Type:tuple(tensor,tensor) orlist(tensor,tensor)): *Memos:-
out=must be used. -
My post explains
outargument.
-
import torch
my_tensor = torch.tensor([7, 1, -5, 7, 9, -3, 0, -3])
torch.msort(input=my_tensor)
my_tensor.msort()
# tensor([-5, -3, -3, 0, 1, 7, 7, 9])
my_tensor = torch.tensor([[7, 1, -5, 7], [9, -3, 0, -3]])
torch.msort(input=my_tensor)
# tensor([[7, -3, -5, -3], [9, 1, 0, 7]])
my_tensor = torch.tensor([[7., 1., -5., 7.], [9., -3., 0., -3.]])
torch.msort(input=my_tensor)
# tensor([[7., -3., -5., -3.], [9., 1., 0., 7.]])
my_tensor = torch.tensor([[True, False, True, False],
[False, True, False, True]])
torch.msort(input=my_tensor)
# tensor([[False, False, False, False],
# [True, True, True, True]])
my_tensor = torch.tensor(7.+5.j)
torch.msort(input=my_tensor)
# tensor(7.+5.j)
Top comments (0)