mindspore.nn.probability.distribution.Exponential

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

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

\[f(x, \lambda) = \lambda \exp(-\lambda x),\]

where \(\lambda\) is the rate of the distribution.

Parameters
  • rate (int, float, list, numpy.ndarray, Tensor) – The inverse scale. 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: ‘Exponential’.

Inputs and Outputs of APIs:

The accessible APIs of the Exp distribution are defined in the base class, including:

  • prob, log_prob, cdf, log_cdf, survival_function, and log_survival

  • mean, sd, 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 Exp distribution, please refer to mindspore.nn.probability.distribution.Distribution, and examples below.

Supported Platforms:

Ascend GPU

Note

rate must be strictly greater than 0. dist_spec_args is rate. dtype must be a float type because Exponential distributions are continuous.

Raises
  • ValueError – When 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 Exponential distribution of the probability 0.5.
>>> e1 = msd.Exponential(0.5, dtype=mindspore.float32)
>>> # An Exponential distribution can be initialized without arguments.
>>> # In this case, `rate` must be passed in through `args` during function calls.
>>> e2 = msd.Exponential(dtype=mindspore.float32)
>>> # Here are some tensors used below for testing
>>> value = Tensor([1, 2, 3], dtype=mindspore.float32)
>>> rate_a = Tensor([0.6], dtype=mindspore.float32)
>>> rate_b = Tensor([0.2, 0.5, 0.4], dtype=mindspore.float32)
>>> # Private interfaces of probability functions corresponding to public interfaces, including
>>> # `prob`, `log_prob`, `cdf`, `log_cdf`, `survival_function`, and `log_survival`, are the same as follows.
>>> # Args:
>>> #     value (Tensor): the value to be evaluated.
>>> #     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 = e1.prob(value)
>>> print(ans.shape)
(3,)
>>> # Evaluate with respect to distribution b.
>>> ans = e1.prob(value, rate_b)
>>> print(ans.shape)
(3,)
>>> # `rate` must be passed in during function calls.
>>> ans = e2.prob(value, rate_a)
>>> print(ans.shape)
(3,)
>>> # Functions `mean`, `sd`, 'var', and 'entropy' have the same arguments as follows.
>>> # Args:
>>> #     rate (Tensor): the rate of the distribution. Default: self.rate.
>>> # Examples of `mean`. `sd`, `var`, and `entropy` are similar.
>>> ans = e1.mean() # return 2
>>> print(ans.shape)
()
>>> ans = e1.mean(rate_b) # return 1 / rate_b
>>> print(ans.shape)
(3,)
>>> # `rate` must be passed in during function calls.
>>> ans = e2.mean(rate_a)
>>> print(ans.shape)
(1,)
>>> # Interfaces of `kl_loss` and `cross_entropy` are the same.
>>> # Args:
>>> #     dist (str): The name of the distribution. Only 'Exponential' is supported.
>>> #     rate_b (Tensor): the rate of distribution b.
>>> #     rate_a (Tensor): the rate of distribution a. Default: self.rate.
>>> # Examples of `kl_loss`. `cross_entropy` is similar.
>>> ans = e1.kl_loss('Exponential', rate_b)
>>> print(ans.shape)
(3,)
>>> ans = e1.kl_loss('Exponential', rate_b, rate_a)
>>> print(ans.shape)
(3,)
>>> # An additional `rate` must be passed in.
>>> ans = e2.kl_loss('Exponential', rate_b, rate_a)
>>> print(ans.shape)
(3,)
>>> # Examples of `sample`.
>>> # Args:
>>> #     shape (tuple): the shape of the sample. Default: ()
>>> #     probs1 (Tensor): the rate of the distribution. Default: self.rate.
>>> ans = e1.sample()
>>> print(ans.shape)
()
>>> ans = e1.sample((2,3))
>>> print(ans.shape)
(2, 3)
>>> ans = e1.sample((2,3), rate_b)
>>> print(ans.shape)
(2, 3, 3)
>>> ans = e2.sample((2,3), rate_a)
>>> print(ans.shape)
(2, 3, 1)
property rate

Return rate.

Returns

Tensor, the rate of the distribution.

cdf(value, rate)

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

Parameters

  • value (Tensor) - the value to compute.

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

Returns

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

cross_entropy(dist, rate_b, rate)

Compute the cross entropy of two distribution

Parameters

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

  • rate_b (Tensor) - the rate of the other distribution.

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

Returns

Tensor, the value of the cross entropy.

entropy(rate)

Compute the value of the entropy.

Parameters

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

Returns

Tensor, the value of the entropy.

kl_loss(dist, rate_b, 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.

  • rate_b (Tensor) - the rate of the other distribution.

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

Returns

Tensor, the value of the K-L loss.

log_cdf(value, rate)

Compute the log value of the cumulatuve distribution function.

Parameters

  • value (Tensor) - the value to compute.

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

Returns

Tensor, the log value of the cumulatuve distribution function.

log_prob(value, rate)

the log value of the probability.

Parameters

  • value (Tensor) - the value to compute.

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

Returns

Tensor, the log value of the probability.

log_survival(value, rate)

Compute the log value of the survival function.

Parameters

  • value (Tensor) - the value to compute.

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

Returns

Tensor, the value of the K-L loss.

mean(rate)

Compute the mean value of the distribution.

Parameters

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

Returns

Tensor, the mean of the distribution.

mode(rate)

Compute the mode value of the distribution.

Parameters

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

Returns

Tensor, the mode of the distribution.

prob(value, rate)

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

Parameters

  • value (Tensor) - the value to compute.

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

Returns

Tensor, the value of the probability.

sample(shape, rate)

Generate samples.

Parameters

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

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

Returns

Tensor, the sample following the distribution.

sd(rate)

The standard deviation.

Parameters

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

Returns

Tensor, the standard deviation of the distribution.

survival_function(value, rate)

Compute the value of the survival function.

Parameters

  • value (Tensor) - the value to compute.

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

Returns

Tensor, the value of the survival function.

var(rate)

Compute the variance of the distribution.

Parameters

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

Returns

Tensor, the variance of the distribution.