Tensorflow.js tf.dispose() Function
Last Updated :
22 Apr, 2022
Tensorflow.js is an open-source library developed by Google brain team for running deep learning neural networks in the browser or node environment.
The .dispose() function is used to dispose a tensor to the memory.
Syntax:
tensor.dispose()
Parameters: this method don’t have any parameters
Return value: void
Example 1: In this example, we will dispose a tensor using tf.dispose function.
Javascript
import * as tf from "@tensorflow/tfjs"
var tr = tf.tensor([1, 2, 3, 4, 5, 6, 7]);
tr.dispose()
tr.print()
|
Output:
An error occurred
Tensor is disposed
Example 2: In this example, we will create a rank-2 tensor and try to print to before and after using tf.dispose function.
Javascript
import * as tf from "@tensorflow/tfjs"
var tr2d = tf.tensor2d([1, 2, 3, 4], [2, 2]);
tr2d.print()
tr2d.dispose()
tr2d.print()
|
Output:
"Tensor
[[1, 2],
[3, 4]]"
Example 3: In this example, we are creating a tensor with value, shape, and dataType. Then, we will dispose it using tf.dispose() function:
Javascript
import * as tf from "@tensorflow/tfjs"
var value = [ '1' , '2' , '3' , '4' , '5' , '6' ]
var shape = [2, 3]
var d_Type = 'string'
var tr3d = tf.tensor(value, shape, d_Type)
tr3d.print()
|
Output:
"Tensor
[['1', '2', '3'],
['4', '5', '6']]"
Reference: https://js.tensorflow.org/api/latest/#dispose
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...