Open In App

Tensorflow.js tf.layers addWeight() Method

Last Updated : 22 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
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 .addWeight() function is used add a variable of weight to the stated layer.

Syntax:

addWeight(name, shape, dtype?, initializer?, 
               regularizer?, trainable?, constraint?)

Parameters:

  • name: It is the stated name of new variable of weight and is of type string.
  • shape: It is the stated shape of the weight. It is of type (null | number)[].
  • dtype: It is the stated datatype of the weight. It is optional and can be of type float32, int32, bool, complex64, or string.
  • initializer: It is the stated initializer instance. It is optional and is of type tf.initializers.Initializer.
  • regularizer: It is the stated regularizer instance. It is optional and is of type Regularizer.
  • trainable: It states if the weight should be instructed through backprop or not by presuming that the layer itself is similarly trainable. It is optional and is of type boolean.
  • constraint: It is an optional trainable and is of type tf.constraints.Constraint.

Return Value: It returns LayerVariable.

Example 1:

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],]

Here, getWeights() method is used to print the weights of the layer specified.

Example 2:

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]}));
model.add(tf.layers.dense({units: 3}));
  
// Calling addWeight() method
const res1 = model.layers[0].addWeight('w_v'
    [1.2, 1.3], 'float32', tf.initializers.zeros(), true);
  
const res2 = model.layers[1].addWeight('wv'
    ["a", "b"], 'int32', tf.initializers.ones(), false);
  
// Printing outputs
console.log(res1);
console.log(res2);
model.layers[0].getWeights()[0].print();
model.layers[1].getWeights()[0].print();


Output:

{
  "dtype": "float32",
  "shape": [
    1.2,
    1.3
  ],
  "id": 7,
  "originalName": "w_v",
  "name": "w_v",
  "trainable_": true,
  "constraint": null,
  "val": {
    "kept": false,
    "isDisposedInternal": false,
    "shape": [
      1.2,
      1.3
    ],
    "dtype": "float32",
    "size": 1.56,
    "strides": [
      1.3
    ],
    "dataId": {
      "id": 4
    },
    "id": 9,
    "rankType": "2",
    "trainable": true,
    "name": "w_v"
  }
}
{
  "dtype": "int32",
  "shape": [
    "a",
    "b"
  ],
  "id": 8,
  "originalName": "wv",
  "name": "wv",
  "trainable_": true,
  "constraint": null,
  "val": {
    "kept": false,
    "isDisposedInternal": false,
    "shape": [
      "a",
      "b"
    ],
    "dtype": "int32",
    "size": null,
    "strides": [
      "b"
    ],
    "dataId": {
      "id": 5
    },
    "id": 11,
    "rankType": "2",
    "trainable": true,
    "name": "wv"
  }
}
Tensor
     [[0.835237, 0.960075],]
Tensor
    [[0.4747705 , -0.6734858, 1.1417971],
     [-0.8185477, 0.1940626 , -0.98313 ]]

Here, tf.initializers.zeros() method is used to produce tensors that are initialized to zero and tf.initializers.ones() method is used to produce tensors that are initialized to one.

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads