JavaScript

# Tensorflow.js – tf.logSigmoid()

In Machine Learning, Log Sigmoid function means logarithmic value of a sigmoid function. The Sigmoid function acts as an activation function that adds the non-linearity to a model. Simply, the Sigmoid function is used to make a non-linear model.

The mathematical formula is 1 / (1 + exp(-x)).

## Tf.logSigmoid() Function

Syntax:

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

</body>
</html>

Output:

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

1. logSigmoid(0) => -0.6931472
2. logSigmoid(1)=> -0.3132616
3. logSigmoid(0)=> -0.6931472
4. logSigmoid(NaN)=> -0.6931472
5. logSigmoid(NaN)=> -0.6931472

We observed that if the input is 0, NaN, null and undefined, the logSigmoid value is -0.6931472.

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

</body>
</html>

Output:

1. logSigmoid(1.23) =>-0.2564178
2. logSigmoid(4.5599999) =>-0.0104076
3. logSigmoid(-0.45) =>-0.9432489
4. logSigmoid(7.8899999) => -0.0003744

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

</body>
</html>

Output:

1. logSigmoid(2.7182817) => -0.0639022
2. logSigmoid(3.7182817) => -0.0239857
3. logSigmoid(1.7182819) => -0.1649839
4. logSigmoid(3.1682818) => -0.0412147

## Conclusion

In this Tensorflow.js tutorial, we learned how to return the natural logarithmic Sigmoid values using the tf.logSigmoid() function with three different examples. We observed that if the input is 0, NaN, null, and undefined, the log sigmoid value is -0.6931472.