isin#
- tensordict.utils.isin(input: TensorDictBase, reference: TensorDictBase, key: NestedKey, dim: int = 0) Tensor#
Returns which elements of
inputalongdimare also inreference.The entry
keyis cut into slices alongdim. An element ofinputis inreferenceif its slice is equal to one of the slices ofreference[key]. The two tensordicts must have the same number of batch dimensions, and the shapes of the two entries must match except alongdim.- Parameters:
input (TensorDictBase) – the tensordict to test.
reference (TensorDictBase) – the tensordict to look in.
key (NestedKey) – the key of the tensor to compare. It must hold a tensor in both tensordicts.
dim (int, optional) – the batch dimension along which to compare. Defaults to
0.
- Returns:
A boolean tensor of length
input.batch_size[dim].- Raises:
KeyError – if
keyis missing or is not a tensor in either tensordict.ValueError – if the numbers of batch dimensions differ, or if
dimis not a batch dimension.
Examples
>>> import torch >>> from tensordict import TensorDict >>> from tensordict.utils import isin >>> td = TensorDict( ... x=torch.tensor([[1, 2], [3, 4], [1, 2], [5, 6]]), ... y=torch.arange(4), ... batch_size=[4], ... ) >>> reference = TensorDict(x=torch.tensor([[1, 2], [5, 6]]), batch_size=[2]) >>> isin(td, reference, key="x") tensor([ True, False, True, True])