JavaScript

# Tensorflow.js – tf.sigmoid()

In this article, we will see how to round-off the values in tensorflow.js framework in the JavaScript. The advantages of rounding is we can represent the values in an integer format without any decimal points.

In machine learning, the Sigmoid function acts as an activation function that adds the non-linearity to a model. Simply, Sigmoid function is used to make a non-linear model. The mathematical formula is 1 / (1 + exp(-x)).

We will see how it is applied on tensor elements.

## Tf.sigmoid() Function

The tf.sigmoid() is used to return the Sigmoid values from a given value in a tensor.
It takes only one parameter, the tensor,that has numbers.

According to the formula, x represents each element in a tensor. Finally, the value is computed and results in a Sigmoid value.

Syntax:

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

</body>
</html>

Output:

1. 1 / (1 + exp(-0)) => 0
2. 1 / (1 + exp(-1)) => 0.7310586
3. 1 / (1 + exp(-0)) => 0.5
4. 1 / (1 + exp(-NaN)) => NaN
5. 1 / (1 + exp(-NaN)) => NaN

We observed that if the input is NaN or undefined, the sigmoid is also NaN.

## 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 Sigmoid 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.sigmoid() </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>");

document.write("Tensor with Sigmoid Values: "+tf.sigmoid(values));
</script>

</body>
</html>

Output:

1. 1 / (1 + exp(-1.23)) => 0.7738186
2. 1 / (1 + exp(-4.5599999)) => 0.9896463
3. 1 / (1 + exp(0.45)) => 0.3893608
4. 1 / (1 + exp(-7.8899999)) => 0.9996257

## 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 Sigmoid 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.sigmoid() </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>");

document.write("Tensor with Sigmoid Values: "+tf.sigmoid(values));
</script>

</body>
</html>

Output:

1. 1 / (1 + exp(-2.7182817)) => 0.9380968
2. 1 / (1 + exp(-3.7182817)) => 0.9762997
3. 1 / (1 + exp(-1.7182819)) => 0.8479074
4. 1 / (1 + exp(-3.1682818)) => 0.959623

## Conclusion

In this Tensorflow.js tutorial, we learned how to return the Sigmoid values using the tf.sigmoid() function with three different examples. The formula for the Sigmoid function is – 1 / (1 + exp(-x)). We observed that if the input is NaN or undefined, the Sigmoid is also NaN.