Tensorflow.js tf.layers dispose() Method Last Updated : 27 Jul, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 weights of the layers stated. Moreover, it decrease the stated layer object's reference count via one. Note: Here, a layer is reference counted. Where, its reference number is raised via one for the first time its apply() method is invoked as well as when it becomes a segment of a new Node by invoking the apply() method on a tf.SymbolicTensor.Here, when a layer's reference number becomes zero then its every single weights will be disposed and the basic memory i.e. the textures assigned in WebGL will also be cleared.When the reference count of a layer is greater than zero following the deduction then the Layer weights will not be disposed.Finally, when a Layer is disposed, it cannot be utilized in the invokes like apply(), getWeights() or setWeights() any longer. Syntax: dispose() Parameters: This method has no parameters. Return Value: It returns DisposeResult. Example 1: JavaScript // Importing the tensorflow.js library import * as tf from "@tensorflow/tfjs" // Creating a model const model = tf.sequential(); // Adding a layer model.add(tf.layers.dense({units: 1, inputShape: [3]})); // Calling dispose method const val = model.layers[0].dispose(); // Printing output console.log(val); Output: { "refCountAfterDispose": 0, "numDisposedVariables": 2 } Example 2: JavaScript // Importing the tensorflow.js library //import * as tf from "@tensorflow/tfjs" // Creating a model const model = tf.sequential(); // Adding layers model.add(tf.layers.dense({units: 1, inputShape: [5, 1]})); model.add(tf.layers.dense({units: 4})); model.add(tf.layers.dense({units: 2, inputShape: [6], batchSize: 5})); // Calling dispose() method const val1 = model.layers[0].dispose(); const val2 = model.layers[1].dispose(); const val3 = model.layers[2].dispose(); // Printing output console.log(val1); console.log(val2); console.log(val3); Output: { "refCountAfterDispose": 0, "numDisposedVariables": 2 } { "refCountAfterDispose": 0, "numDisposedVariables": 2 } { "refCountAfterDispose": 0, "numDisposedVariables": 2 } Reference: https://js.tensorflow.org/api/latest/#tf.layers.Layer.dispose Comment More infoAdvertise with us Next Article Tensorflow.js tf.layers build() Method N nidhi1352singh Follow Improve Article Tags : JavaScript Web Technologies Tensorflow.js Similar Reads Tensorflow.js tf.layers addLoss() 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 .addLoss() function is used to attach losses to the stated layer. Moreover, the loss might be probably conditional 2 min read Tensorflow.js tf.layers apply() Method 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. The tf.layers apply() method is used to execute the Layers computation and return Tensor(s) when we call it with the tf.Tensor(s). If 1 min read Tensorflow.js tf.layers build() 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 .build() function is used to create the weights of the layer stated. This method should be applied on every layers 2 min read Tensorflow.js tf.layers getWeights() Method 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. It also helps the developers to develop ML models in JavaScript language and can use ML directly in the browser or in Node.js. The tf. 2 min read Tensorflow.js tf.LayersModel Class 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.LayerModel class is used to training, interface and evaluation of model. It have many method for training, evaluatio 2 min read Tensorflow.js tf.layers computeOutputShape() 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 .computeOutputShape() function is used to enumerate the output shape of the stated layer. And it presumes that the 2 min read Like