Open In App

Tensorflow.js tf.sigmoid() Function

Tensorflow.js is an open-source library that is being developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment.

The .sigmoid() function is used to find the sigmoid of the stated tensor input i.e. 1 / (1 + exp(-x)) and is done element wise.



Syntax:  

tf.sigmoid(x)

Parameters: This function accepts single parameter which are illustrated below:



Return Value: It returns the tf.Tensor object.

Example 1:  




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining tensor input elements
const y = tf.tensor1d([-1, 15, 0, Math.E-1]);
  
// Calling sigmoid() method and
// printing output
y.sigmoid().print();

Output:

Tensor
    [0.2689414, 0.9999996, 0.5, 0.8479074]

Example 2: In this example, the parameter is passed directly to the sigmoid function.




// Importing the tensorflow.js library 
import * as tf from "@tensorflow/tfjs"
  
// Defining float values
var val = [0.5, -1.5, .66];
  
// Calling tensor1d method
const y = tf.tensor1d(val);
  
// Calling sigmoid() method
var res = tf.sigmoid(y)
  
// Printing output
res.print();

Output:

Tensor
    [0.6224594, 0.1824255, 0.6592603]
Article Tags :