Tensorflow.js tf.time()函数
Tensorflow.js 是一个由谷歌开发的开源库,用于在浏览器或节点环境中运行机器学习模型以及深度学习神经网络。
tf.time()函数用于执行指定的函数f() 并返回一个 promise,以便确定时间细节。结果是一个对象以及诸如Wall execution time , Kernel execution time等属性,不包括数据传输。如果使用了WebGL后端并且查询代码不可用,则此方法将抛出错误对象。此外, WebGL后端将提供更多属性,例如uploadWaitMs即 CPU 在纹理上传时的阻塞时间,以及downloadWaitMs即 CPU 在纹理下载上的阻塞时间。
句法:
tf.time(f)
参数:此函数接受单个参数,如下所述:
- f:既定的执行函数和时间。
返回值:它返回一个TimingInfo的 Promise。
示例 1:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Using truncatedNormal() function in order to
// define the parameter for the function used
const p = tf.truncatedNormal([10, 10]);
// Calling time() method and also using
// dot() method
const tm = await tf.time(() => p.dot(p));
// Printing output
console.log(`Kernel execution time:
${tm.kernelMs}, Wall execution time: ${tm.wallMs}`
);
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Calling time() method and also using
// square() as well as ones() method
const t = await tf.time(() => tf.square(tf.ones([15, 21])));
// Printing output for WebGL backend
console.log(`uploadWaitMs: ${t.uploadWaitMs},
downloadWaitMs: ${t.downloadWaitMs}`);
输出:
Kernel execution time: 0.048498, Wall execution time: 85.19999992847443
示例 2:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Calling time() method and also using
// square() as well as ones() method
const t = await tf.time(() => tf.square(tf.ones([15, 21])));
// Printing output for WebGL backend
console.log(`uploadWaitMs: ${t.uploadWaitMs},
downloadWaitMs: ${t.downloadWaitMs}`);
输出:
uploadWaitMs: 1.100000023841858, downloadWaitMs: 0
参考: https://js.tensorflow.org/api/latest/#time