Tensorflow.js tf.disposeVariables()函数
Tensorflow.js 是一个由谷歌开发的开源库,用于在浏览器或节点环境中运行机器学习模型以及深度学习神经网络。
.disposeVariables()函数用于处理存储在后端引擎中的每个变量。
句法:
tf.disposeVariables()
参数:此方法不接受任何参数。
返回值:返回void。
示例 1:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Declaring a variable
var x = tf.tensor([1, 2, 3, 4]);
// Calling disposeVariables() method
tf.disposeVariables();
// Printing output
console.log("Variables disposed.")
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Declaring two tensors
var t1 = tf.tensor([1.1, 2.1, 3.1]);
var t2 = tf.tensor([null, 0, -2]);
// Calling dispose() and disposeVariables()
// method
tf.dispose(t2);
tf.disposeVariables();
// Printing outputs
console.log(t1);
console.log(t2);
输出:
Variables disposed.
示例 2:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Declaring two tensors
var t1 = tf.tensor([1.1, 2.1, 3.1]);
var t2 = tf.tensor([null, 0, -2]);
// Calling dispose() and disposeVariables()
// method
tf.dispose(t2);
tf.disposeVariables();
// Printing outputs
console.log(t1);
console.log(t2);
输出:
Tensor
[1.1, 2.0999999, 3.0999999]
An error occured on line: 15
Tensor is disposed.
在这里,张量 t2 在处理时发生了错误。
参考: https://js.tensorflow.org/api/latest/#disposeVariables