Open In App

Tensorflow.js tf.layers.Layer Class

Last Updated : 22 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Introduction: 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.Layer class is used to extend the serialization.Serializable class. Moreover, a layer is a clustering of processes as well as weights which could be collected in order to build a tf.LayersModel. The Layers are created by applying the functions which come under the tf.layers namespace.

This tf.layers.Layer class contains ten inbuilt methods which are illustrated below:  

1. tf.layers.Layer class .apply() method: It is used to execute the Layers computation and return Tensor(s) when we call it with the tf.Tensor(s). If we call it with the tf.SymbolicTensor(s) then, it will prepare the layer for future execution.

 

Example:

Javascript




import * as tf from "@tensorflow/tfjs"
  
const denseLayer = tf.layers.dense({
units: 1,
kernelInitializer: 'ones',
useBias: false
});
  
const input = tf.ones([2, 2]);
const output = denseLayer.apply(input);
  
// Print the output
print(output)


Output:

Tensor [[2], [2]]

2. tf.layers.Layer class .countParams() method: It is used to find the absolute count of numbers such as float32, int32 in the stated weights.

Example:

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: 2, inputShape: [11]}));
  
// Calling setWeights() method
model.layers[0].setWeights(
    [tf.truncatedNormal([11, 2]), tf.zeros([2])]);
  
// Calling countParams() method and also
// Printing output
console.log(model.layers[0].countParams());


Output:

24

 

3. tf.layers.Layer class .build() method: It is used to create the weights of the layer stated. This method should be applied on every layers that hold weights. Moreover, its invoked when the apply() method is invoked in order to build the weights.

Example:

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]}));
  
// Defining input
const input = tf.input({shape: [6, 2, 6]});
  
// Calling build method with its
// parameter
model.layers[0].build([input.Shape]);
  
// Printing output
console.log(JSON.stringify(input.shape));
model.layers[0].getWeights()[0].print();


Output:

[null,6,2,6]
Tensor
    [[-0.3726568],
     [0.7343086 ],
     [-0.2459907]]

4. tf.layers.Layer class .getWeights() method: It is used to get the values of the weights of a tensor.

Example:

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]

 

5. tf.layers.Layer class .setWeights() method: It is used to set the weights of the stated layer, from the given tensors.

Example:

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: 2, inputShape: [11]}));
  
// Calling setWeights() method
model.layers[0].setWeights([tf.truncatedNormal([11, 2]), tf.zeros([2])]);
  
// Compiling the model
model.compile({loss: 'categoricalCrossentropy', optimizer: 'sgd'});
  
// Printing output using getWeights() method
model.layers[0].getWeights()[0].print();


Output:

Tensor
    [[-0.5969906, -0.1883931],
     [0.8569255 , -0.49416  ],
     [0.1157023 , 0.1150239 ],
     [-0.4052143, 1.9936075 ],
     [0.3090054 , 0.7212474 ],
     [0.4626641 , -0.7287846],
     [0.4352857 , -0.5195332],
     [0.4626429 , 0.0216295 ],
     [-0.1110666, -0.5997615],
     [-0.5083916, -0.3582681],
     [-0.2847465, 1.184485  ]]

 6. tf.layers.Layer class .addWeight() method: It is used to add a variable of weight to the stated layer.

Example:

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: 2, inputShape: [1]}));
  
// Calling addWeight() method
const res = model.layers[0].addWeight('wt_var',
        [1, 5], 'int32', tf.initializers.ones());
  
// Printing output
console.log(res);
model.layers[0].getWeights()[0].print();


Output:

{
  "dtype": "int32",
  "shape": [
    1,
    5
  ],
  "id": 1582,
  "originalName": "wt_var",
  "name": "wt_var_2",
  "trainable_": true,
  "constraint": null,
  "val": {
    "kept": false,
    "isDisposedInternal": false,
    "shape": [
      1,
      5
    ],
    "dtype": "int32",
    "size": 5,
    "strides": [
      5
    ],
    "dataId": {
      "id": 2452
    },
    "id": 2747,
    "rankType": "2",
    "trainable": true,
    "name": "wt_var_2"
  }
}
Tensor
     [[0.139703, 0.9717236],]

 

7. tf.layers.Layer class .addLoss() method: It is used to attach losses to the stated layer. Moreover, the loss might be probably conditional on a few input tensors, for example operation losses are dependent on the inputs of the stated layers.

Example:

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]}));
  
// Defining input
const input = tf.tensor1d([1, 2, 3, 4]);
  
// Calling addLoss() method with its
// parameter
const res = model.layers[0].addLoss([tf.abs(input)]);
  
// Printing output
console.log(JSON.stringify(input));
model.layers[0].getWeights()[0].print();


Output:

{"kept":false,"isDisposedInternal":false,
"shape":[4],"dtype":"float32",
"size":4,"strides":[],"dataId":{"id":82},
"id":124,"rankType":"1","scopeId":61}
Tensor
    [[0.143441  ],
     [-0.58002  ],
     [-0.5836995]]

 8. tf.layers.Layer class .computeOutputShape() method: It is used to enumerate the output shape of the stated layer. And it presumes that the layer will be created in order to match the supplied input shape.

Example:

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]}));
  
// Defining inputShape
const inputShape = [6, 2, 6];
  
// Calling computeOutputShape() method with its
// parameter
const val = model.layers[0].computeOutputShape(inputShape);
  
// Printing output
console.log(val);


Output:

6,2,1

 

9. tf.layers.Layer class .getConfig() method: It is used to get the configuration of a layer.

Example:

Javascript




const tf = require("@tensorflow/tfjs")
  
// Creating a minLayer
const minLayer = tf.layers.minimum();
  
// Getting the configuration of the layer
const config = minLayer.getConfig();
console.log(config)


Output:

{ name: 'minimum_Minimum1', trainable: true }

 10. tf.layers.Layer class .dispose() method: It is used to dispose the weights of the layers stated. Moreover, it decrease the stated layer object’s reference count via one.

Example:

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
}

Reference: https://js.tensorflow.org/api/latest/#class:layers.Layer



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads