Skip to content
Related Articles

Related Articles

Tensorflow.js tf.keep() Function

Improve Article
Save Article
  • Last Updated : 01 Jun, 2021
Improve Article
Save Article

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 .keep() function is used to hold a tensor input that is formed within a tf.tidy() method from being spontaneously discarded.

Syntax:  

tf.keep(result)

Parameters:  

  • result: It is the stated tensor input which is to be held from being discarded.

Return Value: It returns tf.Tensor object.

Example 1:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Declaring a variable
let res1;
  
// Calling tidy method
const res2 = tf.tidy(() => {
     
  // Defining result parameter
  const result = tf.scalar(121);
     
  // Calling tf.keep() method
  res1 = tf.keep(result.sqrt());
});
  
// Printing output
res1.print();

Output:

Tensor
    11

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Declaring a variable
let res1;
  
// Calling tidy method
const res2 = tf.tidy(() => {
     
  // Calling tf.keep() method with its
  // parameter
  res1 = tf.keep(tf.tensor1d(
    [1.3, 0.5, 0, NaN, null, -.5]).cos());
});
  
// Printing output
res1.print();

Output:

Tensor
    [0.2675007, 0.8775977, 1, NaN, 1, 0.8775977]

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


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!