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:**

torch.tensor(data)

Where data is a multidimensional array.

## torch.lt() Function

The torch.lt() function in PyTorch is used to compare all the elements in two tensors (less than). It returns True if the element in the first tensor is less than the element in the second tensor and returns False if the element in the first tensor is not less than the element in the second tensor. It takes two parameters.

**Syntax:**

torch.lt(tensor_object1, tensor_object2)

**Parameters:**

- tensor_object1 is the first tensor
- tensor_object2 is the second tensor

**Return:**

It will return a tensor with Boolean values.

## Example 1

In this example, we will create one-dimensional tensors: data1 and data2 with 5 numeric values to perform lt().

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)

#lt() on data1 and data2

print("Do the elements in the first tensor are less than elements in the second tensor? : ",torch.lt(data1,data2))

**Output:**

Second Tensor: tensor([ 0, 0, 55, 78, 23])

Are the elements in the first tensor less than elements in the second tensor?: tensor([False, False, False, True, False])

** Working:**

- 0 less than 0 – False
- 45 less than 0 – False
- 67 less than 55 – False
- 0 less than 78 – True
- 23 less 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 lt().

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)

#lt() on data1 and data2

print("Do the elements in the first tensor are less than elements in the second tensor? : ",torch.lt(data1,data2))

**Output:**

[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 less than elements in the second tensor?: tensor([[False, False, False, True, True],

** Working:**

- 23 less than 0 – False, 12 less than 10 – False
- 45 less than 0 – False, 21 less than 20 – False
- 67 less than 55 – False, 34 less than 44 – True
- 0 less than 78 – True, 56 less than 56 – False
- 0 less than 23 – True, 78 less than 0 – False

## Work With CPU

If you want to run a lt() function on the CPU, we need to create a tensor with a cpu() function. This will run on a CPU machine.

When we are creating a tensor, we can use the cpu() function.

**Syntax:**

torch.tensor(data).cpu()

## Example

In this example, we will create two dimensional tensors: data1 and data2 with 5 numeric values each in a row and perform lt().

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)

#lt() on data1 and data2

print("Do the elements in the first tensor are less than elements in the second tensor? : ",torch.lt(data1,data2))

**Output:**

[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 less than elements in the second tensor?: tensor([[False, False, False, True, True],

** Working:**

- 23 less than 0 – False, 12 less than 10 – False
- 45 less than 0 – False, 21 less than 20 – False
- 67 less than 55 – False, 34 less than 44 – True
- 0 less than 78 – True, 56 less than 56 – False
- 0 less than 23 – True, 78 less than 0 – False

## torch.le() Function

The torch.le() function in PyTorch is used to compare all the elements in two tensors (**less than or equal to** ). It returns True if the element in the first tensor is less than or equal to the element in the second tensor and returns False if the element in the first tensor is neither less than nor equal to the element in the second tensor. It takes two parameters.

**Syntax:**

torch.le(tensor_object1,tensor_object2)

**Parameters:**

- tensor_object1 is the first tensor
- tensor_object2 is the second tensor

**Return:**

It will return a tensor with Boolean values.

## Example 1

In this example, we will create one-dimensional tensors: data1 and data2 with 5 numeric values to perform le().

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)

#le() on data1 and data2

print("Do the elements in the first tensor are less than or equal to the elements in the second tensor? : ",torch.le(data1,data2))

**Output:**

Second Tensor: tensor([ 0, 0, 55, 78, 23])

Are the elements in the first tensor less than or equal to the elements in the second tensor?: tensor([ True, False, False, True, True])

** Working:**

- 0 less than or equal to 0 – True
- 45 less than or equal to 0 – False
- 67 less than or equal to 55 – False
- 0 less than or equal to 78 – True
- 23 less 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 a row and perform le().

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)

#le() on data1 and data2

print("Do the elements in the first tensor are less than or equal to the elements in the second tensor? : ",torch.le(data1,data2))

**Output:**

[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 less than or equal to the elements in the second tensor?: tensor([[False, False, False, True, True],

** Working:**

- 23 less than or equal to 0 – False, 12 less than or equal to 10 – False
- 45 less than or equal to 0 – False, 21 less than or equal to 20 – False
- 67 less than or equal to 55 – False, 34 less than or equal to 44 – True
- 0 less than or equal to 78 – True, 56 less than or equal to 56 – True
- 0 less than or equal to 23 – True, 78 less than or equal to 0 – False

## Work With CPU

If you want to run a le() 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, we can use the cpu() function.

**Syntax:**

torch.tensor(data).cpu()

## Example

In this example, we will create two dimensional tensors: data1 and data2 with 5 numeric values each in a row and perform le().

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)

#le() on data1 and data2

print("Do the elements in the first tensor are less than or equal to the elements in the second tensor? : ",torch.le(data1,data2))

**Output:**

[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 less than or equal to the elements in the second tensor?: tensor([[False, False, False, True, True],

** Working:**

- 23 less than or equal to 0 – False, 12 less than or equal to 10 – False
- 45 less than or equal to 0 – False, 21 less than or equal to 20 – False
- 67 less than or equal to 55 – False, 34 less than or equal to 44 – True
- 0 less than or equal to 78 – True, 56 less than or equal to 56 – True
- 0 less than or equal to 23 – True, 78 less than or equal to 0 – False

## Conclusion

In this PyTorch lesson, we discussed torch.lt() and torch.le(). Both are comparison functions used to compare elements in two tensors. The torch.lt() function compares all the elements in two tensors **(less than)**. It returns True if the element in the first tensor is less than the element in the second tensor and False if the element in the first tensor is not less than the element in the second tensor.

The torch.le() function returns True if the element in the first tensor is less than or equal to the element in the second tensor and returns False if the element in the first tensor is neither less than nor equal to the element in the second tensor. We also discussed these functions that will work on the CPU.