Open In App

Tensorflow.js tf.relu() Function

Last Updated : 18 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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

The .relu() function is used to find rectified linear of the stated tensor input i.e. max(x, 0) and is done element wise.

Syntax :  

tf.relu(x)

Parameters:  

  • x: It is the stated tensor input, and it can be of type tf.Tensor, TypedArray, or Array. Moreover, if the stated datatype is of type Boolean then the output datatype will be of type int32.

Return Value: It returns the tf.Tensor object.

Example 1:  

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining tensor input elements
const y = tf.tensor1d([11, 76, 0, -4, 6]);
  
// Calling relu() method and
// Printing output
y.relu().print();


Output:

Tensor
    [11, 76, 0, 0, 6]

Example 2:

Javascript




// Importing the tensorflow.js library 
import * as tf from "@tensorflow/tfjs"
  
// Defining tensor input
var val = [1.5, -5, .22, null, 'a'];
  
// Calling tensor1d method
const y = tf.tensor1d(val);
  
// Calling relu() method
var res = tf.relu(y)
  
// Printing output
res.print();


Output:

Tensor
    [1.5, 0, 0.22, 0, NaN]

Reference: https://js.tensorflow.org/api/latest/#relu


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads