快捷方式

remove_duplicates

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

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

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

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

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

  • dim (int, optional) – 用於識別和移除重複元素的維度。它必須是輸入 TensorDict 批處理大小中的一個維度。預設為 0

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

返回:

輸入 tensordict,其中對應於張量 key

在維度 dim 上的重複元素的索引已移除。

unique_indices (torch.Tensor, optional): 輸入 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 的完整開發者文件

檢視文件

教程

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

檢視教程

資源

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

檢視資源