Function Differences with torch.logical_not

View Source On Gitee

torch.logical_not

class torch.logical_not(input, out=None)

For more information, see torch.logical_not.

mindspore.numpy.logical_not

class mindspore.numpy.logical_not(a, dtype=None)

For more information, see mindspore.numpy.logical_not.

Differences

PyTorch: If not specified, the output tensor will have the bool dtype. If the input tensor is not a bool tensor, zeros are treated as False and non-zeros are treated as True.

MindSpore: Calculate the logical negation of the input tensor element-wise. The input should be a tensor whose dtype is bool.

Code Example

import mindspore.numpy as np
import torch

# MindSpore
print(np.logical_not(np.array([True, False])))
# Tensor(shape=[2], dtype=Bool, value= [False,  True])
print(np.logical_not(np.array([0, 1, -10])))
# TypeError: For 'LogicalNot or '~' operator', the type of `x` should be subclass of Tensor[Bool], but got Tensor[Int32].

# PyTorch
print(torch.logical_not(torch.tensor([True, False])))
# tensor([False,  True])
print(torch.logical_not(torch.tensor([0, 1, -10], dtype=torch.int8)))
# tensor([ True, False, False])
print(torch.logical_not(torch.tensor([0., 1.5, -10.], dtype=torch.double)))
# tensor([ True, False, False])
print(torch.logical_not(torch.tensor([0., 1., -10.], dtype=torch.double), out=torch.empty(3, dtype=torch.int16)))
# tensor([1, 0, 0], dtype=torch.int16)