The torch.any() function tests if any value in the input tensor evaluates to True (or non-zero for numeric tensors) along a specified dimension or an entire tensor. In other words, it simply checks for any True value in the tensor.

The return value of the .any() method depends on the input tensor. It can return a scalar boolean tensor or a tensor containing boolean values. It works well with both CPU and CUDA tensors.
This method can validate conditions, reduce boolean masks, or simplify global operations.
Syntax
torch.any(input, dim=None, keepdim=False, out=None)
Parameters
Argument | Description |
input (Tensor) | It is an input tensor. It can be a boolean or a numeric. |
dim (int, optional) | It is a dimension to reduce. It will reduce all the dimensions if you don’t specify or None. |
keepdim (int or tuple of ints, optional) | If True, retains reduced dimension with size 1. By default, it is False. |
out (None) |
If you have a pre-allocated output tensor, it stores the result in this tensor using this argument. |
Global checking (Entire tensor)
Let’s define a tensor with 0 and 1 values. Since it will find one True value (1), it will return True. No matter how many other False values are there, if it finds a single True or non-zero value, that’s it.
import torch tensor = torch.tensor([0, 0, 1]) global_checking = torch.any(tensor) print(global_checking) # Output: tensor(True)If every value of a tensor is 0, then it will return a tensor(False).
import torch all_zero_tensor = torch.tensor([0, 0, 0]) global_checking = torch.any(all_zero_tensor) print(global_checking) # Output: tensor(False)
Boolean tensor

If the tensor is boolean, it checks for a True value and if it finds one, it returns True; otherwise, it returns False.
import torch bool_tensor = torch.tensor([True, False, False]) output = torch.any(bool_tensor) print(output) # Output: tensor(True)
If every element of the input tensor is False, it returns False.
import torch bool_tensor = torch.tensor([False, False, False]) output = torch.any(bool_tensor) print(output) # Output: tensor(False)
Reducing along a dimension
We need a 2D tensor or a matrix with a row and a column. Let’s define a matrix, and then we can check along its columns and rows.
Along columns (dim=0)

To test the True values along columns, we need to pass dim=0.
import torch tensor_2d = torch.tensor([[0, 0, 1], [0, 0, 0]]) along_column = torch.any(tensor_2d, dim=0) print(along_column) # Output: tensor([False, False, True])
Only the third column has a value of 1, so it returns the third value of the output tensor as True.
The first and second columns do not contain non-zero values, so it returns False.
Along rows (dim=1)

import torch tensor_2d = torch.tensor([[0, 0, 1], [0, 0, 0]]) along_row = torch.any(tensor_2d, dim=1) print(along_row) # Output: tensor([ True, False])
The first row of the input tensor contains 1 value (a non-zero value), so the first value of the output tensor is True. The second row includes no non-zero value, so it returns False.
Using keepdim
To preserve the dimensions, you need to pass keepdim=True as an argument. That means it retains the reduced dimension as size 1, so the output remains 2D instead of being squeezed into 1D.
import torch tensor_2d = torch.tensor([[0, 0, 1], [0, 0, 0]]) keep_dim_output = torch.any(tensor_2d, dim=1, keepdim=True) print(keep_dim_output) # Output: tensor([[ True], # [False]])
In the above code:
- Row 0: [0, 0, 1] → contains a non-zero value → result: True
- Row 1: [0, 0, 0] → all zero → result: False
With keepdim=True, the results are shaped as [2, 1].
Custom conditions
What if we have a custom condition that needs to be tested in the input tensor? In that case, if a single element satisfies that condition, it will return True. If every element of the tensor fails, it will return False.
import torch simple_tensor = torch.tensor([11, 10, 21, 12]) conditional_output = torch.any(simple_tensor > 18) print(conditional_output) # Output: tensor(True)
In the above code, element 21 is greater than 18, and that’s why it returns True.
Here is the program where every element fails the condition.import torch simple_tensor = torch.tensor([11, 19, 21, 12]) conditional_output = torch.any(simple_tensor > 22) print(conditional_output) # Output: tensor(False)Not a single element of the input tensor is > 22, so it returns False.
Empty tensor
What if your input tensor is empty? Well, in that case, it returns False.
import torch empty_tensor = torch.tensor([]) empty_result = torch.any(empty_tensor) print(empty_result) # Output: tensor(False)
Using an “out” parameter
The “out” argument is helpful when we already have a pre-allocated tensor. We can create a pre-allocated tensor using torch.zeros() or torch.empty() method.
import torch pre_allocated_tensor = torch.empty((), dtype=torch.bool) torch.any(torch.tensor([0, 0, 1]), out=pre_allocated_tensor) print(pre_allocated_tensor) # Output: tensor(True)
We saved the output in the pre_allocated_tensor, as you can see in the above program.
That’s all!