# mindspore.nn.probability.distribution.Gamma

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

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

• concentration (int, float, list, numpy.ndarray, Tensor) - 浓度，也被称为伽马分布的alpha。默认值：None。

• rate (int, float, list, numpy.ndarray, Tensor) - 逆尺度参数，也被称为伽马分布的beta。默认值：None。

• seed (int) - 采样时使用的种子。如果为None，则使用全局种子。默认值：None。

• dtype (mindspore.dtype) - 事件样例的类型。默认值：mstype.float32。

• name (str) - 分布的名称。默认值：’Gamma’。

Note

• concentrationrate 中的元素必须大于零。

• dtype 必须是float，因为伽马分布是连续的。

• ValueError - concentration 或者 rate 中元素小于0。

• TypeError - dtype 不是float的子类。

Ascend

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

Tensor，concentration 的值。

property rate

Tensor，rate 的值。

cdf(value, concentration, rate)

• value (Tensor) - 要计算的值。

• concentration (Tensor) - 分布的alpha。默认值：None。

• rate (Tensor) - 分布的beta。默认值：None。

Tensor，累积分布函数的值。

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

• dist (str) - 分布的类型。

• concentration_b (Tensor) - 对比分布的alpha。

• rate_b (Tensor) - 对比分布的beta。

• concentration (Tensor) - 分布的alpha。默认值：None。

• rate (Tensor) - 分布的beta。默认值：None。

Tensor，交叉熵的值。

entropy(concentration, rate)

• concentration (Tensor) - 分布的alpha。默认值：None。

• rate (Tensor) - 分布的beta。默认值：None。

Tensor，熵的值。

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

• dist (str) - 分布的类型。

• concentration_b (Tensor) - 对比分布的alpha。

• rate_b (Tensor) - 对比分布的beta。

• concentration (Tensor) - 分布的alpha。默认值：None。

• rate (Tensor) - 分布的beta。默认值：None。

Tensor，KL散度。

log_cdf(value, concentration, rate)

• value (Tensor) - 要计算的值。

• concentration (Tensor) - 分布的alpha。默认值：None。

• rate (Tensor) - 分布的beta。默认值：None。

Tensor，累积分布函数的对数。

log_prob(value, concentration, rate)

• value (Tensor) - 要计算的值。

• concentration (Tensor) - 分布的alpha。默认值：None。

• rate (Tensor) - 分布的beta。默认值：None。

Tensor，累积分布函数的对数。

log_survival(value, concentration, rate)

• value (Tensor) - 要计算的值。

• concentration (Tensor) - 分布的alpha。默认值：None。

• rate (Tensor) - 分布的beta。默认值：None。

Tensor，生存函数的对数。

mean(concentration, rate)

• concentration (Tensor) - 分布的alpha。默认值：None。

• rate (Tensor) - 分布的beta。默认值：None。

Tensor，概率分布的期望。

mode(concentration, rate)

• concentration (Tensor) - 分布的alpha。默认值：None。

• rate (Tensor) - 分布的beta。默认值：None。

Tensor，概率分布的众数。

prob(value, concentration, rate)

• value (Tensor) - 要计算的值。

• concentration (Tensor) - 分布的alpha。默认值：None。

• rate (Tensor) - 分布的beta。默认值：None。

Tensor，概率值。

sample(shape, concentration, rate)

• shape (tuple) - 样本的shape。

• concentration (Tensor) - 分布的alpha。默认值：None。

• rate (Tensor) - 分布的beta。默认值：None。

Tensor，根据概率分布采样的样本。

sd(concentration, rate)

• concentration (Tensor) - 分布的alpha。默认值：None。

• rate (Tensor) - 分布的beta。默认值：None。

Tensor，概率分布的标准差。

survival_function(value, concentration, rate)

• value (Tensor) - 要计算的值。

• concentration (Tensor) - 分布的alpha。默认值：None。

• rate (Tensor) - 分布的beta。默认值：None。

Tensor，生存函数的值。

var(concentration, rate)

• concentration (Tensor) - 分布的alpha。默认值：None。

• rate (Tensor) - 分布的beta。默认值：None。

Tensor，概率分布的方差。