Open In App

Tensorflow.js tf.nextFrame() Function

Introduction: 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 .nextFrame() function is used to return a promise which determines at which a requestAnimationFrame has been terminated.



Note:

Syntax:



tf.nextFrame()

Parameters: This method does not hold any parameter.

Return Value: It returns promise of void.

Example 1:




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Using for loop
for(let i = 0; i < 5; i++){
  
// Calling nextFrame() method
await tf.nextFrame();
  
// Printing output
console.log('*****');
}

Output: Here, due to nextFrame method stars are printed one by one.

*****
*****
*****
*****
*****

Example 2:




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Using for loop
for(let i = 0; i < 3; i++){
  
// Calling randomNormal() method
const x = tf.randomNormal([2, 3]);
    
// Calling nextFrame() method
await tf.nextFrame();
  
// Printing output
console.log(x);
}

Output: Here, if you run the program promptly more than once then an error occurs as the tensor is disposed.

Tensor
    [[-1.2814652, 0.0379729, 1.4826748],
     [1.5050254 , 0.0769796, 0.5443317]]
Tensor
    [[-1.9229894, 0.2478886 , 0.6501164],
     [0.3088476 , -1.0728339, 0.8636787]]
Tensor
    [[-0.2324371, -1.2162384, 0.3193687],
     [0.0266885 , 1.3987972 , 0.4429231]]

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


Article Tags :