JavaScript

# The tf.expm1() Function in TensorFlow.js

We will discuss how to return the exponential values differed by 1 from a tensor using the tf.expm1() function in the TensorFlow.js library.

## tf.expm1() Function

The tf.expm1() in TensorFlow.js returns the exponential values minus 1 in a tensor.

Mathematically, it is represented as (e^x)-1.

Syntax:

tf.expm1(tensor_input)

Parameter:

tensor_input is a tensor that has numbers.

It can be one-dimensional or two-dimensional.

## Example 1

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

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

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

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

</body>
</html>

Output:

1. exp(Infinity)-1 => Infinity
2. exp(-Infinity)-1 => -1.

## Example 2

Let’s create a one-dimensional tensor in TensorFlow.js that has 0, null, NaN, and undefined values and return exponential values differed by 1.

<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.expm1() </h2></center>
<script>

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

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

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

</body>
</html>

Output:

Tensor takes null as 0 and undefined as NaN.

1. exp(0)-1 => 0
2. exp(0)-1 => 0
3. exp(NaN)-1 => NaN
4. exp(NaN)-1 => NaN

## Example 3

Let’s create a tensor that has two dimensions in TensorFlow.js with 2 rows and 2 columns that has decimal values and raise the exponential values differed by 1.

<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.expm1() </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>");

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

</body>
</html>

Output:

1. exp(-34.4000015)-1 => -1
2. exp( 4.5599999)-1 => 94.5834732
3. exp(4.5)-1 => 89.017128
4. exp(7.8899999)-1 => 2669.4436035

## Example 4

Let’s create a tensor that has two dimensions in TensorFlow.js with 2 rows and 2 columns that has integer values and raise the exponential values differed by 1.

<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.expm1() </h2></center>
<script>

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

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

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

</body>
</html>

Output:

1. exp(-4)-1 => -0.9816844
2. exp(5)-1 => 147.4131622
3. exp(8)-1 =>2979.9580078
4. exp(-45)-1 =>-1.

## Conclusion

In this TensorFlow.js tutorial, we saw how to get the exponential values differed by 1 using the tf.expm1() function with four different examples. It is very similar to exp(), but the result differed by 1.