Tensorflow.js tf.engine()函数
Tensorflow.js 是一个由谷歌开发的开源库,用于在浏览器或节点环境中运行机器学习模型以及深度学习神经网络。
.engine()函数用于返回全局引擎,该引擎保存每个张量以及后端的路径。
句法:
tf.engine()
参数:此方法不包含任何参数。
返回值:返回Engine。
示例 1:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Calling engine() and startScope()
// method
tf.engine().startScope();
// Calling ones() method
const res = tf.ones([200, 250]);
// Printing output
console.log(res);
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Calling engine() and startScope()
// method
tf.engine().startScope();
// Calling ones() method
const t1 = tf.ones([200, 250]);
// Calling tidy() and min() method
// with respect to t1
const t2 = tf.tidy(() => t1.min());
// Calling engine() and endScope()
// method
tf.engine().endScope();
// Printing output
console.log(t2);
输出:这里,范围没有结束,所以返回一个张量。
Tensor
[[1, 1, 1, ..., 1, 1, 1],
[1, 1, 1, ..., 1, 1, 1],
[1, 1, 1, ..., 1, 1, 1],
...,
[1, 1, 1, ..., 1, 1, 1],
[1, 1, 1, ..., 1, 1, 1],
[1, 1, 1, ..., 1, 1, 1]]
示例 2:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Calling engine() and startScope()
// method
tf.engine().startScope();
// Calling ones() method
const t1 = tf.ones([200, 250]);
// Calling tidy() and min() method
// with respect to t1
const t2 = tf.tidy(() => t1.min());
// Calling engine() and endScope()
// method
tf.engine().endScope();
// Printing output
console.log(t2);
输出:在这里,作用域结束,因此结果张量正在被处理。
An error occured on line: 20
Tensor is disposed.
参考: https://js.tensorflow.org/api/latest/#engine