Python

torch.eq() and torch.ne() Functions in PyTorch

In this PyTorch tutorial, we will learn how to perform comparison operations using the torch.eq() and torch.ne() methods in PyTorch.

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. For using a Tensor, we have to import the torch module.

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

Syntax

torch.tensor(data)

Where data is a multidimensional array.

torch.eq() Function

The torch.eq() in PyTorch is used to compare all the elements in two tensors. If both the elements in a tensor are equal, it will return True. Otherwise False is returned. It would take two parameters.

Syntax

torch.eq(tensor_object1,tensor_object2)

Parameters

  1. tensor_object1 is the first tensor
  2. tensor_object2 is the second tensor

Return
It will return a tensor with the Boolean values.

Example 1
In this example, we will create one-dimensional tensors: data1 and data2 with 5 numeric values to perform eq().

#import torch module
import torch

#create a 1D tensor - data1 with 5 numeric values
data1 = torch.tensor([0,45,67,0,23])

#create a 1D tensor - data2 with 5 numeric values
data2 = torch.tensor([0,0,55,78,23])

#display
print("First Tensor: ",data1)
print("Second Tensor: ",data2)

#eq() on data1 and data2
print("Compare two tensors: ",torch.eq(data1,data2))

Output

First Tensor: tensor([ 0, 45, 67,  0, 23])
Second Tensor: tensor([ 0,  0, 55, 78, 23])
Compare two tensors: tensor([ True, False, False, False,  True])

 Working

  1. 0 equal to 0 – True
  2. 45 equal to 0 – False
  3. 67 equal to 55 – False
  4. 0 equal to78 – False
  5. 23 equal to 23 – True

Example 2
In this example, we will create two-dimensional tensors: data1 and data2 with 5 numeric values each in a row and perform eq().

#import torch module
import torch

#create a 2D tensor - data1 with 5 numeric values in each row
data1 = torch.tensor([[23,45,67,0,0],[12,21,34,56,78]])

#create a 2D tensor - data2 with 5 numeric values in each row
data2 = torch.tensor([[0,0,55,78,23],[10,20,44,56,0]])

#display
print("First Tensor: ",data1)
print("Second Tensor: ",data2)

#eq() on data1 and data2
print("Compare two tensors: ",torch.eq(data1,data2))

Output

First Tensor:  tensor([[23, 45, 67,  0,  0],
[12, 21, 34, 56, 78]])

Second Tensor:  tensor([[ 0,  0, 55, 78, 23],
[10, 20, 44, 56,  0]])

Compare two tensors:  tensor([[False, False, False, False, False],
[False, False, False, True, False]])

 Working

  1. 23 equal to 0 – False, 12 equal to 10 – False
  2. 45 equal to 0 – False, 21 equal to 20 – False
  3. 67 equal to 55 – False, 34 equal to 44 – False
  4. 0 equal to78 – False, 56 equal to 56 – True
  5. 0 equal to 23 – False, 78 equal to 0 – False

Work With CPU
If you want to run an eq() function on the CPU, then we have to create a tensor with a cpu() function. This will run on a CPU machine.

When creating a tensor, we can use the cpu() function at this time.

Syntax

torch.tensor(data).cpu()

Example

In this example, we will create two-dimensional tensors: data1 and data2 with 5 numeric values each in row and perform eq().

#import torch module
import torch

#create a 2D tensor - data1 with 5 numeric values in each row
data1 = torch.tensor([[23,45,67,0,0],[12,21,34,56,78]]).cpu()

#create a 2D tensor - data2 with 5 numeric values in each row
data2 = torch.tensor([[0,0,55,78,23],[10,20,44,56,0]]).cpu()

#display
print("First Tensor: ",data1)
print("Second Tensor: ",data2)

#eq() on data1 and data2
print("Compare two tensors: ",torch.eq(data1,data2))

Output

First Tensor:  tensor([[23, 45, 67,  0,  0],
[12, 21, 34, 56, 78]])

Second Tensor:  tensor([[ 0,  0, 55, 78, 23],
[10, 20, 44, 56,  0]])

Compare two tensors:  tensor([[False, False, False, False, False],
[False, False, False,  True, False]])

 Working

  1. 23 equal to 0 – False, 12 equal to 10 – False
  2. 45 equal to 0 – False, 21 equal to 20 – False
  3. 67 equal to 55 – False, 34 equal to 44 – False
  4. 0 equal to78 – False, 56 equal to 56 – True
  5. 0 equal to 23 – False, 78 equal to 0 – False

torch.ne() Function

The torch.ne() in PyTorch is used to compare all the elements in two tensors. If both the elements in a tensor are not equal, it will return True. Otherwise False is returned. It would take two parameters.

