Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to compare two tensors in PyTorch?
To compare two tensors element-wise in PyTorch, we use the torch.eq() method. It compares corresponding elements and returns True if the elements are equal, else it returns False. We can compare tensors with same or different dimensions, but their sizes must match at non-singleton dimensions.
Syntax
torch.eq(input, other)
Parameters:
-
input− First tensor to compare -
other− Second tensor to compare
Return Value: A tensor of boolean values (True or False)
Example 1: Comparing 1-D Tensors
The following example shows how to compare two 1-D tensors element-wise ?
import torch
# Create two 1-D tensors
T1 = torch.Tensor([2.4, 5.4, -3.44, -5.43, 43.5])
T2 = torch.Tensor([2.4, 5.5, -3.44, -5.43, 43])
print("T1:", T1)
print("T2:", T2)
# Compare tensors element-wise
result = torch.eq(T1, T2)
print("Comparison result:", result)
T1: tensor([ 2.4000, 5.4000, -3.4400, -5.4300, 43.5000]) T2: tensor([ 2.4000, 5.5000, -3.4400, -5.4300, 43.0000]) Comparison result: tensor([ True, False, True, True, False])
Example 2: Comparing 2-D Tensors
The following example shows how to compare two 2-D tensors element-wise ?
import torch
# Create two 4x3 2D tensors
T1 = torch.Tensor([[2, 3, -32],
[43, 4, -53],
[4, 37, -4],
[3, 75, 34]])
T2 = torch.Tensor([[2, 3, -32],
[4, 4, -53],
[4, 37, 4],
[3, -75, 34]])
print("T1:", T1)
print("T2:", T2)
# Compare tensors element-wise
result = torch.eq(T1, T2)
print("Comparison result:", result)
T1: tensor([[ 2., 3., -32.],
[ 43., 4., -53.],
[ 4., 37., -4.],
[ 3., 75., 34.]])
T2: tensor([[ 2., 3., -32.],
[ 4., 4., -53.],
[ 4., 37., 4.],
[ 3., -75., 34.]])
Comparison result: tensor([[ True, True, True],
[False, True, True],
[ True, True, False],
[ True, False, True]])
Example 3: Broadcasting with Different Dimensions
The following example shows how to compare a 1-D tensor with a 2-D tensor using broadcasting ?
import torch
# Create tensors with different dimensions
T1 = torch.Tensor([2.4, 5.4, -3.44, -5.43, 43.5])
T2 = torch.Tensor([[2.4, 5.5, -3.44, -5.43, 7],
[1.0, 5.4, 3.88, 4.0, 5.78]])
print("T1:", T1)
print("T2:", T2)
# Compare tensors using broadcasting
result = torch.eq(T1, T2)
print("Comparison result:", result)
T1: tensor([ 2.4000, 5.4000, -3.4400, -5.4300, 43.5000])
T2: tensor([[ 2.4000, 5.5000, -3.4400, -5.4300, 7.0000],
[ 1.0000, 5.4000, 3.8800, 4.0000, 5.7800]])
Comparison result: tensor([[ True, False, True, True, False],
[False, True, False, False, False]])
Other Comparison Methods
PyTorch also provides other comparison methods:
-
torch.ne()− Not equal comparison -
torch.gt()− Greater than comparison -
torch.lt()− Less than comparison -
torch.ge()− Greater than or equal comparison -
torch.le()− Less than or equal comparison
Conclusion
Use torch.eq() to perform element-wise equality comparison between PyTorch tensors. The method supports broadcasting and returns a boolean tensor with the same shape as the input tensors.
