TensorDictBase.masked_select#
- TensorDictBase.masked_select(mask: Tensor, *, inplace: bool = False) Self#
Returns a tensordict with the elements of the batch where
maskisTrue.The batch dimensions that
maskcovers are flattened into one, of sizemask.sum(), and the other batch dimensions are kept, as fortd[mask]. The data is copied. The result is aTensorDictwithout names.- Parameters:
mask (Tensor) – a boolean mask whose shape is the batch size, or its leading dimensions.
- Keyword Arguments:
inplace (bool, optional) – if
True, the tensordict keeps its identity and keys, its leaves are replaced one by one, and the call returnsself. A lazy stack, or a tensordict that holds one, raises aNotImplementedError: callto_tensordict()first. See In-place operations. Defaults toFalse.- Returns:
The masked tensordict, or
selfwithinplace=True.
Examples
>>> import torch >>> from tensordict import TensorDict >>> td = TensorDict(source={'a': torch.zeros(3, 4)}, ... batch_size=[3]) >>> mask = torch.tensor([True, False, False]) >>> td_mask = td.masked_select(mask) >>> td_mask.get("a") tensor([[0., 0., 0., 0.]]) >>> td.masked_select(mask, inplace=True) is td True >>> td.batch_size torch.Size([1])
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.