Rate this Page

torch.any#

torch.any(input:Tensor,*,out:Optional[Tensor])Tensor#

Tests if any element ininput evaluates toTrue.

Note

This function matches the behaviour of NumPy in returningoutput of dtypebool for all supported dtypes exceptuint8.Foruint8 the dtype of output isuint8 itself.

Parameters

input (Tensor) – the input tensor.

Keyword Arguments

out (Tensor,optional) – the output tensor.

Example:

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

For each row ofinput in the given dimensiondim,returnsTrue if any element in the row evaluate toTrue andFalse otherwise.

Ifkeepdim isTrue, the output tensor is of the same sizeasinput except in the dimension(s)dim where it is of size 1.Otherwise,dim is squeezed (seetorch.squeeze()), resulting in theoutput tensor having 1 (orlen(dim)) fewer dimension(s).

Parameters
  • input (Tensor) – the input tensor.

  • dim (int ortuple ofints,optional) – the dimension or dimensions to reduce.IfNone, all dimensions are reduced.

  • keepdim (bool,optional) – whether the output tensor hasdim retained or not. Default:False.

Keyword Arguments

out (Tensor,optional) – the output tensor.

Example:

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