PyTorch .all()
Anonymous contributor
Published Feb 19, 2026
The .all() function returns True if all elements in the input tensor evaluate to True, otherwise it returns False. If a dimension is specified, the function performs a logical AND reduction along that dimension.
Non-zero numeric values evaluate as True, and zero evaluates as False.
Syntax
torch.all(input, dim, keepdim=False, *, out=None)
Or alternatively, in tensor method form:
tensor.all(dim=None, keepdim=False)
Parameters:
input: The input tensor.dim(optional): Anintortupleofintvalues defining the dimension or dimensions to reduce. IfNone, all dimensions are reduced.keepdim(optional): Aboolvalue that defines if the output tensor retains the reduced dimension. The default value isFalse.out(optional): The output tensor.
Return value:
- If
dimis not specified, returns a zero-dimensional boolean tensor containingTrueorFalse. - If
dimis specified, returns a tensor reduced along the given dimension(s), containing boolean values.
Example 1: Reducing an Entire Tensor
In this example, torch.all() checks whether all elements in a boolean tensor are True:
import torchtensor = torch.tensor([True, True, False])print(torch.all(tensor))
This produces the following output:
tensor(False)
Example 2: Using Numeric Values
In this example, non-zero values evaluate as True and zero evaluates as False:
import torchtensor = torch.tensor([1, 2, 0])print(torch.all(tensor))
This produces the following output:
tensor(False)
Codebyte Example: Reducing Along a Dimension
In this example, torch.all() checks values along specific dimensions of a 2D tensor:
import torchmatrix = torch.tensor([[True, True],[True, False],[True, True]])print(torch.all(matrix, dim=1))
All contributors
- Anonymous contributor
Learn PyTorch on Codecademy
- Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
- Includes 27 Courses
- With Professional Certification
- Beginner Friendly.95 hours
- Learn how to use PyTorch to build, train, and test artificial neural networks in this course.
- Intermediate.3 hours