快捷方式

remove_duplicates

class tensordict.utils.remove_duplicates(input: TensorDictBase, key: NestedKey, dim: int = 0, *, return_indices: bool = False)

在指定的維度上移除 key 中重複的索引。

此方法檢測 key 關聯的張量在指定 dim 上的重複元素,並移除 TensorDict 中所有其他張量中相同索引處的元素。預期 dim 是輸入 TensorDict 的批處理大小維度之一,以確保所有張量的一致性。否則,將引發錯誤。

引數:
  • input (TensorDictBase) – 包含可能重複元素的 TensorDict。

  • key (NestedKey) – 要在其中識別和移除重複元素的張量的鍵。它必須是 TensorDict 中的一個葉子鍵,指向一個張量而不是另一個 TensorDict。

  • dim (int, 可選) – 要在其中識別和移除重複元素的維度。它必須是輸入 TensorDict 的批處理大小維度之一。預設為 0

  • return_indices (bool, 可選) – 如果為 True,還將返回輸入張量中唯一元素的索引。預設為 False

返回:

輸入 tensordict,包含對應於重複元素的索引

在張量 keydim 維度上移除。

unique_indices (torch.Tensor, 可選): 在輸入 tensordict 的指定 key 沿指定 dim 的唯一元素的第一個出現處的索引。

僅當 return_index 為 True 時提供。

返回型別:

output (TensorDictBase)

示例

>>> td = TensorDict(
...     {
...         "tensor1": torch.tensor([[1, 2, 3], [4, 5, 6], [1, 2, 3], [7, 8, 9]]),
...         "tensor2": torch.tensor([[10, 20], [30, 40], [40, 50], [50, 60]]),
...     }
...     batch_size=[4],
... )
>>> output_tensordict = remove_duplicate_elements(td, key="tensor1", dim=0)
>>> expected_output = TensorDict(
...     {
...         "tensor1": torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
...         "tensor2": torch.tensor([[10, 20], [30, 40], [50, 60]]),
...     },
...     batch_size=[3],
... )
>>> assert (td == expected_output).all()

文件

訪問全面的 PyTorch 開發者文件

檢視文件

教程

為初學者和高階開發者提供深入的教程

檢視教程

資源

查詢開發資源並讓您的問題得到解答

檢視資源