Tensorflow.js tf.GraphModel 类 .dispose() 方法
Tensorflow.js 是由谷歌开发的开源库,用于在浏览器或节点环境中运行机器学习模型以及深度学习神经网络。
.dispose()函数用于释放权重张量和资源管理器正在使用的内存。
句法:
dispose()
参数:
此方法不保存任何参数。
返回值:返回void。
示例 1:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Defining tensor input elements
const model_Url =
'https://storage.googleapis.com/tfjs-models/savedmodel/mobilenet_v2_1.0_224/model.json';
// Calling the loadGraphModel() method
const mymodel = await tf.loadGraphModel(model_Url);
// Calling dispose() method
mymodel.dispose();
// Printing output
console.log('Model Disposed.');
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Defining tensor input elements
const model_Url =
'https://storage.googleapis.com/tfjs-models/savedmodel/mobilenet_v2_1.0_224/model.json';
// 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();
输出:
Model Disposed.
示例 2:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Defining tensor input elements
const model_Url =
'https://storage.googleapis.com/tfjs-models/savedmodel/mobilenet_v2_1.0_224/model.json';
// 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();
输出:
An error occured
Cannot read property 'backend' of undefined
在这里,发生错误并且由于所述模型已被处理,因此未打印输出。因此,execute() 方法无法返回任何输出。
参考: https://js.tensorflow.org/api/latest/#tf.GraphModel.dispose