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:
- Within, Node.js this method applies setImmediate in place of requestAnimationFrame.
- Moreover, its a basic sugar method such that users are able to do the following: await tf.nextFrame().
Syntax:
tf.nextFrame()
Parameters: This method does not hold any parameter.
Return Value: It returns promise of void.
Example 1:
Javascript
import * as tf from "@tensorflow/tfjs"
for (let i = 0; i < 5; i++){
await tf.nextFrame();
console.log( '*****' );
}
|
Output: Here, due to nextFrame method stars are printed one by one.
*****
*****
*****
*****
*****
Example 2:
Javascript
import * as tf from "@tensorflow/tfjs"
for (let i = 0; i < 3; i++){
const x = tf.randomNormal([2, 3]);
await tf.nextFrame();
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