unique() can get the zero or more unique elements of a 0D or more D tensor as shown below:
*Memos:
-
unique()
can be called both from torch and a tensor. - The 2nd argument with
torch
or the 1st argument with a tensor issorted
(Default:True
). *Currently settingFalse
is also sorted. - The 3rd argument with
torch
or the 2nd argument with a tensor isreturn_inverse
(Default:False
). - The 4th argument with
torch
or the 3rd argument with a tensor isreturn_counts
(Default:False
) which returns the number of each element of the original tensor. - The 5th argument with
torch
or the 4th argument with a tensor isdim
(Default:None
). - Only zero or more integers, floating-point numbers or boolean values can be used so zero or more complex numbers cannot be used.
import torch
my_tensor = torch.tensor([[[2, 2, 0,], [0, 1, 1]],
[[1, 3, 0], [0, 0, 2]]])
torch.unique(my_tensor)
my_tensor.unique()
# tensor([0, 1, 2, 3])
torch.unique(my_tensor,
sorted=False,
return_inverse=True,
return_counts=True)
my_tensor.unique(sorted=False,
return_inverse=True,
return_counts=True)
# (tensor([0, 1, 2, 3]),
# tensor([[[2, 2, 0], [0, 1, 1]],
# [[1, 3, 0], [0, 0, 2]]]),
# tensor([5, 3, 3, 1]))
torch.unique(my_tensor,
sorted=False,
return_inverse=True,
return_counts=True,
dim=0)
my_tensor.unique(sorted=False,
return_inverse=True,
return_counts=True,
dim=0)
torch.unique(my_tensor,
sorted=False,
return_inverse=True,
return_counts=True,
dim=-3)
my_tensor.unique(sorted=False,
return_inverse=True,
return_counts=True,
dim=-3)
# (tensor([[[1, 3, 0], [0, 0, 2]],
# [[2, 2, 0], [0, 1, 1]]]),
# tensor([1, 0]),
# tensor([1, 1]))
torch.unique(my_tensor,
sorted=False,
return_inverse=True,
return_counts=True,
dim=1)
my_tensor.unique(sorted=False,
return_inverse=True,
return_counts=True,
dim=1)
torch.unique(my_tensor,
sorted=False,
return_inverse=True,
return_counts=True,
dim=-2)
my_tensor.unique(sorted=False,
return_inverse=True,
return_counts=True,
dim=-2)
# (tensor([[[0, 1, 1], [2, 2, 0]],
# [[0, 0, 2], [1, 3, 0]]]),
# tensor([1, 0]),
# tensor([1, 1]))
torch.unique(my_tensor,
sorted=False,
return_inverse=True,
return_counts=True,
dim=2)
my_tensor.unique(sorted=False,
return_inverse=True,
return_counts=True,
dim=2)
torch.unique(my_tensor,
sorted=False,
return_inverse=True,
return_counts=True,
dim=-1)
my_tensor.unique(sorted=False,
return_inverse=True,
return_counts=True,
dim=-1)
# (tensor([[[0, 2, 2], [1, 0, 1]],
# [[0, 1, 3], [2, 0, 0]]]),
# tensor([1, 2, 0]),
# tensor([1, 1, 1]))
my_tensor = torch.tensor([[[2., 2., 0.,], [False, 1., True]],
[[1., 3., 0.], [False, 0., 2.]]])
torch.unique(my_tensor)
my_tensor.unique()
# tensor([0., 1., 2., 3.])
unique_consecutive() can get the zero or more unique elements of a 0D or more D tensor by consecutiveness as shown below:
*Memos:
-
unique_consecutive()
can be called both fromtorch
and a tensor. - The 2nd argument with
torch
or the 1st argument with a tensor isreturn_inverse
(Default:False
) which returns the original tensor. - The 3rd argument with
torch
or the 2nd argument with a tensor isreturn_counts
(Default:False
) which returns the number of each element of the original tensor. - The 4th argument with
torch
or the 3rd argument with a tensor isdim
(Default:None
). -
unique_consecutive()
doesn't havesorted
argument. - Only zero or more integers, floating-point numbers or boolean values can be used so zero or more complex numbers cannot be used.
import torch
my_tensor = torch.tensor([[[2, 2, 0,], [0, 1, 1]],
[[1, 3, 0], [0, 0, 2]]])
torch.unique_consecutive(my_tensor)
my_tensor.unique_consecutive()
# tensor([2, 0, 1, 3, 0, 2])
torch.unique_consecutive(my_tensor,
return_inverse=True,
return_counts=True)
my_tensor.unique_consecutive(return_inverse=True,
return_counts=True)
# (tensor([2, 0, 1, 3, 0, 2]),
# tensor([[[0, 0, 1], [1, 2, 2]],
# [[2, 3, 4], [4, 4, 5]]]),
# tensor([2, 2, 3, 1, 3, 1]))
torch.unique_consecutive(my_tensor,
return_inverse=True,
return_counts=True,
dim=0)
my_tensor.unique_consecutive(return_inverse=True,
return_counts=True,
dim=0)
torch.unique_consecutive(my_tensor,
return_inverse=True,
return_counts=True,
dim=-3)
my_tensor.unique_consecutive(return_inverse=True,
return_counts=True,
dim=-3)
# (tensor([[[2, 2, 0], [0, 1, 1]],
# [[1, 3, 0], [0, 0, 2]]]),
# tensor([0, 1]),
# tensor([1, 1]))
torch.unique_consecutive(my_tensor,
return_inverse=True,
return_counts=True,
dim=1)
my_tensor.unique_consecutive(return_inverse=True,
return_counts=True,
dim=1)
torch.unique_consecutive(my_tensor,
return_inverse=True,
return_counts=True,
dim=-2)
my_tensor.unique_consecutive(return_inverse=True,
return_counts=True,
dim=-2)
# (tensor([[[2, 2, 0], [0, 1, 1]],
# [[1, 3, 0], [0, 0, 2]]]),
# tensor([0, 1]),
# tensor([1, 1]))
torch.unique_consecutive(my_tensor,
return_inverse=True,
return_counts=True,
dim=2)
my_tensor.unique_consecutive(return_inverse=True,
return_counts=True,
dim=2)
torch.unique_consecutive(my_tensor,
return_inverse=True,
return_counts=True,
dim=-1)
my_tensor.unique_consecutive(return_inverse=True,
return_counts=True,
dim=-1)
# (tensor([[[2, 2, 0], [0, 1, 1]],
# [[1, 3, 0], [0, 0, 2]]]),
# tensor([0, 1, 2]),
# tensor([1, 1, 1]))
my_tensor = torch.tensor([[[2., 2., 0.,], [False, 1., True]],
[[1., 3., 0.], [False, 0., 2.]]])
torch.unique_consecutive(my_tensor)
my_tensor.unique_consecutive()
# tensor([2., 0., 1., 3., 0., 2.])
Top comments (0)