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


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

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

Tensorflow.js tf.memory() 函數用於獲取程序當前時間的內存信息。此函數返回具有以下屬性的 memoryInfo 對象:

  • numBytes:它指定當前時間分配的未處理字節數。
  • numTensors:它指定分配的唯一張量的數量。
  • 數據緩衝區數: 指定當前時間分配的未處理的唯一數據緩衝區的數量,大於或等於張量的數量。
  • 不可靠:不可靠僅當內存使用不可靠時才為 True。
  • reasons:它指定了一個字符串數組,代表了內存不可靠的原因。

WebGL 屬性:

  • numBytesInGPU:它指定當前時間在 GPU 中分配的未處理字節總數。

用法:

tf.memory() 



參數:該函數不接受任何參數。

返回值:它返回一個 memoryInfo 對象。

範例1:打印分配張量編號的示例。

Javascript


// Importing the tensorflow.js library 
const tf = require("@tensorflow/tfjs"); 
  
// Declaring a variable
let res1 
    
// Calling tidy method
const res2 = tf.tidy(() => {
       
// Defining result parameter
const result = tf.scalar(121);
       
// Calling tf.keep() method
res1 = tf.keep(result.sqrt());
  
});
    
// Printing the number of tensors allocated at this time
console.log('numTensors:' + tf.memory().numTensors);

輸出:

numTensors:1

範例2:

Javascript


// Importing the tensorflow.js library 
const tf = require("@tensorflow/tfjs"); 
  
// Declaring a variable
let res1;
    
// Calling tidy method
const res2 = tf.tidy(() => {
  console.log('numTensors (in tidy):' + tf.memory().numTensors);
       
  // Calling tf.keep() method with its
  // parameter
  res1 = tf.keep(tf.tensor1d(
    [1.3, 0.5, 0, NaN, null, -.5]).cos());
});
  
// Printing memory information
console.log('numBytes:' + tf.memory().numBytes);
console.log('numTensors (outside tidy):' + tf.memory().numTensors);
console.log('numDataBuffers:' + tf.memory().numDataBuffers);

輸出:

numTensors (in tidy):1
numBytes:28
numTensors (outside tidy):2
numDataBuffers:2

參考:https://js.tensorflow.org/api/latest/#memory




相關用法


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