Home Machine Learning PyTorch Rsqrt()

PyTorch Rsqrt()

PyTorch – Rsqrt() returns a new tensor with the square root of each input item as its reciprocal.

The rsqrt() method in PyTorch calculates the square root reciprocal of each input tensor member. Tensors with real and complex values are both acceptable. The square root of a negative number’s reciprocal is returned as “NaN” (not a number), and “inf” is returned as zero. The reciprocal of the square root of an input number is calculated mathematically using the following formula.

PyTorch – Rsqrt()
PyTorch – Rsqrt()

In this PyTorch lesson, we’ll use the sqrt() method to return the reciprocal square root of each element in the tensor. An open-source framework called PyTorch is offered together with the Python programming language. The data is kept in a multidimensional array called a tensor. We must, therefore, import the torch module to use a tensor. The procedure used to produce a tensor is called tensor().

PyTorch – Rsqrt() Syntax

The syntax for PyTorch’s Rsqrt() is:

torch.rsqrt(input)

Parameters

input (Tensor) – the input tensor.

Keyword Arguments

out (Tensor, optional) – refers to the sensor’s output which is a reciprocal of the given square root.

To illustrate this concept, we use the following example:

var_a = torch.randn(4)
print(var_a )
torch.rsqrt(var_a )

What are the necessary steps in using PyTorch -Rsqrt()

Add the necessary library. The essential Python library for each of the following examples is the torch. Make sure that it is already installed.

import torch

Create and print a torch tensor.

var_input = torch.randn(4,5)
print("Tensor's Input value:\n", var_input)

Use the torch to calculate the reciprocal of the square root of each input tensor using tensor.rsqrt(input). The input tensor is called the input here.

val_reciprocal = torch.rsqrt(input)

Reverse values for the computed tensor are to be displayed.

print("SQRT Tensor Reciprocal:\n", val_ reciprocal)

Example: Calculating the square root’s reciprocal

This Python application calculates the reciprocal of the square root of input tensors with real and complex values.

# 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:\n", input)

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

# print the above-computed tensor
print("Reciprocal SQRT Tensor:\n", var_reciprocal)
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:\n", input)

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

# print the above-computed tensor
print("Reciprocal SQRT Tensor:\n", var_reciprocal)

Keep in mind that the Reciprocal SQRT tensor member that corresponds to a zero in the input tensor is NaN.

Example: 2-dimensional tensor with five elements

To demonstrate how to generate a tensor with two dimensions and five elements per row, we’ll create a tensor and then return the reciprocal square root of the elements.

#start by importing the torch module

import torch

#follow-on with creating a 2D tensor
var_data=torch.tensor([[50,72,26,28,7],[2,3,4,5,6]])
 

#display
print("The Actual Tensor is: ")

print(var_data)

print("The resulting reciprocal of the square root is: ")

print(torch.rsqrt(var_data))

Example: compute the reciprocal of the square root

# Import the required library
import torch

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

# print the above-defined tensor
print("The Tensor's Input is:\n", var_input)

# compute the reciprocal of the square root
var_reciprocal = torch.rsqrt(var_input)

# print the above-computed tensor
print("The Reciprocal SQRT Tensor:\n", var_reciprocal)
print("......................................")

# define a complex input tensor
var_real = torch.randn(3,3)
var_imag = torch.randn(3,3)
var_input = torch.complex(var_real, var_imag)

# print the above-defined tensor
print("The Input Tensor:\n", var_input)

# compute the reciprocal of the square root
var_reciprocal = torch.rsqrt(var_input)

# print the above-computed tensor
print("The Tensor's SQRT Reciprocal:\n", var_reciprocal)

Example: 1-dimensional tensor with five elements

In this example, a tensor with one dimension and five elements will be created, returning the reciprocal square roots of these five elements in a tensor.

#import torch module
import torch

#creation of a tensor
var_data = torch.tensor([17,39,61,6,15])

#display
print("The Actual Tensor is: ")
print(var_data)

 
print("The resulting  square Root's Reciprocal is: ")
print(torch.rsqrt(var_data))

Utilizing a CPU

We must create a tensor with a cpu() function to execute an rsqrt() function on the CPU. It will operate on a computer with a CPU. This time, the cpu() function will be vital when building a tensor.

The syntax for using a cpu() is as follows:

torch.tensor(data).cpu()

Example: 1-dimensional tensor with five elements

In this example, we’ll generate a one-dimensional tensor on the computer with five elements and return the reciprocal square roots of this tensor’s five elements.

#importing torch module
import torch

#creation of a new tensor
var_data = torch.tensor([17,39,61,6,15]).cpu()

#display
print("The Actual Tensor is: ")
print(var_data)

print("The square Root's Reciprocal is: ")
print(torch.rsqrt(var_data))

Example :2-dimensional tensor with 5 elements on cpu()

In this example, we’ll build a two-dimensional tensor with five components in each row on the CPU and return the reciprocal square root of the elements.

#start by importing the module, torch 
import torch

#the ensuing it by creating a 2D tensor
var_data=torch.tensor([[50,72,26,28,7],[2,3,4,5,6]]).cpu()

