Open In App

Tensorflow.js tf.initializers.varianceScaling() Function

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. It also helps the developers to develop ML models in JavaScript language and can use ML directly in the browser or in Node.js.

The tf.initializers.varianceScaling() function is capable of adjusting its scale to the shape of weights. Using the value of distribution=NORMAL, samples are drawn from a truncated normal distribution that has center at 0, with stddev = sqrt(scale / n) . Note that the value of n varies as:



Syntax:

tf.initializers.varianceScaling(arguments)

Parameters: It takes an object as arguments that contains 3 key-values listed below:



Returns value: It returns tf.initializers.Initializer

Example 1:




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Initializing the .initializers.varianceScaling()
// function
let geek = tf.initializers.varianceScaling(33)
 
// Printing gain value
console.log(geek);
 
// Printing individual gain value.
console.log('\nIndividual values:\n');
console.log(geek.scale);
console.log(geek.mode);
console.log(geek.distribution);

Output: 

{
  "scale": 1,
  "mode": "fanIn",
  "distribution": "normal"
}

Individual values:

1
fanIn
normal

Example 2: 




// Importing the tensorflow.Js library
import * as tf from "@tensorflow/tfjs
 
// Defining the input value
const inputValue = tf.input({shape:[4]});
 
// Initializing tf.initializers.varianceScaling() function
const funcValue = tf.initializers.varianceScaling(3)
 
// Creating dense layer 1
const dense_layer_1 = tf.layers.dense({
    units: 5,
    activation: 'relu',
    kernelInitialize: funcValue
});
 
// Creating dense layer 2
const dense_layer_2 = tf.layers.dense({
    units: 9,
    activation: 'softmax'
});
 
// Output
const outputValue = dense_layer_2.apply(
    dense_layer_1.apply(inputValue)
);
 
// Creation the model.
const model = tf.model({
    inputs: inputValue,
    outputs: outputValue
});
 
// Predicting the output.
model.predict(tf.ones([2, 4])).print();

Output: 

Tensor
    [[0.0687333, 0.1549079, 0.0899771, 0.084183, 
      0.1593787, 0.1488634, 0.0884578, 0.073244, 0.1322549],
     [0.0687333, 0.1549079, 0.0899771, 0.084183, 
      0.1593787, 0.1488634, 0.0884578, 0.073244, 0.1322549]]

Reference: https://js.tensorflow.org/api/3.6.0/#initializers.varianceScaling

 


Article Tags :