當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Tensorflow.js tf.initializers.glorotNormal()用法及代碼示例

Tensorflow.js是Google開發的開源庫,用於在瀏覽器或節點環境中運行機器學習模型和深度學習神經網絡。它還可以幫助開發人員使用JavaScript語言開發ML模型,並可以直接在瀏覽器或Node.js中使用ML。

tf.initializers.glorotNormal() 函數從以 0 為中心且 stddev = sqrt(2 /(fan_in + fan_out)) 的截斷正態分布中提取樣本。請注意,fan_in 是張量權重中的輸入數量,而 fan_out 是張量權重中的輸出數量。

用法:

tf.initializers.glorotNormal(arguments)

參數:

  • arguments:它是一個包含種子(一個數字)的對象,它是隨機數生成器的種子/數字。

返回值:它返回 tf.initializers.Initializer。



範例1:

Javascript


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

輸出:

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

Individual values:

1
fanAvg
normal

範例2:

Javascript


// Importing the tensorflow.Js lbrary
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();

輸出:

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

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




相關用法


注:本文由純淨天空篩選整理自cyber_psych0大神的英文原創作品 Tensorflow.js tf.initializers.glorotNormal() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。