當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Tensorflow.js tf.time()用法及代碼示例

Tensorflow.js 是一個由 Google 開發的開源庫,用於在瀏覽器或節點環境中運行機器學習模型以及深度學習神經網絡。

tf.time() 函數用於執行所述函數 f() 並返回一個承諾,以便確定時間細節。結果是一個對象以及諸如 Wall 執行時間、內核執行時間之類的屬性,不包括數據傳輸。如果使用了 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}`
);

輸出:

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




相關用法


注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Tensorflow.js tf.time() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。