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
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
Parameters
- tensor_object1 is the first tensor
- 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
#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
- 0 equal to 0 – True
- 45 equal to 0 – False
- 67 equal to 55 – False
- 0 equal to78 – False
- 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
#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
- 23 equal to 0 – False, 12 equal to 10 – False
- 45 equal to 0 – False, 21 equal to 20 – False
- 67 equal to 55 – False, 34 equal to 44 – False
- 0 equal to78 – False, 56 equal to 56 – True
- 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
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
#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
- 23 equal to 0 – False, 12 equal to 10 – False
- 45 equal to 0 – False, 21 equal to 20 – False
- 67 equal to 55 – False, 34 equal to 44 – False
- 0 equal to78 – False, 56 equal to 56 – True
- 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
Parameters
- tensor_object1 is the first tensor
- 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
#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
- 0 not equal to 0 – False
- 45 not equal to 0 – True
- 67 not equal to 55 – True
- 0 not equal to78 – True
- 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
#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
- 23 not equal to 0 – True, 12 not equal to 10 – True
- 45 not equal to 0 – True, 21 not equal to 20 – True
- 67 not equal to 55 – True, 34 not equal to 44 – True
- 0 not equal to78 – True, 56 not equal to 56 – False
- 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
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
#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
- 23 not equal to 0 – True, 12 not equal to 10 – True
- 45 not equal to 0 – True, 21 not equal to 20 – True
- 67 not equal to 55 – True, 34 not equal to 44 – True
- 0 not equal to78 – True, 56 not equal to 56 – False
- 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.