mindspore.nn.probability.distribution.Beta

class mindspore.nn.probability.distribution.Beta(concentration1=None, concentration0=None, seed=None, dtype=mindspore.float32, name='Beta')[source]

Beta distribution.

Parameters
  • concentration1 (list, numpy.ndarray, Tensor) – The concentration1, also know as alpha of the Beta distribution.

  • concentration0 (list, numpy.ndarray, Tensor) – The concentration0, also know as beta of the Beta distribution.

  • 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: ‘Beta’.

Supported Platforms:

Ascend

Note

concentration1 and concentration0 must be greater than zero. dist_spec_args are concentration1 and concentration0. dtype must be a float type because Beta distributions are continuous.

Examples

>>> import mindspore
>>> import mindspore.nn as nn
>>> import mindspore.nn.probability.distribution as msd
>>> from mindspore import Tensor
>>> # To initialize a Beta distribution of the concentration1 3.0 and the concentration0 4.0.
>>> b1 = msd.Beta([3.0], [4.0], dtype=mindspore.float32)
>>> # A Beta distribution can be initialized without arguments.
>>> # In this case, `concentration1` and `concentration0` must be passed in through arguments.
>>> b2 = msd.Beta(dtype=mindspore.float32)
>>> # Here are some tensors used below for testing
>>> value = Tensor([0.1, 0.5, 0.8], dtype=mindspore.float32)
>>> concentration1_a = Tensor([2.0], dtype=mindspore.float32)
>>> concentration0_a = Tensor([2.0, 2.0, 2.0], dtype=mindspore.float32)
>>> concentration1_b = Tensor([1.0], dtype=mindspore.float32)
>>> concentration0_b = Tensor([1.0, 1.5, 2.0], dtype=mindspore.float32)
>>> # Private interfaces of probability functions corresponding to public interfaces, including
>>> # `prob` and `log_prob`, have the same arguments as follows.
>>> # Args:
>>> #     value (Tensor): the value to be evaluated.
>>> #     concentration1 (Tensor): the concentration1 of the distribution. Default: self._concentration1.
>>> #     concentration0 (Tensor): the concentration0 of the distribution. Default: self._concentration0.
>>> # Examples of `prob`.
>>> # Similar calls can be made to other probability functions
>>> # by replacing 'prob' by the name of the function
>>> ans = b1.prob(value)
>>> print(ans.shape)
(3,)
>>> # Evaluate with respect to the distribution b.
>>> ans = b1.prob(value, concentration1_b, concentration0_b)
>>> print(ans.shape)
(3,)
>>> # `concentration1` and `concentration0` must be passed in during function calls
>>> ans = b2.prob(value, concentration1_a, concentration0_a)
>>> print(ans.shape)
(3,)
>>> # Functions `mean`, `sd`, `mode`, `var`, and `entropy` have the same arguments.
>>> # Args:
>>> #     concentration1 (Tensor): the concentration1 of the distribution. Default: self._concentration1.
>>> #     concentration0 (Tensor): the concentration0 of the distribution. Default: self._concentration0.
>>> # Example of `mean`, `sd`, `mode`, `var`, and `entropy` are similar.
>>> ans = b1.mean()
>>> print(ans.shape)
(1,)
>>> ans = b1.mean(concentration1_b, concentration0_b)
>>> print(ans.shape)
(3,)
>>> # `concentration1` and `concentration0` must be passed in during function calls.
>>> ans = b2.mean(concentration1_a, concentration0_a)
>>> print(ans.shape)
(3,)
>>> # Interfaces of 'kl_loss' and 'cross_entropy' are the same:
>>> # Args:
>>> #     dist (str): the type of the distributions. Only "Beta" is supported.
>>> #     concentration1_b (Tensor): the concentration1 of distribution b.
>>> #     concentration0_b (Tensor): the concentration0 of distribution b.
>>> #     concentration1_a (Tensor): the concentration1 of distribution a.
>>> #       Default: self._concentration1.
>>> #     concentration0_a (Tensor): the concentration0 of distribution a.
>>> #       Default: self._concentration0.
>>> # Examples of `kl_loss`. `cross_entropy` is similar.
>>> ans = b1.kl_loss('Beta', concentration1_b, concentration0_b)
>>> print(ans.shape)
(3,)
>>> ans = b1.kl_loss('Beta', concentration1_b, concentration0_b, concentration1_a, concentration0_a)
>>> print(ans.shape)
(3,)
>>> # Additional `concentration1` and `concentration0` must be passed in.
>>> ans = b2.kl_loss('Beta', concentration1_b, concentration0_b, concentration1_a, concentration0_a)
>>> print(ans.shape)
(3,)
>>> # Examples of `sample`.
>>> # Args:
>>> #     shape (tuple): the shape of the sample. Default: ()
>>> #     concentration1 (Tensor): the concentration1 of the distribution. Default: self._concentration1.
>>> #     concentration0 (Tensor): the concentration0 of the distribution. Default: self._concentration0.
>>> ans = b1.sample()
>>> print(ans.shape)
(1,)
>>> ans = b1.sample((2,3))
>>> print(ans.shape)
(2, 3, 1)
>>> ans = b1.sample((2,3), concentration1_b, concentration0_b)
>>> print(ans.shape)
(2, 3, 3)
>>> ans = b2.sample((2,3), concentration1_a, concentration0_a)
>>> print(ans.shape)
(2, 3, 3)
property concentration0

Return the concentration0, also know as the beta of the Beta distribution, after casting to dtype.

property concentration1

Return the concentration1, also know as the alpha of the Beta distribution, after casting to dtype.