快捷方式

isin

class tensordict.utils.isin(input: TensorDictBase, reference: TensorDictBase, key: NestedKey, dim: int = 0)

測試 input 中 keydim 維度上的每個元素是否存在於 reference 中。

此函式返回一個布林張量,其長度為 input.batch_size[dim],對於 key 入口中也存在於 reference 中的元素,其值為 True。此函式假定 inputreference 具有相同的批處理大小幷包含指定的入口,否則將引發錯誤。

引數:
  • input (TensorDictBase) – 輸入 TensorDict。

  • reference (TensorDictBase) – 用於測試的目標 TensorDict。

  • key (Nestedkey) – 要測試的鍵。

  • dim (int, 可選) – 要測試的維度。預設為 0

返回值:

一個布林張量,其長度為 input.batch_size[dim],對於位於

input key 張量中且也存在於 reference 中的元素,其值為 True

返回型別:

out (Tensor)

示例

>>> 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],
... )
>>> td_ref = TensorDict(
...     {
...         "tensor1": torch.tensor([[1, 2, 3], [4, 5, 6], [10, 11, 12]]),
...         "tensor2": torch.tensor([[10, 20], [30, 40], [50, 60]]),
...     },
...     batch_size=[3],
... )
>>> in_reference = isin(td, td_ref, key="tensor1")
>>> expected_in_reference = torch.tensor([True, True, True, False])
>>> torch.testing.assert_close(in_reference, expected_in_reference)

© 版權所有 2022, Meta。

使用 Sphinx 構建,主題由 Read the Docs 提供。

文件

訪問 PyTorch 的全面開發者文件

檢視文件

教程

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

檢視教程

資源

查詢開發資源並解答你的問題

檢視資源