Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment.
The tf.randomGamma() function is used to create a tf.Tensor with values sampled from a gamma distribution.
Syntax:
tf.randomGamma(shape, alpha, beta, dtype, seed)
Parameter: This function accepts three parameters which are illustrated below:
- shape: An array of integers defining the shape of the output tensor.
- alpha: The shape parameter of the gamma distribution.
- beta: It is an optional argument. The inverse scale parameter of the gamma distribution. The default value is 1.
- dtype: The data type of the output. The values of datatype possible are ‘float32’ or ‘int32’. It is also an optional argument.
- seed: It is an optional argument. The seed for the random number generator.
Return: It returns tf.Tensor
Example 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(); |
Output:
Tensor [0, 0, 0, 0, 0]
Example 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(); |
Output:
Tensor [1.4808178, 1.6668015, 0.9527208, 1.6024575, 1.6021353]
Example 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(); |
Output:
Tensor [[0.1157758, 1.4427431], [0.4978852, 0.1617882]]
Example 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(); |
Output:
Tensor [0, 1, 4, 0, 1]
Reference:https://js.tensorflow.org/api/latest/#randomGamma