Python

PyTorch – Rsqrt()

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

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

Syntax:

torch.rsqrt(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 square roots of these 5 elements in a tensor.

#import torch module

import torch

 

#create a tensor

data1 = torch.tensor([12,34,56,1,10])

 

#display

print("Actual Tensor: ")

print(data1)

 

print("Reciprocal square Root: ")

print(torch.rsqrt(data1))

Output:

Actual Tensor:

tensor([12, 34, 56, 1, 10])

Reciprocal square Root:

tensor([0.2887, 0.1715, 0.1336, 1.0000, 0.3162])

Working:

  1. 1/√12 =0.2887
  2. 1/√34 = 0.1715
  3. 1/√56 =0.1336
  4. 1/√1 =1.0000
  5. 1/√10 =0.3162

Example 2:

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

#import torch module

import torch

 

#create a 2D tensor

data1=torch.tensor([[45,67,21,23,2],[2,3,4,5,6]])

 

#display

print("Actual Tensor: ")

print(data1)

 

print("Reciprocal square Root: ")

print(torch.rsqrt(data1))

Output:

Actual Tensor:

tensor([[45, 67, 21, 23, 2],

[ 2, 3, 4, 5, 6]])

Reciprocal square Root:

tensor([[0.1491, 0.1222, 0.2182, 0.2085, 0.7071],

[0.7071, 0.5774, 0.5000, 0.4472, 0.4082]])

Working:

  1. 1/√45 = 0.1491, 1/√2 = 0.7071
  2. 1/√67 = 0.1222, 1/√3=0.5774
  3. 1/√21 = 0.2182, 1/√4=0.5000
  4. 1/√23 = 0.2085, 1/√5=0.4472
  5. 1/√2 = 0.7071, 1/√6=0.4082

Work with CPU

If you want to run a rsqrt() 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 square roots of these 5 elements in a tensor.

#import torch module

import torch

 

#create a tensor

data1 = torch.tensor([12,34,56,1,10]).cpu()

 

#display

print("Actual Tensor: ")

print(data1)

 

print("Reciprocal square Root: ")

print(torch.rsqrt(data1))

Output:

Actual Tensor:

tensor([12, 34, 56, 1, 10])

Reciprocal square Root:

tensor([0.2887, 0.1715, 0.1336, 1.0000, 0.3162])

Working:

  1. 1/√12 =0.2887
  2. 1/√34 = 0.1715
  3. 1/√56 =0.1336
  4. 1/√1 =1.0000
  5. 1/√10 =0.3162

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 square root of elements.

#import torch module

import torch

 

#create a 2D tensor

data1=torch.tensor([[45,67,21,23,2],[2,3,4,5,6]]).cpu()

 

#display

print("Actual Tensor: ")

print(data1)

 

print("Reciprocal square Root: ")

print(torch.rsqrt(data1))

Output:

Actual Tensor:

tensor([[45, 67, 21, 23, 2],

[ 2, 3, 4, 5, 6]])

Reciprocal square Root:

tensor([[0.1491, 0.1222, 0.2182, 0.2085, 0.7071],

[0.7071, 0.5774, 0.5000, 0.4472, 0.4082]])

Working:

  1. 1/√45 = 0.1491, 1/√2 = 0.7071
  2. 1/√67 = 0.1222, 1/√3=0.5774
  3. 1/√21 = 0.2182, 1/√4=0.5000
  4. 1/√23 = 0.2085, 1/√5=0.4472
  5. 1/√2 = 0.7071, 1/√6=0.4082

Conclusion

In this PyTorch lesson, we discussed about the rsqrt() function. It returns the reciprocal square root of every element in the PyTorch tensor. We discussed the two examples with the different dimensional tensors to perform the rsqrt() 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