Skip to content
Related Articles

Related Articles

Improve Article

Tensorflow.js tf.keep() Function

  • Last Updated : 01 Jun, 2021
Geek Week

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

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it’s lowest price ever!




My Personal Notes arrow_drop_up
Recommended Articles
Page :