mindspore.nn.probability.distribution.Categorical
- class mindspore.nn.probability.distribution.Categorical(probs=None, seed=None, dtype=mstype.int32, name='Categorical')[source]
- Categorical distribution. A Categorical Distribution is a discrete distribution with the range \(\{1, 2, ..., k\}\) and the probability mass function as \(P(X = i) = p_i, i = 1, ..., k\). - Parameters
- probs (Tensor, list, numpy.ndarray) – Event probabilities. Default: - None.
- seed (int) – The global seed is used in sampling. Global seed is used if it is None. Default: - None.
- dtype (mindspore.dtype) – The type of the event samples. Default: - mstype.int32.
- name (str) – The name of the distribution. Default: - 'Categorical'.
 
 - Note - probs must have rank at least 1, values are proper probabilities and sum to 1. - Raises
- ValueError – When the sum of all elements in probs is not 1. 
 - Supported Platforms:
- Ascend- GPU
 - Examples - >>> import mindspore >>> import mindspore.nn as nn >>> import mindspore.nn.probability.distribution as msd >>> from mindspore import Tensor >>> # To initialize a Categorical distribution of probs [0.5, 0.5] >>> ca1 = msd.Categorical(probs=[0.2, 0.8], dtype=mindspore.int32) >>> # A Categorical distribution can be initialized without arguments. >>> # In this case, `probs` must be passed in through arguments during function calls. >>> ca2 = msd.Categorical(dtype=mindspore.int32) >>> # Here are some tensors used below for testing >>> value = Tensor([1, 0], dtype=mindspore.int32) >>> probs_a = Tensor([0.5, 0.5], dtype=mindspore.float32) >>> probs_b = Tensor([0.35, 0.65], dtype=mindspore.float32) >>> # Private interfaces of probability functions corresponding to public interfaces, including >>> # `prob`, `log_prob`, `cdf`, `log_cdf`, `survival_function`, and `log_survival`, are the same as follows. >>> # Args: >>> # value (Tensor): the value to be evaluated. >>> # probs (Tensor): event probabilities. Default: self.probs. >>> # Examples of `prob`. >>> # Similar calls can be made to other probability functions >>> # by replacing `prob` by the name of the function. >>> ans = ca1.prob(value) >>> print(ans.shape) (2,) >>> # Evaluate `prob` with respect to distribution b. >>> ans = ca1.prob(value, probs_b) >>> print(ans.shape) (2,) >>> # `probs` must be passed in during function calls. >>> ans = ca2.prob(value, probs_a) >>> print(ans.shape) (2,) >>> # Functions `mean`, `sd`, `var`, and `entropy` have the same arguments. >>> # Args: >>> # probs (Tensor): event probabilities. Default: self.probs. >>> # Examples of `mean`. `sd`, `var`, and `entropy` are similar. >>> ans = ca1.mean() # return 0.8 >>> print(ans.shape) (1,) >>> ans = ca1.mean(probs_b) >>> print(ans.shape) (1,) >>> # `probs` must be passed in during function calls. >>> ans = ca2.mean(probs_a) >>> print(ans.shape) (1,) >>> # Interfaces of `kl_loss` and `cross_entropy` are the same as follows: >>> # Args: >>> # dist (str): the name of the distribution. Only 'Categorical' is supported. >>> # probs_b (Tensor): event probabilities of distribution b. >>> # probs (Tensor): event probabilities of distribution a. Default: self.probs. >>> # Examples of `kl_loss`, `cross_entropy` is similar. >>> ans = ca1.kl_loss('Categorical', probs_b) >>> print(ans.shape) () >>> ans = ca1.kl_loss('Categorical', probs_b, probs_a) >>> print(ans.shape) () >>> # An additional `probs` must be passed in. >>> ans = ca2.kl_loss('Categorical', probs_b, probs_a) >>> print(ans.shape) () - property probs
- Return the event probability. - Returns
- Tensor, the event probability. 
 
 - cdf(value, probs)[source]
- Compute the cumulatuve distribution function(CDF) of the given value. - Parameters
- value (Tensor) - the value to compute. 
- probs (Tensor) - the event probability. Default: - None.
 
- Returns
- Tensor, the value of the cumulatuve distribution function for the given input. 
 
 - cross_entropy(dist, probs_b, probs)[source]
- Compute the cross entropy of two distribution. - Parameters
- dist (str) - the type of the other distribution. 
- probs_b (Tensor) - the event probability of the distribution b. 
- probs (Tensor) - the event probability of the distribution a. Default: - None.
 
- Returns
- Tensor, the value of the cross entropy. 
 
 - entropy(probs)[source]
- Compute the value of the entropy. - Parameters
- probs (Tensor) - the event probability. Default: - None.
 
- Returns
- Tensor, the value of the entropy. 
 
 - kl_loss(dist, probs_b, probs)[source]
- Compute the value of the K-L loss between two distribution, namely KL(a||b). - Parameters
- dist (str) - the type of the other distribution. 
- probs_b (Tensor) - the event probability of the distribution b. 
- probs (Tensor) - the event probability of the distribution a. Default: - None.
 
- Returns
- Tensor, the value of the K-L loss. 
 
 - log_cdf(value, probs)[source]
- Compute the log value of the cumulatuve distribution function. - Parameters
- value (Tensor) - the value to compute. 
- probs (Tensor) - the event probability. Default: - None.
 
- Returns
- Tensor, the log value of the cumulatuve distribution function. 
 
 - log_prob(value, probs)[source]
- the log value of the probability. - Parameters
- value (Tensor) - the value to compute. 
- probs (Tensor) - the event probability. Default: - None.
 
- Returns
- Tensor, the log value of the probability. 
 
 - log_survival(value, probs)[source]
- Compute the log value of the survival function. - Parameters
- value (Tensor) - the value to compute. 
- probs (Tensor) - the event probability. Default: - None.
 
- Returns
- Tensor, the value of the K-L loss. 
 
 - mean(probs)[source]
- Compute the mean value of the distribution. - Parameters
- probs (Tensor) - the event probability. Default: - None.
 
- Returns
- Tensor, the mean of the distribution. 
 
 - mode(probs)[source]
- Compute the mode value of the distribution. - Parameters
- probs (Tensor) - the event probability. Default: - None.
 
- Returns
- Tensor, the mode of the distribution. 
 
 - prob(value, probs)[source]
- The probability of the given value. For the discrete distribution, it is the probability mass function(pmf). - Parameters
- value (Tensor) - the value to compute. 
- probs (Tensor) - the event probability. Default: - None.
 
- Returns
- Tensor, the value of the probability. 
 
 - sample(shape, probs)[source]
- Generate samples. - Parameters
- shape (tuple) - the shape of the sample. 
- probs (Tensor) - the event probability. Default: - None.
 
- Returns
- Tensor, the sample following the distribution. 
 
 - sd(probs)[source]
- The standard deviation. - Parameters
- probs (Tensor) - the event probability. Default: - None.
 
- Returns
- Tensor, the standard deviation of the distribution.