Open In App

Tensorflow.js tf.randomGamma() Function

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:



Return: It returns tf.Tensor

Example 1:




// 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:




// 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:




// 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:




// 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


Article Tags :