Document feedback

Question document fragment

When a question document fragment contains a formula, it is displayed as a space.

Submission type
issue

It's a little complicated...

I'd like to ask someone.

Please select the submission type

Problem type
Specifications and Common Mistakes

- Specifications and Common Mistakes:

- Misspellings or punctuation mistakes,incorrect formulas, abnormal display.

- Incorrect links, empty cells, or wrong formats.

- Chinese characters in English context.

- Minor inconsistencies between the UI and descriptions.

- Low writing fluency that does not affect understanding.

- Incorrect version numbers, including software package names and version numbers on the UI.

Usability

- Usability:

- Incorrect or missing key steps.

- Missing main function descriptions, keyword explanation, necessary prerequisites, or precautions.

- Ambiguous descriptions, unclear reference, or contradictory context.

- Unclear logic, such as missing classifications, items, and steps.

Correctness

- Correctness:

- Technical principles, function descriptions, supported platforms, parameter types, or exceptions inconsistent with that of software implementation.

- Incorrect schematic or architecture diagrams.

- Incorrect commands or command parameters.

- Incorrect code.

- Commands inconsistent with the functions.

- Wrong screenshots.

- Sample code running error, or running results inconsistent with the expectation.

Risk Warnings

- Risk Warnings:

- Lack of risk warnings for operations that may damage the system or important data.

Content Compliance

- Content Compliance:

- Contents that may violate applicable laws and regulations or geo-cultural context-sensitive words and expressions.

- Copyright infringement.

Please select the type of question

Problem description

Describe the bug so that we can quickly locate the problem.

mindspore.nn.probability.distribution.Uniform

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

Uniform Distribution. A Uniform distributio is a continuous distribution with the range [a,b] and the probability density function:

f(x,a,b)=1/(ba),

where a and b are the lower and upper bound respectively.

Parameters
  • low (int, float, list, numpy.ndarray, Tensor) – The lower bound of the distribution. Default: None.

  • high (int, float, list, numpy.ndarray, Tensor) – The upper bound of the distribution. Default: None.

  • seed (int) – The seed uses 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: ‘Uniform’.

Note

low must be strictly less than high. dist_spec_args are high and low. dtype must be float type because Uniform distributions are continuous.

Raises
  • ValueError – When high <= low.

  • TypeError – When the input dtype is not a subclass of float.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> import mindspore.nn as nn