Syntax

torch.ne(tensor_object1,tensor_object2)

Parameters

  1. tensor_object1 is the first tensor
  2. tensor_object2 is the second tensor

Return
It will return a tensor with the Boolean values.

Example 1
In this example, we will create one-dimensional tensors: data1 and data2 with 5 numeric values to perform ne().

#import torch module
import torch

#create a 1D tensor - data1 with 5 numeric values
data1 = torch.tensor([0,45,67,0,23])

#create a 1D tensor - data2 with 5 numeric values
data2 = torch.tensor([0,0,55,78,23])

#display
print("First Tensor: ",data1)
print("Second Tensor: ",data2)

#ne() on data1 and data2
print("Compare two tensors: ",torch.ne(data1,data2))

Output

First Tensor:  tensor([ 0, 45, 67,  0, 23])
Second Tensor:  tensor([ 0,  0, 55, 78, 23])
Compare two tensors:  tensor([False,  True,  True,  True, False])

 Working

  1. 0 not equal to 0 – False
  2. 45 not equal to 0 – True
  3. 67 not equal to 55 – True
  4. 0 not equal to78 – True
  5. 23 not equal to 23 – False

Example 2
In this example, we will create two-dimensional tensors: data1 and data2, with 5 numeric values each in a row and perform ne().

#import torch module
import torch

#create a 2D tensor - data1 with 5 numeric values in each row
data1 = torch.tensor([[23,45,67,0,0],[12,21,34,56,78]])

#create a 2D tensor - data2 with 5 numeric values in each row
data2 = torch.tensor([[0,0,55,78,23],[10,20,44,56,0]])

#display
print("First Tensor: ",data1)
print("Second Tensor: ",data2)

#ne() on data1 and data2
print("Compare two tensors: ",torch.ne(data1,data2))

Output

First Tensor:  tensor([[23, 45, 67,  0,  0],
[12, 21, 34, 56, 78]])

Second Tensor:  tensor([[ 0,  0, 55, 78, 23],
[10, 20, 44, 56,  0]])

Compare  two tensors:  tensor([[ True,  True,  True,  True,  True],
[ True,  True,  True, False,  True]])

 Working

  1. 23 not equal to 0 – True, 12 not equal to 10 – True
  2. 45 not equal to 0 – True, 21 not equal to 20 – True
  3. 67 not equal to 55 – True, 34 not equal to 44 – True
  4. 0 not equal to78 – True, 56 not equal to 56 – False
  5. 0 not equal to 23 – True, 78 not equal to 0 – True

Work With CPU
If you want to run a ne() function on the CPU, then we have to create a tensor with a cpu() function. This will run on a CPU machine.

When we are creating a tensor, at this time, we can use the cpu() function.

Syntax

torch.tensor(data).cpu()

Example
In this example, we will create two-dimensional tensors: data1 and data2 with 5 numeric values each in row and perform ne().

#import torch module
import torch

#create a 2D tensor - data1 with 5 numeric values in each row
data1 = torch.tensor([[23,45,67,0,0],[12,21,34,56,78]]).cpu()

#create a 2D tensor - data2 with 5 numeric values in each row
data2 = torch.tensor([[0,0,55,78,23],[10,20,44,56,0]]).cpu()

#display
print("First Tensor: ",data1)
print("Second Tensor: ",data2)

#ne() on data1 and data2
print("Compare two tensors: ",torch.ne(data1,data2))

Output

First Tensor:  tensor([[23, 45, 67,  0,  0],
[12, 21, 34, 56, 78]])

Second Tensor:  tensor([[ 0,  0, 55, 78, 23],
[10, 20, 44, 56,  0]])

Compare two tensors:  tensor([[ True,  True,  True,  True,  True],
[ True,  True,  True, False,  True]])

 Working

  1. 23 not equal to 0 – True, 12 not equal to 10 – True
  2. 45 not equal to 0 – True, 21 not equal to 20 – True
  3. 67 not equal to 55 – True, 34 not equal to 44 – True
  4. 0 not equal to78 – True, 56 not equal to 56 – False
  5. 0 not equal to 23 – True, 78 not equal to 0 – True

Conclusion

In this PyTorch lesson, we discussed torch.eq() and torch.ne(). Both are comparison functions used to compare elements in two tensors. In torch.eq(), if both the elements in a tensor are equal, it will return True. Otherwise False is returned. The torch.ne() is used to compare all the elements in two tensors. If both the elements in a tensor are not equal, it will return True. Otherwise False is returned. We also discussed these functions that will work on a CPU.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain