JavaScript

# Tensorflow.js – tf.reciprocal()

## Tf.reciprocal() Function

The tf.reciprocal() function in tensorflow.js returns the reciprocal values for the given decimal values in a tensor.

Syntax:

tf.reciprocal(tensor_input)

Parameter:

Tensor_input is a tensor that has numeric elements.
It can be one or two-dimensional.

Computation:

1/x where x refers to each element in a tensor.

## Example 1:

Let’s create a one-dimensional tensor in js that has decimal values and apply the reciprocal() function.

<html>
<!--   CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

<body>
<center><h1>Linux Hint</h1></center>
<center><h2>Tensorflow.js - tf.reciprocal() </h2></center>
<script>

let values = tf.tensor1d([8.56,3.45,7.89,8.32,9.03,1.00,45.6]);
//actual tensor
document.write("Actual Tensor: ",values);

document.write("<br>");
document.write("<br>");

//apply reciprocal() on the above tensor
document.write("Tensor with Reciprocal values:- "+tf.reciprocal(values));
</script>

</body>
</html>

Output:

Working:

[1/8.56,1/3.45,1/7.89,1/8.32,1/9.03,1/1.00,1/45.6] => [0.1168224, 0.2898551, 0.1267427, 0.1201923, 0.110742, 1, 0.0219298]

## Example 2:

Let’s create a tensor that has two dimensions in js with 2 rows and 2 columns that has integer values and get the reciprocal values.

<html>
<!--   CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

<body>
<center><h1>Linux Hint</h1></center>
<center><h2>Tensorflow.js - tf.reciprocal() </h2></center>
<script>

let values = tf.tensor2d([[3,4],[2,3]]);
//actual tensor
document.write("Actual Tensor: ",values);

document.write("<br>");
document.write("<br>");

//apply reciprocal() on the above tensor
document.write("Tensor with Reciprocal values:- "+tf.reciprocal(values));
</script>

</body>
</html>

Output:

Working:

[[1/3, 1/4], [1/2, 1/3]], => [[0.3333333, 0.25 ], [0.5 , 0.3333333]]

## Example 3:

Let’s create a tensor that has two dimensions in js with 2 rows and 2 columns that has exponent values and get the reciprocal values.

<html>
<!--   CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

<body>
<center><h1>Linux Hint</h1></center>
<center><h2>Tensorflow.js - tf.reciprocal() </h2></center>
<script>

let values = tf.tensor2d([[Math.E,Math.E-1],[Math.E+1,Math.E*9]]);
//actual tensor
document.write("Actual Tensor: ",values);

document.write("<br>");
document.write("<br>");

//apply reciprocal() on the above tensor
document.write("Tensor with Reciprocal values:- "+tf.reciprocal(values));
</script>

</body>
</html>

Output:

Working:

[[1/2.7182817,1/ 1.7182819 ], [1/3.7182817, 1/24.4645367]] => [[0.3678795, 0.5819767], [0.2689414, 0.0408755]]

## Conclusion

In this Tensorflow.js tutorial, we learned how to get the reciprocal values using the tf.reciprocal() function with three different examples. It computes with the formula – 1/x.