📜  Tensorflow.js tf.randomGamma()函数

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

Tensorflow.js tf.randomGamma()函数

Tensorflow.js是谷歌开发的一个开源库,用于在浏览器或节点环境中运行机器学习模型和深度学习神经网络。

tf.randomGamma()函数用于创建一个 tf.Tensor,其值从伽马分布中采样。

句法:

tf.randomGamma(shape, alpha, beta, dtype, seed)

参数:该函数接受三个参数,如下图所示:

  • shape:定义输出张量形状的整数数组。
  • alpha : 伽马分布的形状参数。
  • beta :这是一个可选参数。伽马分布的逆尺度参数。默认值为 1。
  • dtype:输出的数据类型。可能的数据类型值是“float32”或“int32”。它也是一个可选参数。
  • 种子:这是一个可选参数。随机数生成器的种子。

返回:它返回 tf.Tensor

示例 1:

Javascript
// Importting the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor with values sampled 
// from a gamma distribution
const x=tf.randomGamma([5], 0);
  
// Printing the tensor
x.print();


Javascript
// Importting the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor with values sampled 
// from a gamma distribution
const x=tf.randomGamma([5], 1);
  
// Printing the tensor
x.print();


Javascript
// Importting the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor with values sampled
// from a gamma distribution
const x=tf.randomGamma([2,2], 1);
  
// Printing the tensor
x.print();


Javascript
// Importting the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor with values sampled 
// from a gamma distribution
const x=tf.randomGamma([5], 1,2,'int32',98);
  
// Printing the tensor
x.print();


输出:

Tensor
    [0, 0, 0, 0, 0]

示例 2:

Javascript

// Importting the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor with values sampled 
// from a gamma distribution
const x=tf.randomGamma([5], 1);
  
// Printing the tensor
x.print();

输出:

Tensor
    [1.4808178, 1.6668015, 0.9527208, 1.6024575, 1.6021353]

示例 3:

Javascript

// Importting the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor with values sampled
// from a gamma distribution
const x=tf.randomGamma([2,2], 1);
  
// Printing the tensor
x.print();

输出:

Tensor
    [[0.1157758, 1.4427431],
     [0.4978852, 0.1617882]]

示例 4:

Javascript

// Importting the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor with values sampled 
// from a gamma distribution
const x=tf.randomGamma([5], 1,2,'int32',98);
  
// Printing the tensor
x.print();

输出:

Tensor
    [0, 1, 4, 0, 1]

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