JavaScript

Tensorflow.js – tf.log()

Tf.log() Function

The tf.log() in Tensorflow.js is used to return the logarithmic values from a given values in a tensor. It takes only one parameter – tensor – that has numbers.

Syntax:

tf.log(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 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.log() </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 log() on the above tensor
document.write("Tensor with Log Values: "+tf.log(values));
</script>

</body>
</html>

 
Output:


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

    1. log(0) => -Infinity
    2. log(1) => 0
    3. log(0) => -Infinity
    4. log(NaN) => NaN
    5. log(NaN) => NaN

We observed that if the input is NaN or undefined, the logarithm is also NaN and returns the -Infinity when the values are 0 or null.

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 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.log() </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 log() on the above tensor
document.write("Tensor with Log Values: "+tf.log(values));
</script>

</body>
</html>

 
Output:

    1. log(1.23) =>0.2070142
    2. log(4.5599999) => 1.5173227
    3. log(-0.45) => NaN
    4. log(7.8899999) => 2.0655961

For the negative values, it returns NaN.

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 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.log() </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 log() on the above tensor
document.write("Tensor with Log Values: "+tf.log(values));
</script>

</body>
</html>

 
Output:

    1. log(2.7182817) => 0.9999999
    2. log(3.7182817) => 1.3132616
    3. log(1.7182819) => 0.5413249
    4. log(3.1682818) => 1.1531894

Conclusion

In this Tensorflow.js tutorial, we learned how to return the logarithmic values using the tf.log() function with three different examples. We observed that if the input is NaN or undefined, the logarithm is also NaN and returns the -Infinity when the values are 0 or null.

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