Skip to content
Related Articles

Related Articles

Tensorflow.js tf.memory() Function

View Discussion
Improve Article
Save Article
  • Last Updated : 09 Aug, 2021

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:

  • numBytes: It specifies the number of undisposed bytes allocated at the current time.
  • numTensors: It specifies the number of unique tensors allocated.
  • numDataBuffers: It specifies the number of undisposed unique data buffers allocated at the current time, which is greater than or equal to the number of tensors.
  • unreliable: unreliable is True only and only if the memory usage is unreliable.
  • reasons: It specifies an array of string, which represents the reasons why memory is unreliable.

WebGL Properties:

  • numBytesInGPU: It specifies the total number of undisposed bytes allocated in the GPU at the current time.

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.

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


Output:

numTensors: 1

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


Output:

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

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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!