mindspore.nn.probability.distribution.Normal
- class mindspore.nn.probability.distribution.Normal(mean=None, sd=None, seed=None, dtype=mstype.float32, name='Normal')[源代码]
- 正态分布(Normal distribution)。 连续随机分布,取值范围为 \((-\inf, \inf)\) ,概率密度函数为 \[f(x, \mu, \sigma) = 1 / \sigma\sqrt{2\pi} \exp(-(x - \mu)^2 / 2\sigma^2).\]- 其中 \(\mu, \sigma\) 分别为正态分布的期望与标准差。 - 参数:
- mean (int, float, list, numpy.ndarray, Tensor) - 正态分布的平均值。默认值: - None。
- sd (int, float, list, numpy.ndarray, Tensor) - 正态分布的标准差。默认值: - None。
- seed (int) - 采样时使用的种子。如果为None,则使用全局种子。默认值: - None。
- dtype (mindspore.dtype) - 事件样例的类型。默认值: - mstype.float32。
- name (str) - 分布的名称。默认值: - 'Normal'。
 
 - 说明 - sd 必须大于零。 
- dtype 必须是float,因为正态分布是连续的。 
 - 异常:
- ValueError - sd 中元素小于0。 
- TypeError - dtype 不是float的子类。 
 
- 支持平台:
- Ascend- GPU
 - 样例: - >>> import mindspore >>> import mindspore.nn as nn >>> import mindspore.nn.probability.distribution as msd >>> from mindspore import Tensor >>> # To initialize a Normal distribution of the mean 3.0 and the standard deviation 4.0. >>> n1 = msd.Normal(3.0, 4.0, dtype=mindspore.float32) >>> # A Normal distribution can be initialized without arguments. >>> # In this case, `mean` and `sd` must be passed in through arguments. >>> n2 = msd.Normal(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) >>> # 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. >>> # mean (Tensor): the mean of the distribution. Default: self._mean_value. >>> # sd (Tensor): the standard deviation of the distribution. Default: self._sd_value. >>> # Examples of `prob`. >>> # Similar calls can be made to other probability functions >>> # by replacing 'prob' by the name of the function >>> ans = n1.prob(value) >>> print(ans.shape) (3,) >>> # Evaluate with respect to the distribution b. >>> ans = n1.prob(value, mean_b, sd_b) >>> print(ans.shape) (3,) >>> # `mean` and `sd` must be passed in during function calls >>> ans = n2.prob(value, mean_a, sd_a) >>> print(ans.shape) (3,) >>> # Functions `mean`, `sd`, `var`, and `entropy` have the same arguments. >>> # Args: >>> # mean (Tensor): the mean of the distribution. Default: self._mean_value. >>> # sd (Tensor): the standard deviation of the distribution. Default: self._sd_value. >>> # Example of `mean`. `sd`, `var`, and `entropy` are similar. >>> ans = n1.mean() # return 0.0 >>> print(ans.shape) () >>> ans = n1.mean(mean_b, sd_b) # return mean_b >>> print(ans.shape) (3,) >>> # `mean` and `sd` must be passed in during function calls. >>> ans = n2.mean(mean_a, sd_a) >>> print(ans.shape) (3,) >>> # Interfaces of 'kl_loss' and 'cross_entropy' are the same: >>> # Args: >>> # dist (str): the type of the distributions. Only "Normal" is supported. >>> # mean_b (Tensor): the mean of distribution b. >>> # sd_b (Tensor): the standard deviation of distribution b. >>> # mean_a (Tensor): the mean of distribution a. Default: self._mean_value. >>> # sd_a (Tensor): the standard deviation of distribution a. Default: self._sd_value. >>> # Examples of `kl_loss`. `cross_entropy` is similar. >>> ans = n1.kl_loss('Normal', mean_b, sd_b) >>> print(ans.shape) (3,) >>> ans = n1.kl_loss('Normal', mean_b, sd_b, mean_a, sd_a) >>> print(ans.shape) (3,) >>> # Additional `mean` and `sd` must be passed in. >>> ans = n2.kl_loss('Normal', mean_b, sd_b, mean_a, sd_a) >>> print(ans.shape) (3,) >>> # Examples of `sample`. >>> # Args: >>> # shape (tuple): the shape of the sample. Default: () >>> # mean (Tensor): the mean of the distribution. Default: self._mean_value. >>> # sd (Tensor): the standard deviation of the distribution. Default: self._sd_value. >>> ans = n1.sample() >>> print(ans.shape) () >>> ans = n1.sample((2,3)) >>> print(ans.shape) (2, 3) >>> ans = n1.sample((2,3), mean_b, sd_b) >>> print(ans.shape) (2, 3, 3) >>> ans = n2.sample((2,3), mean_a, sd_a) >>> print(ans.shape) (2, 3, 3) - cdf(value, mean, sd)[源代码]
