alpha is the shape parameter describing the distribution(s), and beta is
the inverse scale parameter(s).
The samples are differentiable w.r.t. alpha and beta.
The derivatives are computed using the approach described in
(Figurnov et al., 2018).
Example:
samples=tf.random.gamma([10],[0.5,1.5])# samples has shape [10, 2], where each slice [:, 0] and [:, 1] represents# the samples drawn from each distributionsamples=tf.random.gamma([7,5],[0.5,1.5])# samples has shape [7, 5, 2], where each slice [:, :, 0] and [:, :, 1]# represents the 7x5 samples drawn from each of the two distributionsalpha=tf.constant([[1.],[3.],[5.]])beta=tf.constant([[3.,4.]])samples=tf.random.gamma([30],alpha=alpha,beta=beta)# samples has shape [30, 3, 2], with 30 samples each of 3x2 distributions.loss=tf.reduce_mean(tf.square(samples))dloss_dalpha,dloss_dbeta=tf.gradients(loss,[alpha,beta])# unbiased stochastic derivatives of the loss functionalpha.shape==dloss_dalpha.shape# Truebeta.shape==dloss_dbeta.shape# True
Args
shape
A 1-D integer Tensor or Python array. The shape of the output samples
to be drawn per alpha/beta-parameterized distribution.
alpha
A Tensor or Python value or N-D array of type dtype. alpha
provides the shape parameter(s) describing the gamma distribution(s) to
sample. Must be broadcastable with beta.
beta
A Tensor or Python value or N-D array of type dtype. Defaults to 1.
beta provides the inverse scale parameter(s) of the gamma
distribution(s) to sample. Must be broadcastable with alpha.
dtype
The type of alpha, beta, and the output: float16, float32, or
float64.
seed
A Python integer. Used to create a random seed for the distributions.
See
tf.random.set_seed
for behavior.
name
Optional name for the operation.
Returns
samples
a Tensor of shape
tf.concat([shape, tf.shape(alpha + beta)], axis=0) with values of type
dtype.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-26 UTC."],[],[],null,["# tf.random.gamma\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/random_ops.py#L451-L542) |\n\nDraws `shape` samples from each of the given Gamma distribution(s).\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.random.gamma`](https://www.tensorflow.org/api_docs/python/tf/random/gamma), [`tf.compat.v1.random_gamma`](https://www.tensorflow.org/api_docs/python/tf/random/gamma)\n\n\u003cbr /\u003e\n\n tf.random.gamma(\n shape,\n alpha,\n beta=None,\n dtype=../../tf/dtypes#float32,\n seed=None,\n name=None\n )\n\n`alpha` is the shape parameter describing the distribution(s), and `beta` is\nthe inverse scale parameter(s).\n| **Note:** Because internal calculations are done using `float64` and casting has `floor` semantics, we must manually map zero outcomes to the smallest possible positive floating-point value, i.e., `np.finfo(dtype).tiny`. This means that `np.finfo(dtype).tiny` occurs more frequently than it otherwise should. This bias can only happen for small values of `alpha`, i.e., `alpha \u003c\u003c 1` or large values of `beta`, i.e., `beta \u003e\u003e 1`.\n\nThe samples are differentiable w.r.t. alpha and beta.\nThe derivatives are computed using the approach described in\n(Figurnov et al., 2018).\n\n#### Example:\n\n samples = tf.random.gamma([10], [0.5, 1.5])\n # samples has shape [10, 2], where each slice [:, 0] and [:, 1] represents\n # the samples drawn from each distribution\n\n samples = tf.random.gamma([7, 5], [0.5, 1.5])\n # samples has shape [7, 5, 2], where each slice [:, :, 0] and [:, :, 1]\n # represents the 7x5 samples drawn from each of the two distributions\n\n alpha = tf.constant([[1.],[3.],[5.]])\n beta = tf.constant([[3., 4.]])\n samples = tf.random.gamma([30], alpha=alpha, beta=beta)\n # samples has shape [30, 3, 2], with 30 samples each of 3x2 distributions.\n\n loss = tf.reduce_mean(tf.square(samples))\n dloss_dalpha, dloss_dbeta = tf.gradients(loss, [alpha, beta])\n # unbiased stochastic derivatives of the loss function\n alpha.shape == dloss_dalpha.shape # True\n beta.shape == dloss_dbeta.shape # True\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `shape` | A 1-D integer Tensor or Python array. The shape of the output samples to be drawn per alpha/beta-parameterized distribution. |\n| `alpha` | A Tensor or Python value or N-D array of type `dtype`. `alpha` provides the shape parameter(s) describing the gamma distribution(s) to sample. Must be broadcastable with `beta`. |\n| `beta` | A Tensor or Python value or N-D array of type `dtype`. Defaults to 1. `beta` provides the inverse scale parameter(s) of the gamma distribution(s) to sample. Must be broadcastable with `alpha`. |\n| `dtype` | The type of alpha, beta, and the output: `float16`, `float32`, or `float64`. |\n| `seed` | A Python integer. Used to create a random seed for the distributions. See [`tf.random.set_seed`](../../tf/random/set_seed) for behavior. |\n| `name` | Optional name for the operation. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|-----------|-------------------------------------------------------------------------------------------------------|\n| `samples` | a `Tensor` of shape `tf.concat([shape, tf.shape(alpha + beta)], axis=0)` with values of type `dtype`. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| References ---------- ||\n|---|---|\n| Implicit Reparameterization Gradients: [Figurnov et al., 2018](http://papers.nips.cc/paper/7326-implicit-reparameterization-gradients) ([pdf](http://papers.nips.cc/paper/7326-implicit-reparameterization-gradients.pdf)) ||\n\n\u003cbr /\u003e"]]