Rate this Page
★ ★ ★ ★ ★

TensorDictBase.all#

TensorDictBase.all(dim: int | None = None) → bool | TensorCollection#

Returns whether every element of every leaf is True (nonzero).

Parameters:

dim (int, optional) – a batch dimension to reduce. If given, returns a new tensordict of boolean leaves without that dimension. A negative dim counts from the end of the batch dimensions. Defaults to None, which checks every element and returns a bool.

Returns:

A bool, or a new tensordict if dim is given.

Raises:

RuntimeError – if dim is not a batch dimension.

Examples

>>> import torch
>>> from tensordict import TensorDict
>>> td = TensorDict(a=torch.tensor([[True, False], [True, True]]), batch_size=[2, 2])
>>> td.all()
False
>>> td.all(dim=1)["a"]
tensor([False,  True])

Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.