mindspore.nn.probability.distribution.Laplace
- class mindspore.nn.probability.distribution.Laplace(mean=None, sd=None, seed=None, dtype=mstype.float32, name='Laplace')[source]
Laplace distribution. A Laplace distribution is a continuous distribution with the range \((-\inf, \inf)\) and the probability density function:
\[f(x, \mu, b) = 1 / (2 * b) * \exp(-abs(x - \mu) / b).\]where \(\mu, b\) are the mean and the scale of the laplace distribution respectively.
- Parameters
mean (Union[int, float, list, numpy.ndarray, Tensor], optional) – The mean of the distribution. If this arg is None, then the mean of the distribution will be passed in runtime. Default: None.
sd (Union[int, float, list, numpy.ndarray, Tensor], optional) – The scale of the distribution. If this arg is None, then the scale of the distribution will be passed in runtime. Default: None.
seed (int, optional) – The seed used in sampling. The global seed is used if it is None. Default: None.
dtype (mindspore.dtype, optional) – The type of the event samples. Default: mstype.float32.
name (str, optional) – The name of the distribution. Default: ‘Laplace’.
Note
sd must be greater than zero.
dtype must be a float type because Laplace distributions are continuous.
If the arg mean or sd is passed in runtime, then it will be used as the parameter value. Otherwise, the value passed in the constructor will be used.
- Raises
ValueError – When sd <= 0.
TypeError – When the input dtype is not a subclass of float.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore >>> import mindspore.nn as nn >>> from mindspore.nn.probability.distribution import Laplace >>> from mindspore import Tensor >>> # To initialize a Laplace distribution of the mean 3.0 and the scale 4.0. >>> n1 = Laplace(3.0, 4.0, dtype=mindspore.float32) >>> # A Laplace distribution can be initialized without arguments. >>> # In this case, `mean` and `sd` must be passed in through arguments. >>> n2 = Laplace(dtype=mindspore.float32) >>> # Here are some tensors used below for testing >>> value = Tensor([1.0, 2.0, 3.0], dtype=mindspore.float32) >>> mean_a = Tensor([2.0], dtype=mindspore.float32) >>> sd_a = Tensor([2.0, 2.0, 2.0], dtype=mindspore.float32) >>> mean_b = Tensor([1.0], dtype=mindspore.float32) >>> sd_b = Tensor([1.0, 1.5, 2.0], dtype=mindspore.float32) >>> ans = n1.log_prob(value) >>> print(ans.shape) (3,) >>> # Evaluate with respect to the distribution b. >>> ans = n1.log_prob(value, mean_b, sd_b) >>> print(ans.shape) (3,) >>> # `mean` and `sd` must be passed in during function calls >>> ans = n2.log_prob(value, mean_a, sd_a) >>> print(ans.shape) (3,)
- log_prob(value, mean=None, sd=None)
Evaluate log probability of the value of the Laplace distribution.
- Parameters
value (Tensor) - the value to compute.
mean (Tensor, optional) - the mean of the distribution. Default: None.
sd (Tensor, optional) - the standard deviation of the distribution. Default: None.
- Returns
Tensor, the log value of the probability.