PyTorch is an open-source framework available with a Python programming language. We can process the data in PyTorch in the form of a Tensor.

A tensor is a multidimensional array that is used to store the data. So for using a Tensor, we have to import the torch module.

To create a tensor, the method used is tensor()”

**Syntax**:

Where data is a multi-dimensional array.

**torch.logical_not()**

torch.logical_not() in PyTorch is performed on a single tensor object. It returns True if the value is False or 0 and returns False if the value is True or not equal to 0. It takes a tensor as a parameter.

**Syntax**:

torch.logical_not(tensor_object)

**Parameter**:

tensor_object is the tensor

**Example 1**

In this example, we will create a one-dimensional tensor – data1 with 5 boolean values and perform logical_not().

import torch

#create a 1D tensor – data1 with 5 boolean values

data1 = torch.tensor([False,True, True, True,False])

#display

print(«Tensor: «,data1)

#logical_not on data1

print(«Logical NOT on above tensor: «,torch.logical_not(data1))

Output:

Logical NOT on above tensor: tensor([ True, False, False, False, True])

**Working**:

1. logical_not(False) – True

2. logical_not(True) – False

3. logical_not(True) – False

4. logical_not(True) – False

5. logical_not(False) – True

**Example 2**

In this example, we will create a two-dimensional tensor – data1 with 5 boolean values in each two rows and perform logical_not().

import torch

#create a 2D tensor – data1 with 5 boolean values each

data1 = torch.tensor([[False,True, True, True,False],[False,True, True, True,False]])

#display

print(«Tensor: «,data1)

#logical_not on data1

print(«Logical NOT on above tensor: «,torch.logical_not(data1))

Output:

[False, True, True, True, False]])

Logical NOT on above tensor: tensor([[ True, False, False, False, True],

[ True, False, False, False, True]])

**Working**:

2. logical_not(True) – False, logical_not(True) – False

3. logical_not(True) – False, logical_not(True) – False

4. logical_not(True) – False, logical_not(True) – False

5. logical_not(False) – True, logical_not(False) – True

**Example 3**

In this example, we will create a one-dimensional tensor – data1 with 5 numeric values and perform logical_not().

import torch

#create a 1D tensor – data1 with 5 numeric values

data1 = torch.tensor([0,1,23,45,56])

#display

print(«Tensor: «,data1)

#logical_not on data1

print(«Logical NOT on above tensor: «,torch.logical_not(data1))

Output:

Logical NOT on above tensor: tensor([ True, False, False, False, False])

**Working**:

1. logical_not(0) – True

2. logical_not(1) – False

3. logical_not(23) – False

4. logical_not(45) – False

5. logical_not(56) – False

**Example 4**

In this example, we will create a two-dimensional tensor – data1 5 boolean values in each two rows and perform logical_not().

import torch

#create a 2D tensor – data1 with 5 boolean values each

data1 = torch.tensor([[12,34,56,78,90],[0,0,1,2,0]])

#display

print(«Tensor: «,data1)

#logical_not on data1

print(«Logical NOT on above tensor: «,torch.logical_not(data1))

Output:

[ 0, 0, 1, 2, 0]])

Logical NOT on above tensor: tensor([[False, False, False, False, False],

[ True, True, False, False, True]])

**Working**:

1. logical_not(12) – False, logical_not(0) – True

2. logical_not(34) – False, logical_not(0) – True

3. logical_not(56) – False, logical_not(1) – False

4. logical_not(78) – False, logical_not(2) – False

5. logical_not(90) – False, logical_not(0) – True

**Conclusion**

In this PyTorch lesson, we discussed how to perform logical NOT operation with a torch.logical_not() method. It returns True if the value is False or 0 and returns False if the value is True or not equal to 0. We discussed 4 examples of boolean values and numeric values with one and 2-dimensional tensors.