Document feedback

Question document fragment

When a question document fragment contains a formula, it is displayed as a space.

Submission type
issue

It's a little complicated...

I'd like to ask someone.

PR

Just a small problem.

I can fix it online!

Please select the submission type

Problem type
Specifications and Common Mistakes

- Specifications and Common Mistakes:

- Misspellings or punctuation mistakes,incorrect formulas, abnormal display.

- Incorrect links, empty cells, or wrong formats.

- Chinese characters in English context.

- Minor inconsistencies between the UI and descriptions.

- Low writing fluency that does not affect understanding.

- Incorrect version numbers, including software package names and version numbers on the UI.

Usability

- Usability:

- Incorrect or missing key steps.

- Missing main function descriptions, keyword explanation, necessary prerequisites, or precautions.

- Ambiguous descriptions, unclear reference, or contradictory context.

- Unclear logic, such as missing classifications, items, and steps.

Correctness

- Correctness:

- Technical principles, function descriptions, supported platforms, parameter types, or exceptions inconsistent with that of software implementation.

- Incorrect schematic or architecture diagrams.

- Incorrect commands or command parameters.

- Incorrect code.

- Commands inconsistent with the functions.

- Wrong screenshots.

- Sample code running error, or running results inconsistent with the expectation.

Risk Warnings

- Risk Warnings:

- Lack of risk warnings for operations that may damage the system or important data.

Content Compliance

- Content Compliance:

- Contents that may violate applicable laws and regulations or geo-cultural context-sensitive words and expressions.

- Copyright infringement.

Please select the type of question

Problem description

Describe the bug so that we can quickly locate the problem.

mindspore.nn.probability.distribution.Gamma

View Source On Gitee
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,inf) and the probability density function:

f(x,α,β)=βα/Γ(α)xα1exp(βx).

where G is the Gamma function, and α and β are the concentration and the rate of the distribution respectively.

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

  • rate (int, float, list, numpy.ndarray, Tensor) – The rate, also know as β 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' .

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.

Supported Platforms:

Ascend

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 α parameter, of the distribution.

Returns

Tensor, concentration.

property rate

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

Returns

Tensor, rate.

cdf(value, concentration, rate)[source]

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

Parameters
  • value (Tensor) - the value to compute.

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

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

Returns

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

cross_entropy(dist, concentration_b, rate_b, concentration, rate)[source]

Compute the cross entropy of two distribution.

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

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

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

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

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

Returns

Tensor, the value of the cross entropy.

entropy(concentration, rate)[source]

Compute the value of the entropy.

Parameters
  • concentration (Tensor) - the α parameter of the distribution. Default: None .

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

Returns

Tensor, the value of the entropy.

kl_loss(dist, concentration_b, rate_b, concentration, rate)[source]

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 α parameter of the other distribution.

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

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

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

Returns

Tensor, the value of the K-L loss.

log_cdf(value, concentration, rate)[source]

Compute the log value of the cumulatuve distribution function.

Parameters
  • value (Tensor) - the value to compute.

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

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

Returns

Tensor, the log value of the cumulatuve distribution function.

log_prob(value, concentration, rate)[source]

the log value of the probability.

Parameters
  • value (Tensor) - the value to compute.

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

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

Returns

Tensor, the log value of the probability.

log_survival(value, concentration, rate)[source]

Compute the log value of the survival function.

Parameters
  • value (Tensor) - the value to compute.

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

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

Returns

Tensor, the value of the K-L loss.

mean(concentration, rate)[source]

Compute the mean value of the distribution.

Parameters
  • concentration (Tensor) - the α parameter of the distribution. Default: None .

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

Returns

Tensor, the mean of the distribution.

mode(concentration, rate)[source]

Compute the mode value of the distribution.

Parameters
  • concentration (Tensor) - the α parameter of the distribution. Default: None .

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

Returns

Tensor, the mode of the distribution.

prob(value, concentration, rate)[source]

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 α parameter of the distribution. Default: None .

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

Returns

Tensor, the value of the probability.

sample(shape, concentration, rate)[source]

Generate samples.

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

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

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

Returns

Tensor, the sample following the distribution.

sd(concentration, rate)[source]

The standard deviation.

Parameters
  • concentration (Tensor) - the α parameter of the distribution. Default: None .

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

Returns

Tensor, the standard deviation of the distribution.

survival_function(value, concentration, rate)[source]

Compute the value of the survival function.

Parameters
  • value (Tensor) - the value to compute.

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

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

Returns

Tensor, the value of the survival function.

var(concentration, rate)[source]

Compute the variance of the distribution.

Parameters
  • concentration (Tensor) - the α parameter of the distribution. Default: None .

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

Returns

Tensor, the variance of the distribution.