class oneflow.distributions.Categorical(probs=None, logits=None, validate_args=None)

Creates a categorical distribution parameterized by either probs or logits (but not both).


It is equivalent to the distribution that oneflow.multinomial() samples from.

Samples are integers from \(\{0, \ldots, K-1\}\) where K is probs.size(-1). If probs is 1-dimensional with length-K, each element is the relative probability of sampling the class at that index. If probs is N-dimensional, the first N-1 dimensions are treated as a batch of relative probability vectors.


The probs argument must be non-negative, finite and have a non-zero sum, and it will be normalized to sum to 1 along the last dimension. probs will return this normalized value. The logits argument will be interpreted as unnormalized log probabilities and can therefore be any real number. It will likewise be normalized so that the resulting probabilities sum to 1 along the last dimension. logits will return this normalized value.

  • probs (Tensor) – event probabilities

  • logits (Tensor) – event log probabilities (unnormalized)

See also: oneflow.multinomial() For example:

>>> import oneflow as flow
>>> gen = flow.manual_seed(0)
>>> m = flow.distributions.categorical.Categorical(flow.tensor([ 0.25, 0.25, 0.25, 0.25 ]))
>>> m.sample()  # equal probability of 0, 1, 2, 3
tensor(3, dtype=oneflow.int64)