Tensorflow.js tf.LayersModel class .getLayer() Method
Last Updated :
04 Aug, 2021
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
import * as tf from "@tensorflow/tfjs"
const model = tf.sequential();
model.add(tf.layers.dense({units: 4, inputShape: [1]}));
const layer_0 = model.getLayer( null , 0);
layer_0.getWeights()[0].print();
|
Output:
Tensor
[[-0.0678914, 0.6647689, -0.3708572, -0.1764591],]
Example 2:
Javascript
import * as tf from "@tensorflow/tfjs"
const model = tf.sequential();
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' }));
const layer_0 = model.getLayer(NaN, 0);
const layer_1 = model.getLayer( 'denselayer' , 1);
const layer_2 = model.getLayer(undefined, 2);
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...