PyTorch .logical_and()

iydig's avatar
Published Oct 30, 2025
Contribute to Docs

The torch.logical_and() function in PyTorch performs an element-wise logical AND operation between two tensors. It returns a new tensor with boolean values (True or False) depending on whether the corresponding elements in both input tensors evaluate to True.

This operation is often used in tensor-based computations where conditional checks need to be applied element-wise, such as in masking or filtering data.

  • Learn how to use PyTorch to build, train, and test artificial neural networks in this course.
    • Intermediate.
      3 hours
  • Build AI classification models with PyTorch using binary and multi-label techniques.
    • With Certificate
    • Beginner Friendly.
      3 hours

Syntax

torch.logical_and(input, other, *, out=None)

Parameters:

  • input (Tensor): The first tensor for the logical AND operation.
  • other (Tensor): The second tensor, must be broadcastable to the shape of input.
  • out (Tensor, optional): The output tensor to store the result.

Return value:

A tensor of type torch.bool containing the result of the element-wise logical AND operation.

Example 1: Basic Usage

In this example, two Boolean tensors are compared element-wise using torch.logical_and():

import torch
a = torch.tensor([True, False, True])
b = torch.tensor([True, True, False])
result = torch.logical_and(a, b)
print(result)

The output of this code is as follows:

tensor([True, False, False])

Example 2: Using with Integer Tensors

In this example, integer tensors are treated as Boolean values, with nonzero as True and 0 as False:

import torch
x = torch.tensor([1, 0, 3])
y = torch.tensor([2, 0, 0])
result = torch.logical_and(x, y)
print(result)

The output of this code is:

tensor([True, False, False])

Example 3: Broadcasting in torch.logical_and()

In this example, broadcasting allows a smaller tensor to be compared across the rows of a larger tensor.

import torch
m = torch.tensor([[1, 0], [0, 1]])
n = torch.tensor([1, 0])
result = torch.logical_and(m, n)
print(result)

The output of this code will be:

tensor([[True, False],
[False, False]])

All contributors

Contribute to Docs

Learn PyTorch on Codecademy

  • Learn how to use PyTorch to build, train, and test artificial neural networks in this course.
    • Intermediate.
      3 hours
  • Build AI classification models with PyTorch using binary and multi-label techniques.
    • With Certificate
    • Beginner Friendly.
      3 hours