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’.
- Inputs and Outputs of APIs:
The accessible APIs of the Gamma distribution are defined in the base class, including:
prob, log_prob, cdf, log_cdf, survival_function, and log_survival
mean, sd, mode, var, and entropy
kl_loss and cross_entropy
sample
For more details of all APIs, including the inputs and outputs of all APIs of the Gamma distribution, please refer to
mindspore.nn.probability.distribution.Distribution
, and examples below.- 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, also know as the alpha of the Gamma distribution, after casting to dtype.
- Output:
Tensor, the concentration parameter of the distribution.
- property rate
Return the rate, also know as the beta of the Gamma distribution, after casting to dtype.
- Output:
Tensor, the rate parameter of the distribution.