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 Must Do Coding Questions Company-wise nidhi1352singh Follow Improve Article Tags : JavaScript Web Technologies Tensorflow.js Similar Reads Interview Preparation Interview Preparation For Software Developers Must Coding Questions - Company-wise Must Do Coding Questions - Topic-wise Company-wise Practice Problems Company Preparation Competitive Programming Software Design-Patterns Company-wise Interview Experience Experienced - Interview Experiences Internship - Interview Experiences Practice @Geeksforgeeks Problem of the Day Topic-wise Practice Difficulty Level - School Difficulty Level - Basic Difficulty Level - Easy Difficulty Level - Medium Difficulty Level - Hard Leaderboard !! Explore More... Data Structures Arrays Linked List Stack Queue Binary Tree Binary Search Tree Heap Hashing Graph Advance Data Structures Matrix String All Data Structures Algorithms Analysis of Algorithms Searching Algorithms Sorting Algorithms Pattern Searching Geometric Algorithms Mathematical Algorithms Randomized Algorithms Greedy Algorithms Dynamic Programming Divide & Conquer Backtracking Branch & Bound All Algorithms Programming Languages C C++ Java Python C# Go Lang SQL PHP Scala Perl Kotlin Web Technologies HTML CSS JavaScript Bootstrap Tailwind CSS AngularJS ReactJS jQuery NodeJS PHP Web Design Web Browser File Formats Computer Science Subjects Operating Systems DBMS Computer Network Computer Organization & Architecture TOC Compiler Design Digital Elec. & Logic Design Software Engineering Engineering Mathematics Data Science & ML Complete Data Science Course Data Science Tutorial Machine Learning Tutorial Deep Learning Tutorial NLP Tutorial Machine Learning Projects Data Analysis Tutorial Tutorial Library Python Tutorial Django Tutorial Pandas Tutorial Kivy Tutorial Tkinter Tutorial OpenCV Tutorial Selenium Tutorial GATE CS GATE CS Notes Gate Corner Previous Year GATE Papers Last Minute Notes (LMNs) Important Topic For GATE CS GATE Course Previous Year Paper: CS exams Like