Open In App

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

Last Updated : 10 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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.

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 occurred
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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads