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:
- Import PyTorch.
- Define the tensors input1 and input2 to compare. The input2 may be a number but the input1 must be a tensor.
- Compute torch.eq(input1, input2).
- 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 PyTorchimport torch# defining first input tensorinput1 = torch.tensor([1., 5., 7.])print("input1:", input1)# defining second input as a numberinput2 = 5.0print("input2:", input2)# compare the above defined tensor and numberresult = torch.eq(input1, input2)# print the comparison resultprint("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 PyTorchimport torch# defining first input tensorinput1 = torch.tensor([[1., 5., 7.], [3.,-2., -23.]])print("input1:\n", input1)# defining second input as a numberinput2 = torch.tensor([[1., -5., 6.], [3.,12., 32.]])print("input2:\n", input2)# compare the above defined tensorsresult = torch.eq(input1, input2)# print the comparison resultprint("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 PyTorchimport torch# defining first input tensorinput1 = torch.tensor([[1., 5., 7.], [3.,-2., -23.]])print("input1:\n", input1)print("Size of input1:\n", input1.shape)# defining second input as a numberinput2 = torch.tensor([[1., -5., 3.]])print("input2:\n", input2)print("Size of input2:\n", input2.shape)# compare the above defined tensorsresult = torch.eq(input1, input2)# print the comparison resultprint("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 PyTorchimport torch# defining first input tensorinput1 = torch.tensor([[1., 5., 7.], [3.,-2., -23.]])print("input1:\n", input1)print("Size of input1:\n", input1.shape)# defining second input as a numberinput2 = torch.tensor([[1.], [-5]])print("input2:\n", input2)print("Size of input2:\n", input2.shape)# compare the above defined tensorsresult = torch.eq(input1, input2)# print the comparison resultprint("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
# Importing PyTorchimport torch# defining first input tensorinput1 = torch.tensor([[1., 5., 7.], [3.,-2., -23.]])print("input1:\n", input1)print("Size of input1:\n", input1.shape)# defining second input as a numberinput2 = torch.tensor([1.,-5, 3.])print("input2:\n", input2)print("Size of input2:\n", input2.shape)# compare the above defined tensorsresult = torch.eq(input1, input2)# print the comparison resultprint("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:
- How to compute mean, standard deviation and variance of a tensor in PyTorch
- How to calculate mean and standard deviation of images in PyTorch
- Mathematical Operations On Images Using OpenCV and NumPy in Python
- Basic Operations on Images using OpenCV in Python
- Different ways to load images in Python
- How to compute mean, standard deviation, and variance of a tensor in PyTorch
- How to calculate mean and standard deviation of images in PyTorch
Next Post: How to Normalize Image Dataset in PyTorch
Previous Post: How to Convert an Image to a Tensor in TensorFlow
Comments
Post a Comment