Skip to content
class

RelaxedOneHotCategorical

extends_RelaxedFamily
RelaxedOneHotCategorical(temperature: Tensor | float, probs: Tensor | None = None, logits: Tensor | None = None, validate_args: bool | None = None)

Concrete distribution — a continuous relaxation of OneHotCategorical.

RelaxedOneHotCategorical(temperature=τ, probs=p) defines a distribution over the open KK-simplex whose samples are differentiable surrogates for one-hot categorical samples. As τ→0\tau \to 0 the distribution concentrates on the KK vertices of the simplex (recovering one-hot samples); as τ→∞\tau \to \infty samples approach the uniform distribution over the simplex.

This is the Gumbel-softmax distribution of Jang et al. (2017) and the Concrete distribution of Maddison et al. (2017).

It is a lucid.distributions.TransformedDistribution: an ExpRelaxedCategorical (base_dist) pushed through an lucid.distributions.transforms.ExpTransform (transforms).

Parameters

temperatureTensor | float
Temperature parameter τ>0\tau > 0. Smaller values give sharper (more discrete) samples.
probsTensor | None= None
Probability vector(s) of shape (..., K). Normalised internally. Mutually exclusive with logits.
logitsTensor | None= None
Unnormalised log-probability vector(s) of shape (..., K). Mutually exclusive with probs.
validate_argsbool | None= None
If True, validate parameter constraints at construction time.

Attributes

temperatureTensor
Temperature τ\tau.
probsTensor
Normalised probability vector — stored when given, otherwise derived from logits on access.
logitsTensor
Log-probability vector — as given, otherwise derived from probs on access.
base_distExpRelaxedCategorical
The distribution of the log-simplex point.
transformslist[Transform]
[ExpTransform()].

Notes

Reparameterised sampling (Gumbel-softmax trick):

yk=exp⁡ ⁣((lk+gk)/τ)∑jexp⁡ ⁣((lj+gj)/τ),gk∼iidGumbel⁡(0,1)y_k = \frac{\exp\!\bigl((l_k + g_k)/\tau\bigr)} {\sum_j \exp\!\bigl((l_j + g_j)/\tau\bigr)}, \quad g_k \overset{\text{iid}}{\sim} \operatorname{Gumbel}(0, 1)

The result y∈ΔK−1y \in \Delta^{K-1} (open simplex) is differentiable w.r.t. ll and τ\tau.

Log-PDF (Maddison et al. 2017, Eq. 1):

log⁡p(y;l,τ)=log⁡Γ(K)+(K−1)log⁡τ+∑k(lk−(τ+1)log⁡yk)−Klogsumexp⁡k(lk−τlog⁡yk)\log p(y; l, \tau) = \log\Gamma(K) + (K-1)\log\tau + \sum_k (l_k - (\tau+1)\log y_k) - K \operatorname{logsumexp}_k(l_k - \tau\log y_k)

Examples

>>> import lucid
>>> from lucid.distributions import RelaxedOneHotCategorical
>>> dist = RelaxedOneHotCategorical(
...     temperature=0.5,
...     probs=lucid.tensor([0.1, 0.4, 0.5]),
... )
>>> samples = dist.rsample((50,))
>>> samples.shape  # (50, 3) — lies on the open simplex
(50, 3)
>>> bool(((samples.sum(dim=-1) - 1.0).abs() < 1e-5).all())  # each row sums to ~1
True

Used by 1

Constructors

1
dunder

__init__

→None
__init__(temperature: Tensor | float, probs: Tensor | None = None, logits: Tensor | None = None, validate_args: bool | None = None)

Construct a RelaxedOneHotCategorical (Concrete) distribution.

Parameters

temperatureTensor | float
Temperature λ>0\lambda > 0 controlling relaxation tightness. As λ→0\lambda \to 0, samples concentrate on the vertices of the simplex recovering a hard one-hot Categorical; as λ→∞\lambda \to \infty, samples concentrate near the simplex centroid (1/K,…,1/K)(1/K, \ldots, 1/K).
probsTensor | None= None
Probability vector of shape (..., K) on the K-simplex. Rows are automatically normalised to sum to 1. Mutually exclusive with logits.
logitsTensor | None= None
Unnormalised log-probabilities of shape (..., K). Mutually exclusive with probs.
validate_argsbool | None= None
If True, validate parameter constraints at construction time.

Raises

ValueError
If both or neither of probs and logits are provided.

Properties

3
prop

logits

→Tensor
logits: Tensor

Log-probabilities — as given, or derived from probs.

prop

probs

→Tensor
probs: Tensor

Normalised probabilities — as given, or softmax(logits).

prop

temperature

→Tensor
temperature: Tensor

Temperature τ\tau of the relaxation.

Instance methods

1
fn

expand

→Self
expand(batch_shape: Sequence[int], _instance: Self | None = None)

Return a new RelaxedOneHotCategorical expanded to batch_shape.