mindspore.nn.probability.distribution.Cauchy

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

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

\[f(x, a, b) = 1 / \pi b(1 - ((x - a)/b)^2),\]

where a and b are loc and scale parameter respectively.

Parameters
  • loc (int, float, list, numpy.ndarray, Tensor) – The location of the Cauchy distribution. Default: None.

  • scale (int, float, list, numpy.ndarray, Tensor) – The scale of the Cauchy 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: ‘Cauchy’.

Inputs and Outputs of APIs:

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

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

  • mode and entropy

  • kl_loss and cross_entropy

  • sample

For more details of all APIs, including the inputs and outputs of the APIs of the Cauchy distribution, please refer to mindspore.nn.probability.distribution.Distribution, and examples below.

Supported Platforms:

Ascend

Note

scale must be greater than zero. dist_spec_args are loc and scale. dtype must be a float type because Cauchy distributions are continuous. Cauchy distribution is not supported on GPU backend.

Raises
  • ValueError – When scale <= 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 Cauchy distribution of loc 3.0 and scale 4.0.
>>> cauchy1 = msd.Cauchy(3.0, 4.0, dtype=mindspore.float32)
>>> # A Cauchy distribution can be initialized without arguments.
>>> # In this case, 'loc' and `scale` must be passed in through arguments.
>>> cauchy2 = msd.Cauchy(dtype=mindspore.float32)
>>> # Here are some tensors used below for testing
>>> value = Tensor([1.0, 2.0, 3.0], dtype=mindspore.float32)
>>> loc_a = Tensor([2.0], dtype=mindspore.float32)
>>> scale_a = Tensor([2.0, 2.0, 2.0], dtype=mindspore.float32)
>>> loc_b = Tensor([1.0], dtype=mindspore.float32)
>>> scale_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.
>>> #     loc (Tensor): the location of the distribution. Default: self.loc.
>>> #     scale (Tensor): the scale of the distribution. Default: self.scale.
>>> # Examples of `prob`.
>>> # Similar calls can be made to other probability functions
>>> # by replacing 'prob' by the name of the function
>>> ans = cauchy1.prob(value)
>>> print(ans.shape)
(3,)
>>> # Evaluate with respect to distribution b.
>>> ans = cauchy1.prob(value, loc_b, scale_b)
>>> print(ans.shape)
(3,)
>>> # `loc` and `scale` must be passed in during function calls
>>> ans = cauchy2.prob(value, loc_a, scale_a)
>>> print(ans.shape)
(3,)
>>> # Functions `mode` and `entropy` have the same arguments.
>>> # Args:
>>> #     loc (Tensor): the location of the distribution. Default: self.loc.
>>> #     scale (Tensor): the scale of the distribution. Default: self.scale.
>>> # Example of `mode`.
>>> ans = cauchy1.mode() # return 3.0
>>> print(ans.shape)
()
>>> ans = cauchy1.mode(loc_b, scale_b) # return loc_b
>>> print(ans.shape)
(3,)
>>> # `loc` and `scale` must be passed in during function calls.
>>> ans = cauchy2.mode(loc_a, scale_a)
>>> print(ans.shape)
(3,)
>>> # Interfaces of 'kl_loss' and 'cross_entropy' are the same:
>>> # Args:
>>> #     dist (str): the type of the distributions. Only "Cauchy" is supported.
>>> #     loc_b (Tensor): the loc of distribution b.
>>> #     scale_b (Tensor): the scale distribution b.
>>> #     loc (Tensor): the loc of distribution a. Default: self.loc.
>>> #     scale (Tensor): the scale distribution a. Default: self.scale.
>>> # Examples of `kl_loss`. `cross_entropy` is similar.
>>> ans = cauchy1.kl_loss('Cauchy', loc_b, scale_b)
>>> print(ans.shape)
(3,)
>>> ans = cauchy1.kl_loss('Cauchy', loc_b, scale_b, loc_a, scale_a)
>>> print(ans.shape)
(3,)
>>> # Additional `loc` and `scale` must be passed in.
>>> ans = cauchy2.kl_loss('Cauchy', loc_b, scale_b, loc_a, scale_a)
>>> print(ans.shape)
(3,)
>>> # Examples of `sample`.
>>> # Args:
>>> #     shape (tuple): the shape of the sample. Default: ()
>>> #     loc (Tensor): the location of the distribution. Default: self.loc.
>>> #     scale (Tensor): the scale of the distribution. Default: self.scale.
>>> ans = cauchy1.sample()
>>> print(ans.shape)
()
>>> ans = cauchy1.sample((2,3))
>>> print(ans.shape)
(2, 3)
>>> ans = cauchy1.sample((2,3), loc_b, scale_b)
>>> print(ans.shape)
(2, 3, 3)
>>> ans = cauchy2.sample((2,3), loc_a, scale_a)
>>> print(ans.shape)
(2, 3, 3)
property loc

Return the loc parameter of the distribution.

Returns

Tensor, the loc parameter of the distribution.

property scale

Return the scale parameter of the distribution.

Returns

Tensor, the scale parameter of the distribution.

cdf(value, loc, scale)

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

Parameters

  • value (Tensor) - the value to compute.

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

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

Returns

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

cross_entropy(dist, loc_b, scale_b, loc, scale)

Compute the cross entropy of two distribution

Parameters

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

  • loc_b (Tensor) - the loc parameter of the other distribution.

  • scale_b (Tensor) - the scale parameter of the other distribution.

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

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

Returns

Tensor, the value of the cross entropy.

entropy(loc, scale)

Compute the value of the entropy.

Parameters

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

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

Returns

Tensor, the value of the entropy.

kl_loss(dist, loc_b, scale_b, loc, scale)

Compute the value of the K-L loss between two distribution, namely KL(a||b).

Parameters

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

  • loc_b (Tensor) - the loc parameter of the other distribution.

  • scale_b (Tensor) - the scale parameter of the other distribution.

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

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

Returns

Tensor, the value of the K-L loss.

log_cdf(value, loc, scale)

Compute the log value of the cumulatuve distribution function.

Parameters

  • value (Tensor) - the value to compute.

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

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

Returns

Tensor, the log value of the cumulatuve distribution function.

log_prob(value, loc, scale)

the log value of the probability.

Parameters

  • value (Tensor) - the value to compute.

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

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

Returns

Tensor, the log value of the probability.

log_survival(value, loc, scale)

Compute the log value of the survival function.

Parameters

  • value (Tensor) - the value to compute.

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

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

Returns

Tensor, the value of the K-L loss.

mean(loc, scale)

Compute the mean value of the distribution.

Parameters

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

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

Returns

Tensor, the mean of the distribution.

mode(loc, scale)

Compute the mode value of the distribution.

Parameters

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

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

Returns

Tensor, the mode of the distribution.

prob(value, loc, scale)

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

Parameters

  • value (Tensor) - the value to compute.

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

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

Returns

Tensor, the value of the probability.

sample(shape, loc, scale)

Generate samples.

Parameters

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

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

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

Returns

Tensor, the sample following the distribution.

sd(loc, scale)

The standard deviation.

Parameters

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

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

Returns

Tensor, the standard deviation of the distribution.

survival_function(value, loc, scale)

Compute the value of the survival function.

Parameters

  • value (Tensor) - the value to compute.

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

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

Returns

Tensor, the value of the survival function.

var(loc, scale)

Compute the variance of the distribution.

Parameters

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

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

Returns

Tensor, the variance of the distribution.