Tensorflow.js tf.initializers.varianceScaling() Function
Last Updated :
22 Jul, 2021
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
import * as tf from "@tensorflow/tfjs"
let geek = tf.initializers.varianceScaling(33)
console.log(geek);
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
import * as tf from "@tensorflow/tfjs
const inputValue = tf.input({shape:[4]});
const funcValue = tf.initializers.varianceScaling(3)
const dense_layer_1 = tf.layers.dense({
units: 5,
activation: 'relu' ,
kernelInitialize: funcValue
});
const dense_layer_2 = tf.layers.dense({
units: 9,
activation: 'softmax'
});
const outputValue = dense_layer_2.apply(
dense_layer_1.apply(inputValue)
);
const model = tf.model({
inputs: inputValue,
outputs: outputValue
});
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
Share your thoughts in the comments
Please Login to comment...