Tensorflow.js tf.LayersModel class .getLayer() Method Last Updated : 04 Aug, 2021 Comments Improve Suggest changes 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 .getLayer() function is used to fetch a layer that is based upon either its name (which must be unique) or else an index. Where, indices are relying on the order of the horizontal graph traversal in bottom-up fashion. Moreover, in case both name as well as index are given, then the index will take priority. Syntax: getLayer(name?, index?) Parameters: name: It is the stated name of the layer. It is optional and is of type string.index: It is the stated index of the layer. It is optional and is of type number. Return Value: It returns tf.layers.Layer. Example 1: JavaScript // Importing the tensorflow.js library import * as tf from "@tensorflow/tfjs" // Defining model const model = tf.sequential(); // Adding a layer model.add(tf.layers.dense({units: 4, inputShape: [1]})); // Calling getLayer() method const layer_0 = model.getLayer(null, 0); // Printing weights of the layer_0 // using getWeights() method layer_0.getWeights()[0].print(); Output: Tensor [[-0.0678914, 0.6647689, -0.3708572, -0.1764591],] Example 2: JavaScript // Importing the tensorflow.js library import * as tf from "@tensorflow/tfjs" // Defining model const model = tf.sequential(); // Adding layers model.add(tf.layers.dense({units: 4, inputShape: [1]})); model.add(tf.layers.dense({units: 2, inputShape: [3], activation: 'relu6'})); model.add(tf.layers.dense({units: 3, inputShape: [5], activation: 'sigmoid'})); // Calling getLayer() method const layer_0 = model.getLayer(NaN, 0); const layer_1 = model.getLayer('denselayer', 1); const layer_2 = model.getLayer(undefined, 2); // Printing number of numbers in the weights // of the layer_0, layer_1, and layer_2 // using countParams() method console.log(layer_0.countParams()); console.log(layer_1.countParams()); console.log(layer_2.countParams()); Output: 8 10 9 Reference: https://js.tensorflow.org/api/latest/#tf.LayersModel.getLayer Comment More infoAdvertise with us Next Article Tensorflow.js tf.LayersModel class .getLayer() Method nidhi1352singh Follow Improve Article Tags : JavaScript Web Technologies Tensorflow.js Similar Reads Tensorflow.js tf.LayersModel Class .evaluate() 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 .evaluate() function is used to find the measure of loss and the values of metrics in favor of the prototype in tes 3 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 Tensorflow.js tf.LayersModel class .compile() 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 .compile() function configures and makes the model for training and evaluation process. By calling .compile() function we prepare 4 min read Tensorflow.js tf.LayersModel class .fitDataset() 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. Tensorflow.js tf.LayersModel class .fitDataset() method is used to trains the layer model by using a dataset object. Syntax: LayerMode 4 min read Tensorflow.js tf.LayersModel class .evaluateDataset() 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 .evaluateDataset() function is used to evaluate the stated model by means of a stated dataset object. Note: This me 3 min read Like