>>> import mindspore.nn.probability.distribution as msd
>>> from mindspore import Tensor
>>> # To initialize a Uniform distribution of the lower bound 0.0 and the higher bound 1.0.
>>> u1 = msd.Uniform(0.0, 1.0, dtype=mindspore.float32)
>>> # A Uniform distribution can be initialized without arguments.
>>> # In this case, `high` and `low` must be passed in through arguments during function calls.
>>> u2 = msd.Uniform(dtype=mindspore.float32)
>>>
>>> # Here are some tensors used below for testing
>>> value = Tensor([0.5, 0.8], dtype=mindspore.float32)
>>> low_a = Tensor([0., 0.], dtype=mindspore.float32)
>>> high_a = Tensor([2.0, 4.0], dtype=mindspore.float32)
>>> low_b = Tensor([-1.5], dtype=mindspore.float32)
>>> high_b = Tensor([2.5, 5.], 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.
>>> # Args:
>>> #     value (Tensor): the value to be evaluated.
>>> #     low (Tensor): the lower bound of the distribution. Default: self.low.
>>> #     high (Tensor): the higher bound of the distribution. Default: self.high.
>>> # Examples of `prob`.
>>> # Similar calls can be made to other probability functions
>>> # by replacing 'prob' by the name of the function.
>>> ans = u1.prob(value)
>>> print(ans.shape)
(2,)
>>> # Evaluate with respect to distribution b.
>>> ans = u1.prob(value, low_b, high_b)
>>> print(ans.shape)
(2,)
>>> # `high` and `low` must be passed in during function calls.
>>> ans = u2.prob(value, low_a, high_a)
>>> print(ans.shape)
(2,)
>>> # Functions `mean`, `sd`, `var`, and `entropy` have the same arguments.
>>> # Args:
>>> #     low (Tensor): the lower bound of the distribution. Default: self.low.
>>> #     high (Tensor): the higher bound of the distribution. Default: self.high.
>>> # Examples of `mean`. `sd`, `var`, and `entropy` are similar.
>>> ans = u1.mean() # return 0.5
>>> print(ans.shape)
()
>>> ans = u1.mean(low_b, high_b) # return (low_b + high_b) / 2
>>> print(ans.shape)
(2,)
>>> # `high` and `low` must be passed in during function calls.
>>> ans = u2.mean(low_a, high_a)
>>> print(ans.shape)
(2,)
>>> # Interfaces of 'kl_loss' and 'cross_entropy' are the same.
>>> # Args:
>>> #     dist (str): the type of the distributions. Should be "Uniform" in this case.
>>> #     low_b (Tensor): the lower bound of distribution b.
>>> #     high_b (Tensor): the upper bound of distribution b.
>>> #     low_a (Tensor): the lower bound of distribution a. Default: self.low.
>>> #     high_a (Tensor): the upper bound of distribution a. Default: self.high.
>>> # Examples of `kl_loss`. `cross_entropy` is similar.
>>> ans = u1.kl_loss('Uniform', low_b, high_b)
>>> print(ans.shape)
(2,)
>>> ans = u1.kl_loss('Uniform', low_b, high_b, low_a, high_a)
>>> print(ans.shape)
(2,)
>>> # Additional `high` and `low` must be passed in.
>>> ans = u2.kl_loss('Uniform', low_b, high_b, low_a, high_a)
>>> print(ans.shape)
(2,)
>>> # Examples of `sample`.
>>> # Args:
>>> #     shape (tuple): the shape of the sample. Default: ()
>>> #     low (Tensor): the lower bound of the distribution. Default: self.low.
>>> #     high (Tensor): the upper bound of the distribution. Default: self.high.
>>> ans = u1.sample()
>>> print(ans.shape)
()
>>> ans = u1.sample((2,3))
>>> print(ans.shape)
(2, 3)
>>> ans = u1.sample((2,3), low_b, high_b)
>>> print(ans.shape)
(2, 3, 2)
>>> ans = u2.sample((2,3), low_a, high_a)
>>> print(ans.shape)
(2, 3, 2)
property high

Return the upper bound of the distribution.

Returns

Tensor, the upper bound of the distribution.

property low

Return the upper bound of the distribution.

Returns

Tensor, the lower bound of the distribution.

cdf(value, high, low)

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

Parameters

  • value (Tensor) - the value to compute.

  • high (Tensor) - the upper bound of the distribution. Default value: None.

  • low (Tensor) - the lower bound of the distribution. Default value: None.

Returns

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

cross_entropy(dist, high_b, low_b, high, low)

Compute the cross entropy of two distribution.

Parameters

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

  • high_b (Tensor) - the upper bound of the other distribution.

  • low_b (Tensor) - the lower bound of the other distribution.

  • high (Tensor) - the upper bound of the distribution. Default value: None.

  • low (Tensor) - the lower bound of the distribution. Default value: None.

Returns

Tensor, the value of the cross entropy.

entropy(high, low)

Compute the value of the entropy.

Parameters

  • high (Tensor) - the upper bound of the distribution. Default value: None.

  • low (Tensor) - the lower bound of the distribution. Default value: None.

Returns

Tensor, the value of the entropy.

kl_loss(dist, high_b, low_b, high, low)

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

Parameters

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

  • high_b (Tensor) - the upper bound of the other distribution.

  • low_b (Tensor) - the lower bound of the other distribution.

  • high (Tensor) - the upper bound of the distribution. Default value: None.

  • low (Tensor) - the lower bound of the distribution. Default value: None.

Returns

Tensor, the value of the K-L loss.

log_cdf(value, high, low)

Compute the log value of the cumulatuve distribution function.

Parameters

  • value (Tensor) - the value to compute.

  • high (Tensor) - the upper bound of the distribution. Default value: None.

  • low (Tensor) - the lower bound of the distribution. Default value: None.

Returns

Tensor, the log value of the cumulatuve distribution function.

log_prob(value, high, low)

the log value of the probability.

Parameters

  • value (Tensor) - the value to compute.

  • high (Tensor) - the upper bound of the distribution. Default value: None.

  • low (Tensor) - the lower bound of the distribution. Default value: None.

Returns

Tensor, the log value of the probability.

log_survival(value, high, low)

Compute the log value of the survival function.

Parameters

  • value (Tensor) - the value to compute.

  • high (Tensor) - the upper bound of the distribution. Default value: None.

  • low (Tensor) - the lower bound of the distribution. Default value: None.

Returns

Tensor, the value of the K-L loss.

mean(high, low)

Compute the mean value of the distribution.

Parameters

  • high (Tensor) - the upper bound of the distribution. Default value: None.

  • low (Tensor) - the lower bound of the distribution. Default value: None.

Returns

Tensor, the mean of the distribution.

mode(high, low)

Compute the mode value of the distribution.

Parameters

  • high (Tensor) - the upper bound of the distribution. Default value: None.

  • low (Tensor) - the lower bound of the distribution. Default value: None.

Returns

Tensor, the mode of the distribution.

prob(value, high, low)

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

Parameters

  • value (Tensor) - the value to compute.

  • high (Tensor) - the upper bound of the distribution. Default value: None.

  • low (Tensor) - the lower bound of the distribution. Default value: None.

Returns

Tensor, the value of the probability.

sample(shape, high, low)

Generate samples.

Parameters

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

  • high (Tensor) - the upper bound of the distribution. Default value: None.

  • low (Tensor) - the lower bound of the distribution. Default value: None.

Returns

Tensor, the sample following the distribution.

sd(high, low)

The standard deviation.

Parameters

  • high (Tensor) - the upper bound of the distribution. Default value: None.

  • low (Tensor) - the lower bound of the distribution. Default value: None.

Returns

Tensor, the standard deviation of the distribution.

survival_function(value, high, low)

Compute the value of the survival function.

Parameters

  • value (Tensor) - the value to compute.

  • high (Tensor) - the upper bound of the distribution. Default value: None.

  • low (Tensor) - the lower bound of the distribution. Default value: None.

Returns

Tensor, the value of the survival function.

var(high, low)

Compute the variance of the distribution.

Parameters

  • high (Tensor) - the upper bound of the distribution. Default value: None.

  • low (Tensor) - the lower bound of the distribution. Default value: None.

Returns

Tensor, the variance of the distribution.