- 计算给定值的累积分布函数(Cumulative Distribution Function, CDF)。 - 参数:
- value (Tensor) - 要计算的值。 
- mean (Tensor) - 分布的期望。默认值: - None。
- sd (Tensor) - 分布的标准差。默认值: - None。
 
- 返回:
- Tensor,累积分布函数的值。 
 
 - cross_entropy(dist, mean_b, sd_b, mean, sd)[源代码]
- 计算分布a和b之间的交叉熵。 - 参数:
- dist (str) - 分布的类型。 
- mean_b (Tensor) - 对比分布的期望。 
- sd_b (Tensor) - 对比分布的标准差。 
- mean (Tensor) - 分布的期望。默认值: - None。
- sd (Tensor) - 分布的标准差。默认值: - None。
 
- 返回:
- Tensor,交叉熵的值。 
 
 - entropy(mean, sd)[源代码]
- 计算熵。 - 参数:
- mean (Tensor) - 分布的期望。默认值: - None。
- sd (Tensor) - 分布的标准差。默认值: - None。
 
- 返回:
- Tensor,熵的值。 
 
 - kl_loss(dist, mean_b, sd_b, mean, sd)[源代码]
- 计算KL散度,即KL(a||b)。 - 参数:
- dist (str) - 分布的类型。 
- mean_b (Tensor) - 对比分布的期望。 
- sd_b (Tensor) - 对比分布的标准差。 
- mean (Tensor) - 分布的期望。默认值: - None。
- sd (Tensor) - 分布的标准差。默认值: - None。
 
- 返回:
- Tensor,KL散度。 
 
 - log_cdf(value, mean, sd)[源代码]
- 计算给定值对应的累积分布函数的对数。 - 参数:
- value (Tensor) - 要计算的值。 
- mean (Tensor) - 分布的期望。默认值: - None。
- sd (Tensor) - 分布的标准差。默认值: - None。
 
- 返回:
- Tensor,累积分布函数的对数。 
 
 - log_prob(value, mean, sd)[源代码]
- 计算给定值对应的概率的对数。 - 参数:
- value (Tensor) - 要计算的值。 
- mean (Tensor) - 分布的期望。默认值: - None。
- sd (Tensor) - 分布的标准差。默认值: - None。
 
- 返回:
- Tensor,累积分布函数的对数。 
 
 - log_survival(value, mean, sd)[源代码]
- 计算给定值对应的生存函数的对数。 - 参数:
- value (Tensor) - 要计算的值。 
- mean (Tensor) - 分布的期望。默认值: - None。
- sd (Tensor) - 分布的标准差。默认值: - None。
 
- 返回:
- Tensor,生存函数的对数。 
 
 - mode(mean, sd)[源代码]
- 计算众数。 - 参数:
- mean (Tensor) - 分布的期望。默认值: - None。
- sd (Tensor) - 分布的标准差。默认值: - None。
 
- 返回:
- Tensor,概率分布的众数。 
 
 - prob(value, mean, sd)[源代码]
- 计算给定值下的概率。对于连续分布是计算概率密度函数(Probability Density Function)。 - 参数:
- value (Tensor) - 要计算的值。 
- mean (Tensor) - 分布的期望。默认值: - None。
- sd (Tensor) - 分布的标准差。默认值: - None。
 
- 返回:
- Tensor,概率值。 
 
 - sample(shape, mean, sd)[源代码]
- 采样函数。 - 参数:
- shape (tuple) - 样本的shape。 
- mean (Tensor) - 分布的期望。默认值: - None。
- sd (Tensor) - 分布的标准差。默认值: - None。
 
- 返回:
- Tensor,根据概率分布采样的样本。