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.gt() Function
The torch.lt() in PyTorch is used to compare all the elements in two tensors (greater than). It returns True if the element in the first tensor is greater than the element in the second tensor and returns False if the element in the first tensor is not greater than the element in the second tensor. It takes 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 gt().
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)
#gt() on data1 and data2
print("Do the elements in the first tensor are greater than elements in the second tensor? : ",torch.gt(data1,data2))
Output
First Tensor: tensor([ 0, 45, 67, 0, 23])
Second Tensor: tensor([ 0, 0, 55, 78, 23])
Do the elements in the first tensor are greater than elements in the second tensor? : tensor([False, True, True, False, False])
Working
- 0 greater than 0 – False
- 45 greater than 0 – True
- 67 greater than 55 – True
- 0 greater than 78 – False
- 23 greater than 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 gt().
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)
#gt() on data1 and data2
print("Do the elements in the first tensor are greater than elements in the second tensor? : ",torch.gt(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]])
Do the elements in the first tensor are greater than elements in the second tensor? : Tensor([[ True, True, True, False, False],
[ True, True, False, False, True]])
Working
- 23 greater than 0 – True, 12 greater than 10 – True
- 45 greater than 0 – True, 21 greater than 20 – True
- 67 greater than 55 – True, 34 greater than 44 – False
- 0 greater than 78 – False, 56 greater than 56 – False
- 0 greater than 23 – False, 78 greater than 0 – True
Work With CPU
If you want to run a gt() 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 gt().
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)
#gt() on data1 and data2
print("Do the elements in the first tensor are greater than elements in the second tensor? : ",torch.gt(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]])
Are the elements in the first tensor greater than the elements in the second tensor?
tensor([[ True, True, True, False, False],
[ True, True, False, False, True]])
Working
- 23 greater than 0 – True, 12 greater than 10 – True
- 45 greater than 0 – True, 21 greater than 20 – True
- 67 greater than 55 – True, 34 greater than 44 – False
- 0 greater than 78 – False, 56 greater than 56 – False
- 0 greater than 23 – False, 78 greater than 0 – True
torch.ge() Function
The torch.ge() function in PyTorch is used to compare all the elements in two tensors (greater than or equal to). It returns True if the element in the first tensor is greater than or equal to the element in the second tensor and False if the element in the first tensor is neither greater than nor equal to the element in the second tensor. It takes two parameters.
Syntax
<strong>Parameters</strong>
<ol>
<li>tensor_object1 is the first tensor</li>
<li>tensor_object2 is the second tensor</li>
</ol>
<strong>Return
</strong>It will return a tensor with the Boolean values.
<strong>Example 1
</strong>In this example, we will create one-dimensional tensors: data1 and data2 with 5 numeric values to perform ge().
[cc lang="python" width="100%" height="100%" escaped="true" theme="blackboard" nowrap="0"]
#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)
#ge() on data1 and data2
print("Do the elements in the first tensor are greater than or equal to the elements in the second tensor? : ",torch.ge(data1,data2))
Output
First Tensor: tensor([ 0, 45, 67, 0, 23])
Second Tensor: tensor([ 0, 0, 55, 78, 23])
Do the elements in the first tensor are greater than or equal to the elements in the second tensor? : tensor([ True, True, True, False, True])
Working
- 0 greater than or equal to 0 – True
- 45 greater than or equal to 0 – True
- 67 greater than or equal to 55 – True
- 0 greater than or equal to 78 – False
- 23 greater ss than or equal to 23 – True
Example 2
In this example, we will create two dimensional tensors: data1 and data2 with 5 numeric values each in row and perform ge().
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)
#ge() on data1 and data2
print("Do the elements in the first tensor are greater than or equal to the elements in the second tensor? : ",torch.ge(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]])
Do the elements in the first tensor are greater than or equal to the elements in the second tensor? : tensor([[ True, True, True, False, False],
[ True, True, False, True, True]])
Working
- 23 greater than or equal to 0 – True, 12 greater than or equal to 10 – True
- 45 greater than or equal to 0 – True, 21 greater than or equal to 20 – True
- 67 greater than or equal to 55 – True, 34 greater than or equal to 44 – False
- 0 greater than or equal to 78 – False, 56 greater than or equal to 56 – True
- 0 greater than or equal to 23 – False, 78 greater than or equal to 0 – True
Work With CPU
If you want to run a ge() 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 ge().
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)
#ge() on data1 and data2
print("Do the elements in the first tensor are greater than or equal to the elements in the second tensor? : ",torch.ge(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]])
Do the elements in the first tensor are greater than or equal to the elements in the second tensor? tensor([[ True, True, True, False, False],
[ True, True, False, True, True]])
Working
- 23 greater than or equal to 0 – True, 12 greater than or equal to 10 – True
- 45 greater than or equal to 0 – True, 21 greater than or equal to 20 – True
- 67 greater than or equal to 55 – True, 34 greater than or equal to 44 – False
- 0 greater than or equal to 78 – False, 56 greater than or equal to 56 – True
- 0 greater than or equal to 23 – False, 78 greater than or equal to 0 – True
Conclusion
In this PyTorch lesson, we discussed torch.gt() and torch.ge(). Both are comparison functions used to compare elements in two tensors. The torch.gt() compares all the elements in two tensors (greater than). It returns True if the element in the first tensor is greater than the element in the second tensor and returns False if the element in the first tensor is not greater than the element in the second tensor.
The torch.ge() returns True if the element in the first tensor is greater than or equal to the element in the second tensor and returns False if the element in the first tensor is neither greater than nor equal to the element in the second tensor. We also discussed these functions that will work on a CPU.