mindspore.nn.probability.distribution.Gamma

class mindspore.nn.probability.distribution.Gamma(concentration=None, rate=None, seed=None, dtype=mstype.float32, name='Gamma')[source]

Gamma distribution. A Gamma distributio is a continuous distribution with the range \([0, 1]\) and the probability density function:

\[f(x, \alpha, \beta) = \beta^\alpha / \Gamma(\alpha) x^{\alpha - 1} \exp(-\beta x).\]

where \(G\) is the Gamma function, and \(\alpha, \beta\) are the concentration and the rate of the distribution respectively.

Parameters
  • concentration (int, float, list, numpy.ndarray, Tensor) – The concentration, also know as alpha of the Gamma distribution. Default: None.

  • rate (int, float, list, numpy.ndarray, Tensor) – The rate, also know as beta of the Gamma distribution. Default: None.

  • seed (int) – The seed used in sampling. The global seed is used if it is None. Default: None.

  • dtype (mindspore.dtype) – The type of the event samples. Default: mstype.float32.

  • name (str) – The name of the distribution. Default: ‘Gamma’.

Supported Platforms:

Ascend

Note

concentration and rate must be greater than zero. dist_spec_args are concentration and rate. dtype must be a float type because Gamma distributions are continuous.

Raises
  • ValueError – When concentration <= 0 or rate <= 0.

  • TypeError – When the input dtype is not a subclass of float.

Examples

>>> import mindspore
>>> import mindspore.nn as nn
>>> import mindspore.nn.probability.distribution as msd
>>> from mindspore import Tensor
>>> # To initialize a Gamma distribution of the concentration 3.0 and the rate 4.0.
>>> g1 = msd.Gamma([3.0], [4.0], dtype=mindspore.float32)
>>> # A Gamma distribution can be initialized without arguments.
>>> # In this case, `concentration` and `rate` must be passed in through arguments.
>>> g2 = msd.Gamma(dtype=mindspore.float32)
>>> # Here are some tensors used below for testing
>>> value = Tensor([1.0, 2.0, 3.0], dtype=mindspore.float32)
>>> concentration_a = Tensor([2.0], dtype=mindspore.float32)
>>> rate_a = Tensor([2.0, 2.0, 2.0], dtype=mindspore.float32)
>>> concentration_b = Tensor([1.0], dtype=mindspore.float32)
>>> rate_b = Tensor([1.0, 1.5, 2.0], dtype=mindspore.float32)
>>>
>>> # Private interfaces of probability functions corresponding to public interfaces, including
>>> # `prob`, `log_prob`, `cdf`, `log_cdf`, `survival_function`, and `log_survival`,
>>> # have the same arguments as follows.
>>> # Args:
>>> #     value (Tensor): the value to be evaluated.
>>> #     concentration (Tensor): the concentration of the distribution. Default: self._concentration.
>>> #     rate (Tensor): the rate of the distribution. Default: self._rate.
>>> # Examples of `prob`.
>>> # Similar calls can be made to other probability functions
>>> # by replacing 'prob' by the name of the function
>>> ans = g1.prob(value)
>>> print(ans.shape)
(3,)
>>> # Evaluate with respect to the distribution b.
>>> ans = g1.prob(value, concentration_b, rate_b)
>>> print(ans.shape)
(3,)
>>> # `concentration` and `rate` must be passed in during function calls for g2.
>>> ans = g2.prob(value, concentration_a, rate_a)
>>> print(ans.shape)
(3,)
>>> # Functions `mean`, `sd`, `mode`, `var`, and `entropy` have the same arguments.
>>> # Args:
>>> #     concentration (Tensor): the concentration of the distribution. Default: self._concentration.
>>> #     rate (Tensor): the rate of the distribution. Default: self._rate.
>>> # Example of `mean`, `sd`, `mode`, `var`, and `entropy` are similar.
>>> ans = g1.mean()
>>> print(ans.shape)
(1,)
>>> ans = g1.mean(concentration_b, rate_b)
>>> print(ans.shape)
(3,)
>>> # `concentration` and `rate` must be passed in during function calls.
>>> ans = g2.mean(concentration_a, rate_a)
>>> print(ans.shape)
(3,)
>>> # Interfaces of 'kl_loss' and 'cross_entropy' are the same:
>>> # Args:
>>> #     dist (str): the type of the distributions. Only "Gamma" is supported.
>>> #     concentration_b (Tensor): the concentration of distribution b.
>>> #     rate_b (Tensor): the rate of distribution b.
>>> #     concentration_a (Tensor): the concentration of distribution a. Default: self._concentration.
>>> #     rate_a (Tensor): the rate of distribution a. Default: self._rate.
>>> # Examples of `kl_loss`. `cross_entropy` is similar.
>>> ans = g1.kl_loss('Gamma', concentration_b, rate_b)
>>> print(ans.shape)
(3,)
>>> ans = g1.kl_loss('Gamma', concentration_b, rate_b, concentration_a, rate_a)
>>> print(ans.shape)
(3,)
>>> # Additional `concentration` and `rate` must be passed in.
>>> ans = g2.kl_loss('Gamma', concentration_b, rate_b, concentration_a, rate_a)
>>> print(ans.shape)
(3,)
>>> # Examples of `sample`.
>>> # Args:
>>> #     shape (tuple): the shape of the sample. Default: ()
>>> #     concentration (Tensor): the concentration of the distribution. Default: self._concentration.
>>> #     rate (Tensor): the rate of the distribution. Default: self._rate.
>>> ans = g1.sample()
>>> print(ans.shape)
(1,)
>>> ans = g1.sample((2,3))
>>> print(ans.shape)
(2, 3, 1)
>>> ans = g1.sample((2,3), concentration_b, rate_b)
>>> print(ans.shape)
(2, 3, 3)
>>> ans = g2.sample((2,3), concentration_a, rate_a)
>>> print(ans.shape)
(2, 3, 3)
property concentration

