PyTorch .all()

Anonymous contributor's avatar
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.

  • 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

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): An int or tuple of int values defining the dimension or dimensions to reduce. If None, all dimensions are reduced.
  • keepdim (optional): A bool value that defines if the output tensor retains the reduced dimension. The default value is False.
  • out (optional): The output tensor.

Return value:

  • If dim is not specified, returns a zero-dimensional boolean tensor containing True or False.
  • If dim is 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 torch
tensor = 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 torch
tensor = 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 torch
matrix = torch.tensor([[True, True],
[True, False],
[True, True]])
print(torch.all(matrix, dim=1))

All contributors

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