JavaScript

# Tensorflow.js – tf.log1p()

## Tf.log1p() Function

The tf.log1p() in Tensorflow.js is used to return the natural logarithmic values from a given values in a tensor. It takes only one parameter, the tensor, that has numbers. Mathematically, it can be referred as log(1+x).

Syntax:

tf.log1p(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 null, undefined, and NaN values and return the natural logarithmic 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.log1p() </h2></center>
<script>

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

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

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

</body>
</html>

Output:

Tensor takes null as 0 and the undefined and NaN as NaN value.

1. log1p(0) => 0
2. log1p(1)=> 1
3. log1p(0)=> 0
4. log1p(NaN)=> NaN
5. log1p(NaN)=> NaN

We observed that if the input is 0, NaN, null and undefined, the logarithmic value is 0.

## Example 2:

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

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

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

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

</body>
</html>

Output:

1. log1p(1.23) =>0.8020017
2. log1p(4.5599999) => 1.7155979
3. log1p(-0.45) =>-0.597837
4. log1p(7.8899999) => 2.184927

## Example 3:

Let’s create a tensor that has two dimensions in js with 2 rows and 2 columns that has exponent values and return the natural logarithmic 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.log1p() </h2></center>
<script>

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

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

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

</body>
</html>

Output:

1. log1p(2.7182817) => 1.3132617
2. log1p(3.7182817) => 1.5514446
3. log1p(1.7182819) => 0.9999998
4. log1p(3.1682818) => 1.4275037

## Conclusion

In this Tensorflow.js tutorial, we learned how to return the natural logarithmic values using the tf.log1p() function with three different examples. We observed that if the input is 0, NaN, null, and undefined, the logarithmic value 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