Open In App

Tensorflow.js tf.memory() Function

Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment.

Tensorflow.js tf.memory() function is used to get memory info of the program at the current time. This function returns a memoryInfo object with the following properties:



WebGL Properties:

Syntax:



tf.memory() 

  
Parameters: This function do not accept any parameter.

Return value: It returns a memoryInfo object.

Example 1: Example to print number the allocated tensors.




// 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);

Output:

numTensors: 1

Example 2:




// 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);

Output:

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

Reference: https://js.tensorflow.org/api/latest/#memory


Article Tags :