Search code examples
pythonpytorchtensorany

How to select multiple dimensions in torch.any?


I have a (BxHxW) tensor with boolean values which makes sense of a semantic map in a semantic segmentation problem . For each semantic map in the batch, I need to understand whether this map contains some False or not.

I used torch.any(input, dim=(1, 2)), so that in the end there was one dimension left - the size of the butch. However, dim can only be int.

How to make such a check in the most concise way and what is the reason for the absence of such a seemingly simple extension of the method.


Solution

  • You can use:

    torch.any(input.view(input.shape[0], -1), dim=1)