OneHotCategorical
DistributionOneHotCategorical(probs: Tensor | None = None, logits: Tensor | None = None, validate_args: bool | None = None)Categorical distribution with one-hot encoded samples.
OneHotCategorical wraps a Categorical and returns samples
as one-hot vectors of shape (..., K) instead of integer indices.
It is particularly useful for:
- REINFORCE-style gradient estimators where you need a discrete sample but want to use it in differentiable downstream computation.
- Relaxations — replacing with
RelaxedOneHotCategoricalgives a differentiable approximation that converges to one-hot as temperature .
Parameters
(..., K). Normalised internally.
Mutually exclusive with logits.(..., K). Mutually exclusive with
probs.validate_argsbool | None= NoneTrue, validate parameter constraints at construction time.Attributes
Notes
Samples are integer-valued one-hot vectors in with exactly one 1 at the sampled category index.
Log-probability for a one-hot vector :
which is simply the log-probability of the selected category.
Entropy equals that of the underlying Categorical:
The event_shape is (K,) whereas Categorical has
event_shape = ().
Examples
>>> import lucid
>>> from lucid.distributions import OneHotCategorical
>>> dist = OneHotCategorical(probs=lucid.tensor([0.1, 0.5, 0.4]))
>>> sample = dist.sample()
>>> sample.shape # (3,) — one-hot
(3,)
>>> sample.sum() # always 1
tensor(1.)Used by 1
Constructors
1__init__
→None__init__(probs: Tensor | None = None, logits: Tensor | None = None, validate_args: bool | None = None)Initialise a OneHotCategorical distribution.
Parameters
(..., K). Normalised
internally. Mutually exclusive with logits.(..., K). Mutually
exclusive with probs.validate_argsbool | None= NoneTrue, validate parameter constraints at construction time.Raises
ValueErrorprobs and logits are provided.Properties
4Mean of the OneHotCategorical distribution.
A one-hot sample is a vector of indicators, and the expectation of an indicator is the probability of the event it indicates — so the mean is the probability vector itself.
Unlike the underlying Categorical, whose labels carry no
metric, this one is perfectly well defined: the one-hot encoding
supplies the vector space the expectation needs.
Returns
TensorProbabilities of shape batch_shape + (K,).
Per-coordinate standard deviation, .
Support of the distribution: the probability simplex.
Returns
ConstraintThe simplex constraint, as each sample is a one-hot vector
whose entries are non-negative and sum to 1.
Variance of the OneHotCategorical distribution.
Each coordinate is a Bernoulli indicator, so
This is the diagonal of the covariance; the off-diagonal terms are not returned.
Returns
TensorPer-coordinate variances of shape batch_shape + (K,).
Instance methods
5Shannon entropy of the OneHotCategorical distribution.
Equal to the entropy of the underlying Categorical:
Returns
TensorEntropy values of shape batch_shape (nats).
Log-probabilities, from the underlying Categorical.
Normalised probabilities, from the underlying Categorical.
Draw one-hot encoded samples.
Internally samples category indices from the underlying
Categorical distribution and converts them to one-hot
vectors via one_hot.
Parameters
sample_shapetuple[int, ...]= ()Returns
TensorFloat tensor of shape (*sample_shape, *batch_shape, K)
containing one-hot vectors (exactly one 1 per row).