any

Documentation

treetensor.torch.any(input, *args, reduce=None, **kwargs)[source]

In treetensor, you can get the any result of a whole tree with this function.

Example:

>>> import torch
>>> import treetensor.torch as ttorch
>>> ttorch.any(torch.tensor([False, False]))  # the same as torch.any
tensor(False)

>>> ttorch.any(ttorch.tensor({'a': [True, False], 'b': {'x': [False, False]}}))
tensor(True)

>>> ttorch.any(ttorch.tensor({'a': [False, False], 'b': {'x': [False, False]}}))
tensor(False)

>>> ttorch.any(ttorch.tensor({'a': [True, False], 'b': {'x': [False, False]}}), reduce=False)
<Tensor 0x7fd45b52d518>
├── a --> tensor(True)
└── b --> <Tensor 0x7fd45b52d470>
    └── x --> tensor(False)

>>> ttorch.any(ttorch.tensor({'a': [False, False], 'b': {'x': [False, False]}}), dim=0)
<Tensor 0x7fd45b534128>
├── a --> tensor(False)
└── b --> <Tensor 0x7fd45b534080>
    └── x --> tensor(False)

Torch Version Related

This documentation is based on torch.any in torch v2.4.1+cu121. Its arguments’ arrangements depend on the version of pytorch you installed.

If some arguments listed here are not working properly, please check your pytorch’s version with the following command and find its documentation.

1
python -c 'import torch;print(torch.__version__)'

The arguments and keyword arguments supported in torch v2.4.1+cu121 is listed below.

Description From Torch v2.4.1+cu121

torch.any(input)Tensor
Args:

input (Tensor): the input tensor.

Tests if any element in input evaluates to True.

Note

This function matches the behaviour of NumPy in returning output of dtype bool for all supported dtypes except uint8. For uint8 the dtype of output is uint8 itself.

Example:

>>> a = torch.rand(1, 2).bool()
>>> a
tensor([[False, True]], dtype=torch.bool)
>>> torch.any(a)
tensor(True, dtype=torch.bool)
>>> a = torch.arange(0, 3)
>>> a
tensor([0, 1, 2])
>>> torch.any(a)
tensor(True)
torch.any(input, dim, keepdim=False, *, out=None)Tensor

For each row of input in the given dimension dim, returns True if any element in the row evaluate to True and False otherwise.

If keepdim is True, the output tensor is of the same size as input except in the dimension dim where it is of size 1. Otherwise, dim is squeezed (see torch.squeeze()), resulting in the output tensor having 1 fewer dimension than input.

Args:

input (Tensor): the input tensor. dim (int): the dimension to reduce. keepdim (bool): whether the output tensor has dim retained or not.

Keyword args:

out (Tensor, optional): the output tensor.

Example:

>>> a = torch.randn(4, 2) < 0
>>> a
tensor([[ True,  True],
        [False,  True],
        [ True,  True],
        [False, False]])
>>> torch.any(a, 1)
tensor([ True,  True,  True, False])
>>> torch.any(a, 0)
tensor([True, True])