Tensorflow.js tf.Tensor 类 .dispose() 方法
Tensorflow.js 是由谷歌开发的开源库,用于在浏览器或节点环境中运行机器学习模型以及深度学习神经网络。
.dispose()函数用于从内存中释放指定的tf.Tensor 。
句法:
dispose()
参数:此方法不保存任何参数。
返回值:返回void。
示例 1:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Creating a tensor
const t = tf.tensor([6, 7]);
// Calling dispose() method
t.dispose();
// Printing output
console.log("Tensor Disposed.")
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Creating a 2d tensor
const tn = tf.tensor2d([6, 7], [1, 2]);
// Calling dispose() method
const y = tn.dispose();
// Printing output
console.log(tn)
输出:
Tensor Disposed.
示例 2:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Creating a 2d tensor
const tn = tf.tensor2d([6, 7], [1, 2]);
// Calling dispose() method
const y = tn.dispose();
// Printing output
console.log(tn)
输出:
An error occured on line: 11
Tensor is disposed.
这里,在打印张量输出时发生错误,因为张量已经被处理掉了。
参考: https://js.tensorflow.org/api/latest/#tf.Tensor.dispose