Tensorflow.js tf.layers getWeights() Method Last Updated : 22 Apr, 2022 Comments Improve Suggest changes Like Article Like Report 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.layers.Layer.getWeights() function is used to get the values of the weights of a tensor. Syntax: getWeights( trainableOnly? ) Parameters: trainableOnly(boolean): If true, the function will return only the values of weights that are trainable. Return Value: It returns a tf.Tensor Example 1: JavaScript // Creating a model const model = tf.sequential(); // Adding layers model.add(tf.layers.dense({units: 2, inputShape: [5]})); model.add(tf.layers.dense({units: 3})); model.compile({loss: 'categoricalCrossentropy', optimizer: 'sgd'}); // Printing the weights of the layers model.layers[0].getWeights()[0].print() model.layers[0].getWeights()[1].print() Output: Tensor [[-0.4756567, 0.2925433 ], [0.3505997 , -0.5043278], [0.5344347 , 0.2662918 ], [-0.1357223, 0.2435055 ], [-0.6059403, 0.1990891 ]] Tensor [0, 0] Example 2: JavaScript const tf = require("@tensorflow/tfjs") // Creating a model const model = tf.sequential(); // Adding layers model.add(tf.layers.dense({units: 1, inputShape: [10]})); model.add(tf.layers.dense({units: 3})); // Setting new weights model.layers[0].setWeights([tf.zeros([10, 1]), tf.ones([1])]); model.compile({loss: 'categoricalCrossentropy', optimizer: 'sgd'}); // Printing the weights of the layers model.layers[0].getWeights()[0].print() model.layers[0].getWeights()[1].print() Output: Tensor [[0], [0], [0], [0], [0], [0], [0], [0], [0], [0]] Tensor [1] Reference: https://js.tensorflow.org/api/latest/#tf.layers.Layer.getWeights Comment More infoAdvertise with us Next Article Tensorflow.js tf.layers getWeights() Method P parasmadan15 Follow Improve Article Tags : JavaScript Web Technologies Tensorflow.js TensorFlow.js-Classes TensorFlow.js-layers +1 More Similar Reads Tensorflow.js tf.layers addWeight() 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 .addWeight() function is used add a variable of weight to the stated layer. Syntax: addWeight(name, shape, dtype?, 3 min read Tensorflow.js tf.layers getConfig() 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 getConfig() function is used to get the configuration of a layer. Syntax: getConfig() Parameters: This function does not 2 min read Tensorflow.js tf.layers 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 weights of the layers stated. Moreover, it decrease the stated layer obje 2 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 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 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 countParams() 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 .countParams() function is used to find the absolute count of numbers such as float32, int32 in the stated weights. 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 Tensorflow.js tf.LayersModel class .getLayer() 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 .getLayer() function is used to fetch a layer that is based upon either its name (which must be unique) or else an 2 min read Tensorflow.js tf.LayersModel class .fit() 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.LayersModel class .fit( ) method is used to train the model for the fixed number of epochs (iterations on a dataset). Syntax: f 4 min read Like