Tensorflow.js tf.memory() Function
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
const tf = require( "@tensorflow/tfjs" );
let res1
const res2 = tf.tidy(() => {
const result = tf.scalar(121);
res1 = tf.keep(result.sqrt());
});
console.log( 'numTensors: ' + tf.memory().numTensors);
|
Output:
numTensors: 1
Example 2:
Javascript
const tf = require( "@tensorflow/tfjs" );
let res1;
const res2 = tf.tidy(() => {
console.log( 'numTensors (in tidy) : ' + tf.memory().numTensors);
res1 = tf.keep(tf.tensor1d(
[1.3, 0.5, 0, NaN, null , -.5]).cos());
});
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...