Open In App

Tensorflow.js tf.initializers.glorotNormal() 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.glorotNormal() function extract samples from a truncated normal distribution which is been centered at 0 with stddev = sqrt(2 / (fan_in + fan_out)). Note that the fan_in is the number of inputs in the tensor weight and the fan_out is the number of outputs in the tensor weight.



Syntax:

tf.initializers.glorotNormal(arguments)

Parameters:



Return value: It returns tf.initializers.Initializer.

Example 1:




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Initializing the .initializers.glorotNormal() function
console.log(tf.initializers.glorotNormal(9));
 
// Printing Individual gainvalues
console.log('\nIndividual values:\n');
console.log(tf.initializers.glorotNormal(9).scale);
console.log(tf.initializers.glorotNormal(9).mode);
console.log(tf.initializers.glorotNormal(9).distribution);

Output:

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

Individual values:

1
fanAvg
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.glorotNormal() function
const funcValue = tf.initializers.glorotNormal(9)
 
// Creating dense layer 1
const dense_layer_1 = tf.layers.dense({
    units: 11,
    activation: 'relu',
    kernelInitialize: funcValue
});
 
// Creating dense layer 2
const dense_layer_2 = tf.layers.dense({
    units: 7,
    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.1004296, 0.1564845, 0.1716817, 0.1526613, 
            0.1739253, 0.1059624, 0.1388552],
     [0.1004296, 0.1564845, 0.1716817, 0.1526613, 
            0.1739253, 0.1059624, 0.1388552]]

Reference: https://js.tensorflow.org/api/latest/#initializers.glorotNormal


Article Tags :