📜  Tensorflow.js tf.randomNormal()函数

📅  最后修改于: 2022-05-13 01:56:52.866000             🧑  作者: Mango

Tensorflow.js tf.randomNormal()函数

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

tf.randomNormal()函数用于创建具有从正态分布采样的值的 tf.Tensor。

句法:

tf.randomNormal (shape, mean, stdDev, dtype, seed)

参数:

  • shape:定义输出张量形状的整数数组。
  • 意思是:这是一个可选参数。正态分布的平均值。
  • stdDev:它也是一个可选参数。正态分布的标准差。
  • dtype:输出的数据类型。可能的数据类型值是“float32”或“int32”。它也是一个可选参数。
  • 种子:这是一个可选参数。随机数生成器的种子。

返回值:返回 tf.Tensor。

示例 1:

Javascript
// Creating the tensor with values 
// sampled from a normal distribution
const x = tf.randomNormal([5]);
  
// Printing the tensor
x.print();


Javascript
// Creating the tensor with values 
// sampled from a normal distribution
const x = tf.randomNormal([2, 2]);
  
// Printing the tensor
x.print();


Javascript
// Creating the tensor with values 
// sampled from a normal distribution
const x=tf.randomNormal([5], 5, 1, 'int32', 2);
  
// Printing the tensor
x.print();


输出:

Tensor
   [1.5322036, 2.2685387, -0.4921667, 1.1309422, 1.470457]

示例 2:

Javascript

// Creating the tensor with values 
// sampled from a normal distribution
const x = tf.randomNormal([2, 2]);
  
// Printing the tensor
x.print();

输出:

Tensor
   [[1.9162624 , -0.9760998],
    [-0.2262698, -2.1717837]]

示例 3

Javascript

// Creating the tensor with values 
// sampled from a normal distribution
const x=tf.randomNormal([5], 5, 1, 'int32', 2);
  
// Printing the tensor
x.print();

输出:

​Tensor
   [5, 7, 6, 5, 6]

参考: https://js.tensorflow.org/api/latest/#randomNormal