Return the concentration, aka the alpha parameter, of the distribution.

Returns

Tensor, concentration.

property rate

Return the rate, aka the beta parameter, of the distribution.

Returns

Tensor, rate.

cdf(value, concentration, rate)

Compute the cumulatuve distribution function(CDF) of the given value.

Parameters

  • value (Tensor) - the value to compute.

  • concentration (Tensor) - the alpha parameter of the distribution. Default value: None.

  • rate (Tensor) - the beta parameter of the distribution. Default value: None.

Returns

Tensor, the value of the cumulatuve distribution function for the given input.

cross_entropy(dist, concentration_b, rate_b, concentration, rate)

Compute the cross entropy of two distribution

Parameters

  • dist (str) - the type of the other distribution.

  • concentration_b (Tensor) - the alpha parameter of the other distribution.

  • rate_b (Tensor) - the beta parameter of the other distribution.

  • concentration (Tensor) - the alpha parameter of the distribution. Default value: None.

  • rate (Tensor) - the beta parameter of the distribution. Default value: None.

Returns

Tensor, the value of the cross entropy.

entropy(concentration, rate)

Compute the value of the entropy.

Parameters

  • concentration (Tensor) - the alpha parameter of the distribution. Default value: None.

  • rate (Tensor) - the beta parameter of the distribution. Default value: None.

Returns

Tensor, the value of the entropy.

kl_loss(dist, concentration_b, rate_b, concentration, rate)

Compute the value of the K-L loss between two distribution, namely KL(a||b).

Parameters

  • dist (str) - the type of the other distribution.

  • concentration_b (Tensor) - the alpha parameter of the other distribution.

  • rate_b (Tensor) - the beta parameter of the other distribution.

  • concentration (Tensor) - the alpha parameter of the distribution. Default value: None.

  • rate (Tensor) - the beta parameter of the distribution. Default value: None.

Returns

Tensor, the value of the K-L loss.

log_cdf(value, concentration, rate)

Compute the log value of the cumulatuve distribution function.

Parameters

  • value (Tensor) - the value to compute.

  • concentration (Tensor) - the alpha parameter of the distribution. Default value: None.

  • rate (Tensor) - the beta parameter of the distribution. Default value: None.

Returns

Tensor, the log value of the cumulatuve distribution function.

log_prob(value, concentration, rate)

the log value of the probability.

Parameters

  • value (Tensor) - the value to compute.

  • concentration (Tensor) - the alpha parameter of the distribution. Default value: None.

  • rate (Tensor) - the beta parameter of the distribution. Default value: None.

Returns

Tensor, the log value of the probability.

log_survival(value, concentration, rate)

Compute the log value of the survival function.

Parameters

  • value (Tensor) - the value to compute.

  • concentration (Tensor) - the alpha parameter of the distribution. Default value: None.

  • rate (Tensor) - the beta parameter of the distribution. Default value: None.

Returns

Tensor, the value of the K-L loss.

mean(concentration, rate)

Compute the mean value of the distribution.

Parameters

  • concentration (Tensor) - the alpha parameter of the distribution. Default value: None.

  • rate (Tensor) - the beta parameter of the distribution. Default value: None.

Returns

Tensor, the mean of the distribution.

mode(concentration, rate)

Compute the mode value of the distribution.

Parameters

  • concentration (Tensor) - the alpha parameter of the distribution. Default value: None.

  • rate (Tensor) - the beta parameter of the distribution. Default value: None.

Returns

Tensor, the mode of the distribution.

prob(value, concentration, rate)

The probability of the given value. For the continuous distribution, it is the probability density function.

Parameters

  • value (Tensor) - the value to compute.

  • concentration (Tensor) - the alpha parameter of the distribution. Default value: None.

  • rate (Tensor) - the beta parameter of the distribution. Default value: None.

Returns

Tensor, the value of the probability.

sample(shape, concentration, rate)

Generate samples.

Parameters

  • shape (tuple) - the shape of the sample.

  • concentration (Tensor) - the alpha parameter of the distribution. Default value: None.

  • rate (Tensor) - the beta parameter of the distribution. Default value: None.

Returns

Tensor, the sample following the distribution.

sd(concentration, rate)

The standard deviation.

Parameters

  • concentration (Tensor) - the alpha parameter of the distribution. Default value: None.

  • rate (Tensor) - the beta parameter of the distribution. Default value: None.

Returns

Tensor, the standard deviation of the distribution.

survival_function(value, concentration, rate)

Compute the value of the survival function.

Parameters

  • value (Tensor) - the value to compute.

  • concentration (Tensor) - the alpha parameter of the distribution. Default value: None.

  • rate (Tensor) - the beta parameter of the distribution. Default value: None.

Returns

Tensor, the value of the survival function.

var(concentration, rate)

Compute the variance of the distribution.

Parameters

  • concentration (Tensor) - the alpha parameter of the distribution. Default value: None.

  • rate (Tensor) - the beta parameter of the distribution. Default value: None.

Returns

Tensor, the variance of the distribution.