How to Compare Two Tensors Element-wise in PyTorch

In this article we will understand how to compare two tensors element-wise. PyTorch provides us with the method torch.eq() to compare two tensors. It compares the corresponding elements of the tensors and returns True if values are the same else return False. Following is the syntax of this method:

Syntax: torch.eq(input1, input2) 

Parameters:

  • input1: it's the first tensor to compare. 
  • input2: it's the second tensor or a number to compare. 

Output: It returns a Boolean Tensor with elements True if the corresponding elements of input1 and input2 are same else False.

Approach:

  1. Import PyTorch.
  2. Define the tensors input1 and input2 to compare. The input2 may be a number but the input1 must be a tensor.
  3. Compute torch.eq(input1, input2).
  4. Print the above computed value.

Let's understand the comparison of two tenors with the help of some Python examples.

Example 1. Comparison between Tensor and a number

In the below Python program, we compare a PyTorch Tensor with a number. 

# Importing PyTorch
import torch
# defining first input tensor
input1 = torch.tensor([1., 5., 7.])
print("input1:", input1)
# defining second input as a number
input2 = 5.0
print("input2:", input2)
# compare the above defined tensor and number
result = torch.eq(input1, input2)
# print the comparison result
print("Result:", result)

Output:

input1: tensor([1., 5., 7.])
input2: 5.0
Result: tensor([False,  True, False])

Explanation: The number 5.0 is compared with each element of the first tensor and the corresponding result True or False is returned.

Example 2. Comparison between two Tensors of same dimension

In the Python program below , we compare two 2D tensors.

# Importing PyTorch
import torch
# defining first input tensor
input1 = torch.tensor([[1., 5., 7.], [3.,-2., -23.]])
print("input1:\n", input1)
# defining second input as a number
input2 = torch.tensor([[1., -5., 6.], [3.,12., 32.]])
print("input2:\n", input2)
# compare the above defined tensors
result = torch.eq(input1, input2)
# print the comparison result
print("Result:\n", result)

Output:

input1:
 tensor([[  1.,   5.,   7.],
        [  3.,  -2., -23.]])
input2:
 tensor([[ 1., -5.,  6.],
        [ 3., 12., 32.]])
Result:
 tensor([[ True, False, False],
        [ True, False, False]])

Explanation: Please note that in the above example we compared two 2D tensors of size [2, 3]. We could also compare tensor of size [2,3] with tensor of size [1, 3]. We can also compare tensors of size [2,3] with a tensor of size [2, 1]. See the below example codes

# Importing PyTorch
import torch
# defining first input tensor
input1 = torch.tensor([[1., 5., 7.], [3.,-2., -23.]])
print("input1:\n", input1)
print("Size of input1:\n", input1.shape)
# defining second input as a number
input2 = torch.tensor([[1., -5., 3.]])
print("input2:\n", input2)
print("Size of input2:\n", input2.shape)
# compare the above defined tensors
result = torch.eq(input1, input2)
# print the comparison result
print("Result:\n", result)

Output:

input1:
 tensor([[  1.,   5.,   7.],
        [  3.,  -2., -23.]])
Size of input1:
 torch.Size([2, 3])
input2:
 tensor([[ 1., -5.,  3.]])
Size of input2:
 torch.Size([1, 3])
Result:
 tensor([[ True, False, False],
        [False, False, False]])

In the above example a tensor of size [2, 3] is compared with other tensor of size [1, 3].

See the below example-

# Importing PyTorch
import torch
# defining first input tensor
input1 = torch.tensor([[1., 5., 7.], [3.,-2., -23.]])
print("input1:\n", input1)
print("Size of input1:\n", input1.shape)
# defining second input as a number
input2 = torch.tensor([[1.], [-5]])
print("input2:\n", input2)
print("Size of input2:\n", input2.shape)
# compare the above defined tensors
result = torch.eq(input1, input2)
# print the comparison result
print("Result:\n", result)

Output:

input1:
 tensor([[  1.,   5.,   7.],
        [  3.,  -2., -23.]])
Size of input1:
 torch.Size([2, 3])
input2:
 tensor([[ 1.],
        [-5.]])
Size of input2:
 torch.Size([2, 1])
Result:
 tensor([[ True, False, False],
        [False, False, False]])

In the above example a tensor of size [2, 3] is compared with other tensor of size [2, 1].

Example 3: Comparison between two Tensors of different dimensions

In the Python program below we perform comparison between two input tensors with different dimensions. The input1 is a 2D tensor whereas input2 is a 1D tensor. In this case the size of both tensors at first dimension (non-singleton dimension 1) must be equal. See the below example-
# Importing PyTorch
import torch
# defining first input tensor
input1 = torch.tensor([[1., 5., 7.], [3.,-2., -23.]])
print("input1:\n", input1)
print("Size of input1:\n", input1.shape)
# defining second input as a number
input2 = torch.tensor([1.,-53.])
print("input2:\n", input2)
print("Size of input2:\n", input2.shape)
# compare the above defined tensors
result = torch.eq(input1, input2)
# print the comparison result
print("Result:\n", result)

Output:

input1:
 tensor([[  1.,   5.,   7.],
        [  3.,  -2., -23.]])
Size of input1:
 torch.Size([2, 3])
input2:
 tensor([ 1., -5.,  3.])
Size of input2:
 torch.Size([3])
Result:
 tensor([[ True, False, False],
        [False, False, False]])

In the above example we compared a 2D tensor with a 1D tensor. Notice that the size of first tensor input1 (size at dimension 1 = 3) matches the size of second tensor input2 (size at dimension 1 = 3) at non-singleton dimension 1. 

References:

Further Readings:


Next Post: How to Normalize Image Dataset in PyTorch

Previous Post: How to Convert an Image to a Tensor in TensorFlow






Comments