Tensor math logical operations: any() and all() functions

Coming from Torch7 I am missing logical operations such as the any() and all() functions to check whether a Boolean Tensor contains either only zeros or ones (e.g. after another logical operation):

I understand I could probably do something like

a = torch.rand(3,3)
torch.sum(torch.lt(a,1)) / torch.numel(a)

and check whether this is 1 or 0 (result is a byte tensor so this should be integer division), but it feels rather inconvenient.

Is there some equivalent in PyTorch and if not, would it be possible to add these functions?

1 Like

any and all functions are defined on Byte Tensors.

torch.randn(10).byte().any()
6 Likes

thanks for the super quick response. That’s exactly what I need.

Sorry if I didn’t look hard enough and my question was perhaps trivial, but I don’t seem to be able to find it in the documentation neither under tensor, nor where all the other comparison ops are listed in math:

http://pytorch.org/docs/master/torch.html#comparison-ops

it’s not in the docs. we’ll add it (I recently got to know that it’s exposed but we missed the docs on them).

1 Like