Skip to content
class

RelaxedBernoulli

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

Concrete (Gumbel-sigmoid) relaxation of the Bernoulli distribution.

RelaxedBernoulli(temperature=τ, probs=p) defines a continuous distribution over (0,1)(0, 1) whose samples are differentiable surrogates for Bernoulli samples. As τ→0\tau \to 0 the distribution concentrates on {0,1}\{0, 1\}, recovering the discrete Bernoulli. As τ→∞\tau \to \infty the distribution approaches Uniform⁡(0,1)\operatorname{Uniform}(0, 1).

The Concrete distribution (Maddison et al. 2017) / Gumbel-softmax (Jang et al. 2017) trick enables gradient-based optimisation through discrete latent variables in variational autoencoders and related models.

It is a lucid.distributions.TransformedDistribution: a LogitRelaxedBernoulli (base_dist) pushed through a lucid.distributions.transforms.SigmoidTransform (transforms).

Parameters

temperatureTensor | float
Temperature parameter τ>0\tau > 0. Controls the sharpness of the relaxation. Small values give near-discrete samples; large values give near-uniform samples.
probsTensor | float | None= None
Bernoulli success probability p∈[0,1]p \in [0, 1]. Mutually exclusive with logits.
logitsTensor | float | None= None
Log-odds l=log⁡(p/(1−p))∈Rl = \log(p/(1-p)) \in \mathbb{R}. Mutually exclusive with probs.
validate_argsbool | None= None
If True, validate parameter constraints at construction time.

Attributes

temperatureTensor
Temperature τ\tau.
probsTensor
Success probability — stored when given, otherwise derived from logits on access.
logitsTensor
Log-odds — stored when given, otherwise derived from probs on access.
base_distLogitRelaxedBernoulli
The distribution of the pre-sigmoid logit.
transformslist[Transform]
[SigmoidTransform()].

Notes

Reparameterised sampling (Gumbel-sigmoid trick):

y=σ ⁣(l+g1−g2τ),g1,g2∼iidGumbel⁡(0,1)y = \sigma\!\left(\frac{l + g_1 - g_2}{\tau}\right), \quad g_1, g_2 \overset{\text{iid}}{\sim} \operatorname{Gumbel}(0, 1)

where σ(⋅)\sigma(\cdot) is the sigmoid function. Gradients propagate through both ll and τ\tau.

Log-PDF (Maddison et al. 2017, the binary Concrete density):

log⁡p(y;l,τ)=log⁡τ+d−2log⁡ ⁣(1+ed)−log⁡y−log⁡(1−y),d=l−τ logit(y)\log p(y; l, \tau) = \log\tau + d - 2\log\!\left(1 + e^{d}\right) - \log y - \log(1 - y), \qquad d = l - \tau\,\text{logit}(y)

where logit(y)=log⁡(y/(1−y))\text{logit}(y) = \log(y/(1-y)). The first three terms are the logistic density of logit(Y)\text{logit}(Y); the last two are the Jacobian of the sigmoid that maps it into (0,1)(0, 1). A value of 0 or 1 is scored one step inside the interval, as the reference framework's inverse sigmoid does.

Examples

>>> import lucid
>>> from lucid.distributions import RelaxedBernoulli
>>> _ = lucid.manual_seed(0)
>>> dist = RelaxedBernoulli(temperature=0.5, probs=0.7)
>>> samples = dist.rsample((100,))
>>> # Samples are in (0, 1)
>>> bool(((samples > 0) & (samples < 1)).all())
True

Used by 1

Constructors

1
dunder

__init__

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

Construct a RelaxedBernoulli (Binary Concrete) distribution.

Parameters

temperatureTensor | float
Temperature λ>0\lambda > 0 controlling the relaxation tightness. As λ→0\lambda \to 0, samples concentrate on {0,1}\{0, 1\} recovering a hard Bernoulli; as λ→∞\lambda \to \infty, samples concentrate around 1/21/2.
probsTensor | float | None= None
Probability p∈[0,1]p \in [0, 1] parameter. Mutually exclusive with logits.
logitsTensor | float | None= None
Log-odds log⁡p1−p\log\frac{p}{1-p} parameter. 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-odds — as given, or derived from probs.

prop

probs

→Tensor
probs: Tensor

Success probability — as given, or sigmoid(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 RelaxedBernoulli instance with parameters expanded to batch_shape.