Friday, July 3, 2026
HomeLanguagesJavascriptTensorflow.js tf.Tensor class .dispose() Method

Tensorflow.js tf.Tensor class .dispose() Method

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

The .dispose() function is used to dispose the stated tf.Tensor from the memory.

Syntax:

dispose()

Parameters: This method does not hold any parameters.

Return Value: It returns void.

Example 1:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Creating a tensor
const t = tf.tensor([6, 7]);
 
// Calling dispose() method
t.dispose();
 
// Printing output
console.log("Tensor Disposed.")


Output:

Tensor Disposed.

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Creating a 2d tensor
const tn = tf.tensor2d([6, 7], [1, 2]);
 
// Calling dispose() method
const y = tn.dispose();
 
// Printing output
console.log(tn)


Output:

An error occurred on line: 11
Tensor is disposed.

Here, an error occurred while printing the tensor output as the tensor is already being disposed of.

Reference: https://js.tensorflow.org/api/latest/#tf.Tensor.dispose

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32519 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6900 POSTS0 COMMENTS
Nicole Veronica
12015 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12110 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6966 POSTS0 COMMENTS