Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Tensorflow.js tf.GraphModel class .dispose() Method

  • Last Updated : 05 Aug, 2021

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 .dispose() function is used to free the memory that is being utilized by the weight tensors as well as resourceManager.

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!

Syntax:

dispose()

Parameters:  



This method do not holds any parameter.

Return Value: It returns void.

Example 1: 

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining tensor input elements
const model_Url =
  
// Calling the loadGraphModel() method
const mymodel = await tf.loadGraphModel(model_Url);
  
// Calling dispose() method
mymodel.dispose();
  
// Printing output
console.log('Model Disposed.');

Output:

Model Disposed.

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining tensor input elements
const model_Url =
  
// Calling the loadGraphModel() method
const mymodel = await tf.loadGraphModel(model_Url);
  
// Defining inputs
const inputs = tf.zeros([1, 224, 224, 3]);
  
// Calling dispose() method
mymodel.dispose();
  
// Calling execute() method and
// Printing output
mymodel.execute(inputs).print();

Output:

An error occured
Cannot read property 'backend' of undefined

Here, an error occurred and the output is not printed as the stated model is already disposed. So, the execute() method is not able to return any output.

Reference: https://js.tensorflow.org/api/latest/#tf.GraphModel.dispose




My Personal Notes arrow_drop_up
Recommended Articles
Page :