当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。