Open In App

Tensorflow.js tf.initializers.varianceScaling() Function

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

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:

  • It is the number of inputs in the tensor weight, if the value of mode = FAN_IN.
  • It is the number of outputs in the tensor weight, if the value of mode = FAN_OUT.
  • It is the average of outputs and inputs in the tensor weight, if the value of mode = FAN_AVG.

Syntax:

tf.initializers.varianceScaling(arguments)

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

  • scale: It is the scaling factor. It is a positive float value.
  • mode: It is the fanning mode for the outputs and inputs.
  • distribution: It is the probabilistic distribution of the values.
  • seed: It is the random number generator seed.

Returns value: It returns tf.initializers.Initializer

Example 1:

Javascript




// 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: 

Javascript




// 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

 



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

Similar Reads