Tensorflow.js tf.GraphModel Class
Introduction: Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment. Tensorflow.js tf.GraphModel class is used to build an acyclic graph from SavedModel and made it inference execution. tf.GraphModel is created by tf.loadGraphModel() method.
Syntax:
tf.loadGraphModel.Method(args);
Parameters:
- args: Different methods accept different parameters.
Returns: Different method returns different data values, etc.
Below we will see some of the examples of tf.GraphModel class:
Example 1: In this example, we will see executeAsync() method which is used to implement implication in favor of the model. It takes tensor as a parameter input and output node name as a string. It returns the promise of tensor.
Javascript
import * as tf from "@tensorflow/tfjs" async function run(){ // Tensor input elements const gfg_Url = // Calling loadGraphModel() function const gfg_Model = await tf.loadGraphModel(gfg_Url); // Inputs for the model const gfg_shape = [1, 224, 224, 3]; const gfg_Input = tf.zeros(gfg_shape); // Calling executeAsync() const gfg_result = await gfg_Model.executeAsync(gfg_Input); gfg_result.print(); } await run(); |
Output:
Tensor [[-0.1800359, -0.4059841, 0.8190171, ..., -0.895331, -1.084169, 1.2912908],]
Example 2: In this example, we will see dispose() method which is used to dispose of the tensor. It doesn’t take any parameters. it returns void.
Javascript
import * as tf from "@tensorflow/tfjs" async function run(){ // Defining tensor input elements const gfg_Url = // Calling the loadGraphModel() function const gfg_Model = await tf.loadGraphModel(gfg_Url); // Input for our function const gfg_shape = [1, 224, 224, 3]; const gfg_Input = tf.zeros(gfg_shape); // Disposing our Tensor const gfg_result = await gfg_Model.executeAsync(gfg_Input); gfg_result.dispose(); console.log(gfg_result) ; } await run(); |
Output:
Tensor is disposed.
Reference: https://js.tensorflow.org/api/latest/#class:GraphModel
Please Login to comment...