torch.unique

torch.unique(input, sorted=True, return_inverse=False, return_counts=False, dim=None) Tuple[Tensor,Tensor,Tensor]

返回输入张量中的唯一元素。

注意

此函数与torch.unique_consecutive()的不同之处在于,它还会消除非连续的重复值。

注意

目前,在 CUDA 实现和 CPU 实现中,torch.unique 总是在开始时对张量进行排序,无论 sort 参数如何设置。由于排序可能会很慢,因此如果输入张量已经是有序的,则建议使用torch.unique_consecutive() 来避免排序。

参数
  • 输入 (Tensor) – 输入的张量

  • sorted (bool) – 是否在返回结果前将唯一的元素按升序排列。

  • return_inverse (bool) – 是否还返回原始输入中各元素在返回的唯一列表中的位置索引。

  • return_counts (bool) – 是否为每个唯一的元素返回其计数值。

  • dim (int, optional) – 操作的维度。如果为 None,则返回展平输入的独特值;否则,给定维度索引的每个张量都将被视为应用唯一操作的一个元素。更多详情请参见示例。默认值: None

返回值

包含一个张量或张量元组

  • 输出 (Tensor):包含唯一标量元素的输出列表。

  • inverse_indices (Tensor):(可选)如果 return_inverse 为 True,则会额外返回一个与输入形状相同的张量,表示原始输入中的元素在输出中的位置;否则,此函数只会返回一个张量。

  • counts (Tensor):(可选)当 return_counts 为 True 时,会额外返回一个与输出形状相同的张量(如果指定了 dim,则为 output.size(dim)),表示每个唯一值或张量的出现次数。

返回类型

(Tensor, Tensor (可选), Tensor (可选))

示例:

>>> output = torch.unique(torch.tensor([1, 3, 2, 3], dtype=torch.long))
>>> output
tensor([1, 2, 3])

>>> output, inverse_indices = torch.unique(
...     torch.tensor([1, 3, 2, 3], dtype=torch.long), sorted=True, return_inverse=True)
>>> output
tensor([1, 2, 3])
>>> inverse_indices
tensor([0, 2, 1, 2])

>>> output, inverse_indices = torch.unique(
...     torch.tensor([[1, 3], [2, 3]], dtype=torch.long), sorted=True, return_inverse=True)
>>> output
tensor([1, 2, 3])
>>> inverse_indices
tensor([[0, 2],
        [1, 2]])

>>> a = torch.tensor([
...     [
...         [1, 1, 0, 0],
...         [1, 1, 0, 0],
...         [0, 0, 1, 1],
...     ],
...     [
...         [0, 0, 1, 1],
...         [0, 0, 1, 1],
...         [1, 1, 1, 1],
...     ],
...     [
...         [1, 1, 0, 0],
...         [1, 1, 0, 0],
...         [0, 0, 1, 1],
...     ],
... ])

>>> # If we call `torch.unique(a, dim=0)`, each of the tensors `a[idx, :, :]`
>>> # will be compared. We can see that `a[0, :, :]` and `a[2, :, :]` match
>>> # each other, so one of them will be removed.
>>> (a[0, :, :] == a[2, :, :]).all()
tensor(True)
>>> a_unique_dim0 = torch.unique(a, dim=0)
>>> a_unique_dim0
tensor([[[0, 0, 1, 1],
         [0, 0, 1, 1],
         [1, 1, 1, 1]],
        [[1, 1, 0, 0],
         [1, 1, 0, 0],
         [0, 0, 1, 1]]])

>>> # Notice which sub-tensors from `a` match with the sub-tensors from
>>> # `a_unique_dim0`:
>>> (a_unique_dim0[0, :, :] == a[1, :, :]).all()
tensor(True)
>>> (a_unique_dim0[1, :, :] == a[0, :, :]).all()
tensor(True)

>>> # For `torch.unique(a, dim=1)`, each of the tensors `a[:, idx, :]` are
>>> # compared. `a[:, 0, :]` and `a[:, 1, :]` match each other, so one of
>>> # them will be removed.
>>> (a[:, 0, :] == a[:, 1, :]).all()
tensor(True)
>>> torch.unique(a, dim=1)
tensor([[[0, 0, 1, 1],
         [1, 1, 0, 0]],
        [[1, 1, 1, 1],
         [0, 0, 1, 1]],
        [[0, 0, 1, 1],
         [1, 1, 0, 0]]])

>>> # For `torch.unique(a, dim=2)`, the tensors `a[:, :, idx]` are compared.
>>> # `a[:, :, 0]` and `a[:, :, 1]` match each other. Also, `a[:, :, 2]` and
>>> # `a[:, :, 3]` match each other as well. So in this case, two of the
>>> # sub-tensors will be removed.
>>> (a[:, :, 0] == a[:, :, 1]).all()
tensor(True)
>>> (a[:, :, 2] == a[:, :, 3]).all()
tensor(True)
>>> torch.unique(a, dim=2)
tensor([[[0, 1],
         [0, 1],
         [1, 0]],
        [[1, 0],
         [1, 0],
         [1, 1]],
        [[0, 1],
         [0, 1],
         [1, 0]]])
本页目录