当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Tensorflow.js tf.initializers.randomUniform()用法及代码示例


Tensorflow.js是Google开发的开源库,用于在浏览器或节点环境中运行机器学习模型和深度学习神经网络。它还可以帮助开发人员使用JavaScript语言开发ML模型,并可以直接在浏览器或Node.js中使用ML。

tf.initializers.randomUniform() 函数生成初始化为均匀分布的随机值。这些值在配置的 min-value 和 max-value 之间均匀分布。

用法:

tf.initializers.randomUniform(arguments)

参数:

  • arguments:它是一个包含下面列出的 3 个 key-values 的对象:
    1. mean:它是要生成的随机值的平均值。
    2. stddev:它是要生成的随机值的标准偏差。
    3. seed:它是随机数生成器的种子。

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



范例1:

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Initializing the .initializers.randomUniform() function
let geek = tf.initializers.randomUniform(5)
 
// Printing gain value
console.log(geek);
 
// Printing individual gain value.
console.log('\nIndividual values:\n');
console.log(geek.DEFAULT_MINVAL);
console.log(geek.DEFAULT_MAXVAL);
console.log(geek.minval);
console.log(geek.maxval);

输出:

{
  "DEFAULT_MINVAL":-0.05,
  "DEFAULT_MAXVAL":0.05,
  "minval":-0.05,
  "maxval":0.05
}

Individual values:

-0.05
0.05
-0.05
0.05

范例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.randomUniform() function.
const funcValue = tf.initializers.randomUniform(8)
 
// 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: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.1145501, 0.133405, 0.0640167, 0.2349582, 
     0.1064994, 0.0799759, 0.2665946],
     [0.1145501, 0.133405, 0.0640167, 0.2349582, 
      0.1064994, 0.0799759, 0.2665946]]

参考:https://js.tensorflow.org/api/3.6.0/#initializers.randomUniform




相关用法


注:本文由纯净天空筛选整理自thacker_shahid大神的英文原创作品 Tensorflow.js tf.initializers.randomUniform() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。