#display
print("The Actual Tensor is: ")
print(var_data)

print("The square Root's Reciprocal is: ")
print(torch.rsqrt(var_data))

Example 1: A Modified resnet block for passing additional noise input

This code snippet is responsible for passing additional noise input as a modified resnet block.

def forward_pass(self, input, noise):
	# to be used for conditional instance norm
        # assumes input.dim() == 4, TODO: generalize that.

        var_shift = self.shift_conv.forward(noise)
        var_scale = self.scale_conv.forward(noise)
        var_size = input.size()
        x_reshaped = input.view(var_size[0], var_size[1], var_size[2]* var_size[3])
        var_mean = x_reshaped.mean(2, keepdim=True)
        var = x_reshaped.var(2, keepdim=True)
        std =  torch.rsqrt(var + self.eps)
        norm_features = ((x_reshaped - var_mean) * std).view(*var_size)
        var_output = norm_features * var_scale + var_shift
        return var_output

Example 2: The numeric data type NaN

Explore this example to understand the NaN numeric data type.

aVar = torch.randn(4)
print('variable a = ', aVar)
bVar = torch.rsqrt(aVar)
print('variable b = ', bVar)

The numeric data type NaN, which stands for not a number, represents any value that is undefined or unpresentable. For instance, since 0/0 cannot be specified as a real number, NaN is used to represent it.

Example 3: The square root’s reciprocal

The square root’s reciprocal of the elements in “var_a” makes up the tensor ” var_b.”

var_a = torch.tensor([5., 10.])
print('The variable a is = ', var_a)
var_b = torch.rsqrt(var_a)
print(' var_b = ', var_b)

Example 4: Illustrating rsqrt() as an element-level operation & it’s effect

This example looks at rsqrt()’s element-level operation and the subsequent effect that accompanies the same.

import torch

var_x = torch.Tensor([[3,6],[18, 27]])
var_y = torch.rsqrt(var_x)
print(var_y)

Example 5: Computing Similarity

The following code function computes the similarity of input variables.

def compute_similarity(var_inputs):
	"""
	"""
	var_values, var_indices = torch.max(inputs, 1)
	var_thres = 0.9
	var_weights = values.ge(var_thres)
	var_weights = var_weights.type(torch.cuda.FloatTensor)
	[batch_size, dim] = var_inputs.shape
	var_indices = torch.unsqueeze(var_indices.cpu(), 1)
	var_one_hot_labels = torch.zeros(batch_size, dim).scatter_(1, var_indices, 1)
	var_one_hot_labels = var_one_hot_labels.cuda()
	var_inputs_two = torch.mul(inputs, inputs)
	var_norm_two = torch.sum(var_inputs_two, 1)
	var_root_inv = torch.rsqrt(var_norm_two)
	var_tmp_var_one = root_inv.expand(dim,batch_size)
	var_tmp_var_two = torch.t(tmp_var_one)
	var_nml_inputs = torch.mul(inputs, var_tmp_var_two)
	var_similarity = torch.matmul(nml_inputs, torch.t(var_nml_inputs))			
	var_similarity_two = var_similarity - torch.eye(batch_size).cuda()
	return var_similarity, var_one_hot_labels, var_weights

Example 6: computing a 3-D tensor’s rsqrt

Using the torch, we compute a 3-D tensor’s rsqrt function in the example below. You might note that you obtain a different number with each run. That is because we will use a random number generator to generate the numbers in this case. The rsqrt of a multidimensional tensor element is computed similarly to one-dimensional tensors.

# Python program to compute the reciprocal of
# square root of a multi-dimensional tensor
# importing torch
import torch

# define the input tensor
var_a = torch.randn(7, 8, 7)

# print the input tensor
print("The tensor value is:\n", var_a)

# compute reciprocal square root
var_rs = torch.rsqrt(var_a)

# print the computed result
print("rsqrt of var_a:\n", var_rs)

Example 7: Using torch.rsqrt() to compute a 1-dimensional complex tensor

Using the torch, we compute the rsqrt of a one-dimensional complex tensor in the example below. With rsqrt() function You might notice that you obtain a different number with each run because the complex numbers are generated using a random number generator.

# Python program to compute the reciprocal of
# square root of a complex tensor
# importing torch
import torch

# define the input tensor
var_a = torch.randn(4, dtype=torch.cfloat)

# print the input tensor
print("The tensor  value is:\n", var_a)

# compute reciprocal square root
var_rs = torch.rsqrt(a)

# print the computed result
print("The rsqrt of var_a is :\n", var_rs)

Conclusion

The rsqrt() function is discussed in this PyTorch -Rsqrt() tutorial. Every component of the PyTorch tensor is given its reciprocal square root. To use the rsqrt() function, we reviewed a number of examples using various dimensional tensors.

Each member of the input tensor’s square-root reciprocal is calculated using the torch.rsqrt() method. It accepts inputs with real and complex values. The matching element in the output tensor is NaN if an element in the input tensor is zero.

You may also like

Leave a Comment