快捷方式

torch.unique

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

返回輸入張量的唯一元素。

注意

此函式與 torch.unique_consecutive() 不同,因為它還會消除非連續的重複值。

注意

當前在 CUDA 實現和 CPU 實現中,torch.unique 無論 sort 引數為何,總是在開始時對張量進行排序。排序可能很慢,因此如果您的輸入張量已經排序,建議使用 torch.unique_consecutive() 以避免排序。

引數
  • input (Tensor) – 輸入張量

  • sorted (bool) – 是否在返回輸出之前按升序對唯一元素進行排序。

  • return_inverse (bool) – 是否也返回原始輸入中元素在返回的唯一列表中的索引。

  • return_counts (bool) – 是否也返回每個唯一元素的計數。

  • dim (int, 可選) – 操作維度。如果為 None,則返回展平輸入的唯一元素。否則,按給定維度索引的每個張量都將被視為應用 unique 操作的一個元素。詳情請參閱示例。預設值:None

返回

包含以下內容的張量或張量元組:

  • output (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]]])

文件

查閱 PyTorch 的全面開發者文件

檢視文件

教程

獲取面向初學者和高階開發者的深入教程

檢視教程

資源

查詢開發資源並獲得問題解答

檢視資源