Wednesday, July 3, 2024
HomeLanguagesJavascriptTensorflow.js tf.layers addWeight() Method

Tensorflow.js tf.layers addWeight() 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 .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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Thapelo Manthata
I’m a desktop support specialist transitioning into a SharePoint developer role by day and Software Engineering student by night. My superpowers include customer service, coding, the Microsoft office 365 suite including SharePoint and power platform.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments