mindspore.nn.probability.distribution.TransformedDistribution

View Source On Gitee
class mindspore.nn.probability.distribution.TransformedDistribution(bijector, distribution, seed=None, name='transformed_distribution')[source]

Transformed Distribution. This class contains a bijector and a distribution and transforms the original distribution to a new distribution through the operation defined by the bijector. If \(X\) is an random variable following the underying distribution, and \(g(x)\) is a function represented by the bijector, then \(Y = g(X)\) is a random variable following the transformed distribution.

Parameters
  • bijector (Bijector) – The transformation to perform.

  • distribution (Distribution) – The original distribution. Must be a float dtype.

  • seed (int) – The seed is used in sampling. The global seed is used if it is None. Default: None . If this seed is given when a TransformedDistribution object is initialized, the object's sampling function will use this seed; elsewise, the underlying distribution's seed will be used.

  • name (str) – The name of the transformed distribution. Default: 'transformed_distribution' .

Note

The arguments used to initialize the original distribution cannot be None. For example, mynormal = msd.Normal(dtype=mindspore.float32) cannot be used to initialized a TransformedDistribution since mean and sd are not specified. batch_shape is the batch_shape of the original distribution. broadcast_shape is the broadcast shape between the original distribution and bijector. is_scalar_batch is only true if both the original distribution and the bijector are scalar batches. default_parameters, parameter_names and parameter_type are set to be consistent with the original distribution. Derived class can overwrite default_parameters and parameter_names by calling reset_parameters followed by add_parameter.

Raises
  • TypeError – When the input bijector is not a Bijector instance.

  • TypeError – When the input distribution is not a Distribution instance.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import numpy as np
>>> import mindspore
>>> import mindspore.nn as nn
>>> import mindspore.nn.probability.distribution as msd
>>> import mindspore.nn.probability.bijector as msb
>>> from mindspore import Tensor
>>> class Net(nn.Cell):
...     def __init__(self, shape, dtype=mindspore.float32, seed=0, name='transformed_distribution'):
...         super(Net, self).__init__()
...         # create TransformedDistribution distribution
...         self.exp = msb.Exp()
...         self.normal = msd.Normal(0.0, 1.0, dtype=dtype)
...         self.lognormal = msd.TransformedDistribution(self.exp, self.normal, seed=seed, name=name)
...         self.shape = shape
...
...     def construct(self, value):
...         cdf = self.lognormal.cdf(value)
...         sample = self.lognormal.sample(self.shape)
...         return cdf, sample
>>> shape = (2, 3)
>>> net = Net(shape=shape, name="LogNormal")
>>> x = np.array([2.0, 3.0, 4.0, 5.0]).astype(np.float32)
>>> tx = Tensor(x, dtype=mindspore.float32)
>>> cdf, sample = net(tx)
>>> print(sample.shape)
(2, 3)
property bijector

Return the bijector.

Returns

Bijector, the bijector.

property distribution

Return the distribution before transformation.

Returns

Distribution, the distribution before transformation.

property dtype

Return the data type of distribution.

Returns

mindspore.dtype, the data type of distribution.

property is_linear_transformation

Return whether the bijector is linear.

Returns

Bool, return True if the bijector is linear, otherwise return False.

cdf(value)

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

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

Returns

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

log_cdf(value)

Compute the log value of the cumulatuve distribution function.

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

Returns

Tensor, the log value of the cumulatuve distribution function.

log_prob(value)

the log value of the probability.

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

Returns

Tensor, the log value of the probability.

log_survival(value)

Compute the log value of the survival function.

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

Returns

Tensor, the value of the K-L loss.

mean()

Compute the mean value of the distribution.

Returns

Tensor, the mean of the distribution.

prob(value)

The probability of the given value.

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

Returns

Tensor, the value of the probability.

sample(shape)

Generate samples.

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

Returns

Tensor, the sample following the distribution.

survival_function(value)

Compute the value of the survival function.

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

Returns

Tensor, the value of the survival function.