Python

PyTorch – Reciprocal()

We will return the reciprocal of all the elements in the tensor using the reciprocal() method 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.reciprocal()

Reciprocal() in PyTorch returns the reciprocal of every element in the PyTorch tensor. It takes one parameter.

Syntax:

torch.reciprocal(tensor_object)

Parameter:

tensor_object is a tensor

Example 1:

In this example, we will create a tensor with one dimension that has 5 elements and return the reciprocal of these 5 elements in a tensor.

#first import the torch module

import torch

 

#create a 1D tensor

data1 = torch.tensor([1.34,5.67,8.90,4.56,7.43])

 

#display

print("Actual elements in the Tensor: ")

print(data1)

 

print("Reciprocals")

print(torch.reciprocal(data1))

Output:

Actual elements in the Tensor:

tensor([1.3400, 5.6700, 8.9000, 4.5600, 7.4300])

Reciprocals

tensor([0.7463, 0.1764, 0.1124, 0.2193, 0.1346]))

Working:

1. 1/1.3400 =0.7463

2. 1/5.6700 = 0.1764

3. 1/8.9000 =0.1124

4. 1/4.5600 =0.2193

5. 1/7.4300 =0.1346

Example 2:

In this example, we will create a tensor with two dimensions that has 5 elements in each row and return the reciprocal of elements.

#first import the torch module

import torch

 

#create a 2D tensor

data1 = torch.tensor([[1.34,5.67,8.90,4.56,7.43],[1,2,3,4,5]])

 

#display

print("Actual elements in the Tensor: ")

print(data1)

 

print("Reciprocals")

print(torch.reciprocal(data1))

Output:

Actual elements in the Tensor:

tensor([[1.3400, 5.6700, 8.9000, 4.5600, 7.4300],

[1.0000, 2.0000, 3.0000, 4.0000, 5.0000]])

Reciprocals

tensor([[0.7463, 0.1764, 0.1124, 0.2193, 0.1346],

[1.0000, 0.5000, 0.3333, 0.2500, 0.2000]])

Working:

1. 1/1.3400 =0.7463,1/1.0000=1.0000

2. 1/5.6700 = 0.1764,1/ 2.0000=0.5000

3. 1/8.9000 =0.1124,1/3.0000=0.3333

4. 1/4.5600 =0.2193, 1/ 4.0000=0.2500

5. 1/7.4300 =0.1346, 1/5.0000=0.2000

Work with CPU

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

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

Syntax:

torch.tensor(data).cpu()

Example 1:

In this example, we will create a tensor with one dimension that has 5 elements on the cpu and return the reciprocal of these 5 elements in a tensor.

#first import the torch module

import torch

 

#create a 1D tensor

data1 = torch.tensor([1.34,5.67,8.90,4.56,7.43]).cpu()

 

#display

print("Actual elements in the Tensor: ")

print(data1)

 

print("Reciprocals")

print(torch.reciprocal(data1))

Output:

Actual elements in the Tensor:

tensor([1.3400, 5.6700, 8.9000, 4.5600, 7.4300])

Reciprocals

tensor([0.7463, 0.1764, 0.1124, 0.2193, 0.1346]))

Working:

1. 1/1.3400 =0.7463

2. 1/5.6700 = 0.1764

3. 1/8.9000 =0.1124

4. 1/4.5600 =0.2193

5. 1/7.4300 =0.1346

Example 2:

In this example, we will create a tensor with two dimensions that has 5 elements on the cpu in each row and return the reciprocal of elements.

#first import the torch module

import torch

 

#create a 2D tensor

data1 = torch.tensor([[1.34,5.67,8.90,4.56,7.43],[1,2,3,4,5]]).cpu()

 

#display

print("Actual elements in the Tensor: ")

print(data1)

 

print("Reciprocals")

print(torch.reciprocal(data1))

Output:

Actual elements in the Tensor:

tensor([[1.3400, 5.6700, 8.9000, 4.5600, 7.4300],

[1.0000, 2.0000, 3.0000, 4.0000, 5.0000]])

Reciprocals

tensor([[0.7463, 0.1764, 0.1124, 0.2193, 0.1346],

[1.0000, 0.5000, 0.3333, 0.2500, 0.2000]])

Working:

1. 1/1.3400 =0.7463,1/1.0000=1.0000

2. 1/5.6700 = 0.1764,1/ 2.0000=0.5000

3. 1/8.9000 =0.1124,1/3.0000=0.3333

4. 1/4.5600 =0.2193, 1/ 4.0000=0.2500

5. 1/7.4300 =0.1346, 1/5.0000=0.2000

Conclusion

In this PyTorch lesson, we discussed about the reciprocal() function. It returns the reciprocal of every element in the PyTorch tensor. We discussed the two examples with different dimensional tensors to perform the reciprocal() function.

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