Python

PyTorch – Lcm()

We will see how to return least common multiples in an input tensor in this PyTorch tutorial.

PyTorch is an open-source framework available with a Python programming language. Tensor is a multidimensional array that is used to store the data. To use a tensor, we have to import the torch module. To create a tensor, the method used is tensor().

Syntax:

torch.tensor(data)

Where the data is a multi-dimensional array.

Torch.lcm()

Lcm() in PyTorch is used to return the least common multiples from both the elements in two tensor objects.

Syntax:

torch.lcm(tensor_object1,tensor_object2)

Where:

  1. tensor_object1 is the first tensor
  2. tensor_object2 is the second tensor

Return:

It also returns the least common multiples from the two tensors.

Example 1:

In this example, we will create two tensors with one dimension that has 5 elements each and perform the lcm() operation on them.

#import torch module

import torch

 

#create two 1D tensors

data1 = torch.tensor([1,2,3,4,5])

data2 = torch.tensor([34,45,3,40,10])

 

#display

print("Actual Tensors: ")

print(data1)

print(data2)

 

print("LCM")

#return least common multiples

print(torch.lcm(data1,data2))

Output:

Actual Tensors:

tensor([1, 2, 3, 4, 5])

tensor([34, 45, 3, 40, 10])

LCM

tensor([34, 90, 3, 40, 10])

Working:

  1. lcm(1,34) – 34
  2. lcm(2,45) – 90
  3. lcm(3,3) – 3
  4. lcm(4,40) – 40
  5. lcm(5,10) – 10

It is also possible to return the LCM with one element that computes with each and every element.

Example 2:

In this example, we will create two tensors with one dimension that has 5 elements in the first tensor and one element in the second tensor, and perform the lcm() operation on them.

#import torch module

import torch

 

#create two 1D tensors

data1 = torch.tensor([1,2,3,4,5])

data2 = torch.tensor([10])

 

#display

print("Actual Tensors: ")

print(data1)

print(data2)

 

print("LCM")

#return least common multiples

print(torch.lcm(data1,data2))

Output:

Actual Tensors:

tensor([1, 2, 3, 4, 5])

tensor([10])

LCM

tensor([10, 10, 30, 20, 10])

Working:

  1. lcm(1,10) -10
  2. lcm(2,10) – 10
  3. lcm(3,10) – 30
  4. lcm(4,10) – 20
  5. lcm(5,10) – 10

Example 3:

In this example, we will create two tensors with 2 dimensions that have 5 elements each and perform the lcm() operation on them.

#import torch module

import torch

 

#create two 2D tensors

data1 = torch.tensor([[1,2,3,4,5],[45,67,89,87,78]])

data2 = torch.tensor([[134,54,67,65,56],[45,67,89,87,78]])

 

#display

print("Actual Tensors: ")

print(data1)

print(data2)

 

print("LCM")

#return least common multiples

print(torch.lcm(data1,data2))

Output:

Actual Tensors:

tensor([[ 1, 2, 3, 4, 5],

[45, 67, 89, 87, 78]])

tensor([[134, 54, 67, 65, 56],

[ 45, 67, 89, 87, 78]])

LCM

tensor([[134, 54, 201, 260, 280],

[ 45, 67, 89, 87, 78]])

Working:

  1. lcm(1,134) -134,lcm(45,45) -45
  2. lcm(2,54) – 54,lcm(67,67) -67
  3. lcm(3,67) – 201,lcm(89,89) -89
  4. lcm(4,65) – 260,lcm(87,87) -87
  5. lcm(5,56) – 280,lcm(78,78) -78

Work with CPU

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

When we are create a tensor, this time, we can use the cpu() function.

Syntax:

torch.tensor(data).cpu()

Example 1:

In this example, we will create two tensors with one dimension that has 5 elements each and perform the lcm() operation on them.

#import torch module

import torch

 

#create two 1D tensors

data1 = torch.tensor([1,2,3,4,5]).cpu()

data2 = torch.tensor([34,45,3,40,10]).cpu()

 

#display

print("Actual Tensors: ")

print(data1)

print(data2)

 

print("LCM")

#return least common multiples

print(torch.lcm(data1,data2))

Output:

Actual Tensors:

tensor([1, 2, 3, 4, 5])

tensor([34, 45, 3, 40, 10])

LCM

tensor([34, 90, 3, 40, 10])

Working:

  1. lcm(1,34) – 34
  2. lcm(2,45) – 90
  3. lcm(3,3) – 3
  4. lcm(4,40) – 40
  5. lcm(5,10) – 10

It is also possible to return the LCM with one element that computes with each and every element.

Example 2:

In this example, we will create two tensors with one dimension that has 5 elements in the first tensor and one element in the second tensor, and perform the lcm() operation on them.

#import torch module

import torch

 

#create two 1D tensors

data1 = torch.tensor([1,2,3,4,5]).cpu()

data2 = torch.tensor([10]).cpu()

 

#display

print("Actual Tensors: ")

print(data1)

print(data2)

 

print("LCM")

#return least common multiples

print(torch.lcm(data1,data2))

Output:

Actual Tensors:

tensor([1, 2, 3, 4, 5])

tensor([10])

LCM

tensor([10, 10, 30, 20, 10])

Working:

  1. lcm(1,10) -10
  2. lcm(2,10) – 10
  3. lcm(3,10) – 30
  4. lcm(4,10) – 20
  5. lcm(5,10) – 10

Example 3:

In this example, we will create two tensors with 2 dimensions that have 5 elements each and perform the lcm() operation on them.

#import torch module

import torch

 

#create two 2D tensors

data1 = torch.tensor([[1,2,3,4,5],[45,67,89,87,78]]).cpu()

data2 = torch.tensor([[134,54,67,65,56],[45,67,89,87,78]]).cpu()

 

#display

print("Actual Tensors: ")

print(data1)

print(data2)

 

print("LCM")

#return least common multiples

print(torch.lcm(data1,data2))

Output:

Actual Tensors:

tensor([[ 1, 2, 3, 4, 5],

[45, 67, 89, 87, 78]])

tensor([[134, 54, 67, 65, 56],

[ 45, 67, 89, 87, 78]])

LCM

tensor([[134, 54, 201, 260, 280],

[ 45, 67, 89, 87, 78]])

Working:

  1. lcm(1,134) -134,lcm(45,45) -45
  2. lcm(2,54) – 54,lcm(67,67) -67
  3. lcm(3,67) – 201,lcm(89,89) -89
  4. lcm(4,65) – 260,lcm(87,87) -87
  5. lcm(5,56) – 280,lcm(78,78) -78

Conclusion

In this PyTorch lesson, we learned about the lcm() function and how to apply it on a tensor to return the least common multiple . We also created a tensor with the cpu() function and returned the LCM.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain