Function Differences with torch.logical_or

View Source On Gitee

torch.logical_or

class torch.logical_or(input, other, out=None)

For more information, see torch.logical_or.

mindspore.ops.LogicalOr

class class mindspore.ops.LogicalOr()(x, y)

For more information, see mindspore.ops.LogicalOr.

Differences

PyTorch: Computes the element-wise logical OR of the given input tensors. Zeros are treated as False and nonzeros are treated as True.

MindSpore: Computes the “logical OR” of two tensors element-wise. The input should be a bool or a tensor whose data type is bool.

Code Example

import numpy as np
import torch
from mindspore import Tensor, Parameter
from mindspore import dtype as mstype

# MindSpore
x = Tensor(np.array([True, False, True]), mstype.bool_)
y = Tensor(np.array([True, True, False]), mstype.bool_)
logical_or = ops.LogicalOr()
print(logical_or(x, y))
# [ True  True  True]
x = Tensor(np.array([True, False, True]), mstype.bool_)
y = Tensor(np.array([True, True, False]), mstype.int32)
logical_or = ops.LogicalOr()
print(logical_or(x, y))
# TypeError: For 'LogicalOr', the type of `x` should be subclass of Tensor[Bool], but got Tensor[Int32] .

# PyTorch
print(torch.logical_or(torch.tensor([True, False, True]), torch.tensor([True, False, False])))
# tensor([ True, False,  True])
a = torch.tensor([0, 1, 10, 0], dtype=torch.int8)
b = torch.tensor([4, 0, 1, 0], dtype=torch.int8)
print(torch.logical_or(a, b))
# tensor([ True,  True,  True, False])
print(torch.logical_or(a.double(), b.double()))
# tensor([ True,  True,  True, False])
print(torch.logical_or(a.double(), b))
# tensor([ True,  True,  True, False])
print(torch.logical_or(a, b, out=torch.empty(4, dtype=torch.bool)))
# tensor([ True,  True,  True, False])