torch.rsqrt() Method in Python PyTorch

The torch.rsqrt() method computes the reciprocal of square-root of each element of the input tensor. It supports both real and complex-valued inputs. If an element in the input tensor is zero, then the corresponding element in the output tensor is NaN.

Syntax

torch.rsqrt(input)

Parameters

• input – Input tensor

Output

It returns a tensor with reciprocal of square-root.

Steps

• Import the required library. In all the following examples, the required Python library is torch. Make sure you have already installed it.

import torch
• Create a torch tensor and print it.

input = torch.randn(3,4)
print("Input Tensor:", input)
• Compute the reciprocal of the square-root of each element in the input tensor using torch.rsqrt(input). Here input is the input tensor.

recip = torch.rsqrt(input)
• Display the computed tensor with reciprocal values.

print("Reciprocal SQRT Tensor:", recip)

Example 1

In this Python program, we compute the reciprocal of the square-root of both real and complex-valued input tensors.

# Import the required library
import torch

# define an input tensor
input = torch.tensor([1.2, 3., 4., 4.2, -3.2])

# print the above defined tensor
print("Input Tensor:", input)

# compute the reciprocal of the square root
recip = torch.rsqrt(input)

# print the above computed tensor
print("Reciprocal SQRT Tensor:", recip)
print("............................")

# define a complex input tensor
input = torch.tensor([1.2+2j, 3.+4.j, 4.2-3.2j])

# print the above defined tensor
print("Input Tensor:", input)

# compute the reciprocal of the square root
recip = torch.rsqrt(input)

# print the above computed tensor
print("Reciprocal SQRT Tensor:", recip)

Output

Input Tensor:
tensor([ 1.2000, 3.0000, 4.0000, 4.2000, -3.2000])
Reciprocal SQRT Tensor:
tensor([0.9129, 0.5774, 0.5000, 0.4880, nan])
............................
Input Tensor:
tensor([1.2000+2.0000j, 3.0000+4.0000j, 4.2000-3.2000j])
Reciprocal SQRT Tensor:
tensor([0.5698-0.3226j, 0.4000-0.2000j, 0.4123+0.1392j])

Notice that the element in the Reciprocal SQRT tensor corresponding to a zero in the input tensor is NaN.

Example 2

# Import the required library
import torch

# define an input tensor
input = torch.randn(3,4)

# print the above defined tensor
print("Input Tensor:", input)

# compute the reciprocal of the square root
recip = torch.rsqrt(input)

# print the above computed tensor
print("Reciprocal SQRT Tensor:", recip)
print("......................................")

# define a complex input tensor
real = torch.randn(3,3)
imag = torch.randn(3,3)
input = torch.complex(real, imag)

# print the above defined tensor
print("Input Tensor:", input)

# compute the reciprocal of the square root
recip = torch.rsqrt(input)

# print the above computed tensor
print("Reciprocal SQRT Tensor:", recip)

Output

Input Tensor:
tensor([[ 7.4712e-01, -1.5884e+00, -9.7091e-01, -2.9538e-01],
[ 2.0326e-01, 1.6650e+00, -3.1351e-01, 1.1758e-03],
[ 1.6752e+00, 7.2334e-01, -7.4212e-01, 3.6498e-01]])
Reciprocal SQRT Tensor:
tensor([[ 1.1569, nan, nan, nan],
[ 2.2181, 0.7750, nan, 29.1634],
[ 0.7726, 1.1758, nan, 1.6553]])
......................................
Input Tensor:
tensor([[ 1.3595+0.1929j, -0.3348+0.0729j, 2.0567-1.1657j],
[ 0.9777-1.4377j, -0.0728+0.7813j, 0.9582+1.3582j],
[-0.5014+0.7377j, -0.5462-0.9864j, 1.1664-0.5318j]])
Reciprocal SQRT Tensor:
tensor([[0.8513-0.0601j, 0.1827-1.6986j, 0.6289+0.1658j],
[0.6703+0.3548j, 0.7603-0.8344j, 0.6886-0.3569j],
[0.4954-0.9358j, 0.4782+0.8113j, 0.8631+0.1875j]])