Monday, November 17, 2025
HomeLanguagesJavascriptTensorflow.js tf.randomGamma() Function

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:

  • 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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32402 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6769 POSTS0 COMMENTS
Nicole Veronica
11920 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11990 POSTS0 COMMENTS
Shaida Kate Naidoo
6897 POSTS0 COMMENTS
Ted Musemwa
7150 POSTS0 COMMENTS
Thapelo Manthata
6851 POSTS0 COMMENTS
Umr Jansen
6843 POSTS0 COMMENTS