JavaScript

Tensorflow.js – tf.exp()

We will discuss how to return the exponential values from a tensor using the tf.exp() function in the Tensorflow.js library.

Tf.exp() Function

The tf.exp() in Tensorflow.js returns the exponential values in a tensor. Mathematically, “e” is raised to the power of “x”.

Syntax:

tf.exp(tensor_input)

Parameter:

The tensor_input is a tensor that has numbers.
It can be one or two-dimensional.

Example 1:

Let’s create a one-dimensional tensor in js that has positive and negative Infinities and apply the exp() 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.exp() </h2></center>
<script>

let values = tf.tensor1d([Infinity,-Infinity]);
//actual tensor
document.write("Actual Tensor: ",values);

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

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

</body>
</html>

Output:

  1. exp(Infinity) – Infinity
  2. exp(-Infinity) – 0

Example 2:

Let’s create a one-dimensional tensor in js that has 0, null, NaN, and undefined values and return the exponential 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.exp() </h2></center>
<script>

let values = tf.tensor1d([0,null,NaN,undefined]);
//actual tensor
document.write("Actual Tensor: ",values);

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

document.write("Tensor with Exponential Values:- "+tf.exp(values));
</script>

</body>
</html>

Output:

Tensor takes null as 0 and undefined as NaN.

  1. exp(0) – 1
  2. exp(0) – 1
  3. exp(NaN) – NaN
  4. exp(NaN) – NaN

Example 3:

Let’s create a tensor that has two dimensions in js with 2 rows and 2 columns that has decimal values and raise the exponential 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.exp() </h2></center>
<script>

let values = tf.tensor2d([[-34.4,4.56],[4.5,7.89]]);
//actual tensor
document.write("Actual Tensor: ",values);

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

document.write("Tensor with Exponential Values:- "+tf.exp(values));
</script>

</body>
</html>

Output:

  1. exp(-34.4000015) – 0
  2. exp( 4.5599999) – 95.5834732
  3. exp(4.5) – 90.017128
  4. exp(7.8899999) – 2670.4436035

Example 4:

Let’s create a tensor that has two dimensions in js with 2 rows and 2 columns that has integer values and raise the exponential 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.exp() </h2></center>
<script>

let values = tf.tensor2d([[-4,5],[8,-45]]);
//actual tensor
document.write("Actual Tensor: ",values);

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

document.write("Tensor with Exponential Values:- "+tf.exp(values));
</script>

</body>
</html>

Output:

  1. exp(-4) – 0
  2. exp(5) – 148.4131622
  3. exp(8) – 2980.9580078
  4. exp(-45) – 0

Conclusion

In this Tensorflow.js tutorial, we learned how to get the exponential values using the tf.exp() function with four different examples. We noticed that for the negative values, the exponent values is 0.

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