Tensorflow.js tf.initializers.heNormal()函数
Tensorflow.js是谷歌开发的一个开源库,用于在浏览器或节点环境中运行机器学习模型和深度学习神经网络。它还可以帮助开发人员用 JavaScript 语言开发 ML 模型,并且可以直接在浏览器或 Node.js 中使用 ML。
tf.initializers.heNormal()函数从以零为中心的截断正态分布中抽取样本,其中stddev = sqrt(2 / fanIn)在 [-limit, limit] 内,其中 limit 为sqrt(6 / fan_in) 。请注意,fanIn 是张量权重中的输入数量。
句法:
tf.initializers.heNormal(arguments)
参数:
- arguments:它是一个包含种子(一个数字)的对象,它是随机数生成器的种子/数字。
返回值:它返回tf.initializers.Initializer
示例 1:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Initializing the .initializers.heNormal()
// function
const geek = tf.initializers.heNormal(7)
// Printing gain
console.log(geek);
console.log('\nIndividual values:\n');
console.log(geek.scale);
console.log(geek.mode);
console.log(geek.distribution);
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.heNormal() function
const funcValue = tf.initializers.heNormal(3)
// Creating dense layer 1
const dense_layer_1 = tf.layers.dense({
units: 7,
activation: 'relu',
kernelInitialize: funcValue
});
// Creating dense layer 2
const dense_layer_2 = tf.layers.dense({
units: 8,
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();
输出:
{
"scale": 2,
"mode": "fanIn",
"distribution": "normal"
}
Individual values:
2
fanIn
normal
示例 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.heNormal() function
const funcValue = tf.initializers.heNormal(3)
// Creating dense layer 1
const dense_layer_1 = tf.layers.dense({
units: 7,
activation: 'relu',
kernelInitialize: funcValue
});
// Creating dense layer 2
const dense_layer_2 = tf.layers.dense({
units: 8,
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.0802892, 0.1482767, 0.1004469, 0.1141223, 0.218376, 0.1217001, 0.139549, 0.0772399],
[0.0802892, 0.1482767, 0.1004469, 0.1141223, 0.218376, 0.1217001, 0.139549, 0.0772399]]
参考: https://js.tensorflow.org/api/latest/#initializers.heNormal