Skip to content
class

ContinuousBernoulli

extendsDistribution
ContinuousBernoulli(probs: Tensor | float | None = None, logits: Tensor | float | None = None, lims: tuple[float, float] = (0.499, 0.501), validate_args: bool | None = None)

Continuous Bernoulli distribution on [0,1][0, 1].

A continuous relaxation of the lucid.distributions.Bernoulli distribution introduced by Loaiza-Ganem & Cunningham (2019) to fix the well-known bias incurred by VAE decoders that use a Bernoulli likelihood on real-valued image pixels in [0,1][0, 1]. Unlike the naive "Bernoulli on [0,1]" likelihood, the Continuous Bernoulli has a proper normalising constant and yields unbiased ELBO gradients.

Parameters

probsTensor or float= None
Parameter λ∈[0,1]\lambda \in [0, 1]. Note that E[X]≠λ\mathbb{E}[X] \neq \lambda in general — see Notes for the closed-form mean. Mutually exclusive with logits.
logitsTensor or float= None
Log-odds ℓ=log⁡(λ/(1−λ))\ell = \log(\lambda / (1 - \lambda)). Mutually exclusive with probs.
limstuple[float, float]= (0.499, 0.501)
Interval of λ\lambda around 12\tfrac12, as (lower, upper), inside which the normaliser, mean, variance and entropy are evaluated from their series expansions about 12\tfrac12 instead of their closed forms (which divide 00 by 00 there). Default (0.499, 0.501), the reference framework's. The distribution also switches to the series wherever float32 needs it (∣λ−12∣<0.1|\lambda - \tfrac12| < 0.1 for the moments), so lims can only widen that region. cdf, icdf and sampling are cancellation-free closed forms and ignore it.
validate_argsbool= None
If True, validate parameter constraints at construction time.

Raises

ValueError
If lims is not a pair lower <= upper inside [0,1][0, 1].

Notes

Probability density on x∈[0,1]x \in [0, 1]:

p(x;λ)=C(λ) λx(1−λ)1−xp(x; \lambda) = C(\lambda)\, \lambda^x (1 - \lambda)^{1 - x}

where the normalising constant is

C(λ)={2tanh⁡−1(1−2λ)1−2λλ≠122λ=12C(\lambda) = \begin{cases} \dfrac{2 \tanh^{-1}(1 - 2\lambda)}{1 - 2\lambda} & \lambda \neq \tfrac{1}{2} \\[4pt] 2 & \lambda = \tfrac{1}{2} \end{cases}

Mean (closed form):

E[X]={λ2λ−1+12tanh⁡−1(1−2λ)λ≠1212λ=12\mathbb{E}[X] = \begin{cases} \dfrac{\lambda}{2\lambda - 1} + \dfrac{1}{2 \tanh^{-1}(1 - 2\lambda)} & \lambda \neq \tfrac{1}{2} \\[4pt] \tfrac{1}{2} & \lambda = \tfrac{1}{2} \end{cases}

Variance (closed form, ℓ=logit⁡λ\ell = \operatorname{logit}\lambda):

Var[X]=λ(λ−1)(1−2λ)2+1ℓ2(λ≠12),Var[X]=112(λ=12)\mathrm{Var}[X] = \frac{\lambda(\lambda - 1)}{(1 - 2\lambda)^2} + \frac{1}{\ell^2} \quad (\lambda \neq \tfrac{1}{2}), \qquad \mathrm{Var}[X] = \tfrac{1}{12} \quad (\lambda = \tfrac{1}{2})

Both closed forms are differences of terms of order 1/(1−2λ)1/(1 - 2\lambda), which cancel catastrophically as λ→12\lambda \to \tfrac{1}{2}; within ∣λ−12∣<0.1|\lambda - \tfrac{1}{2}| < 0.1 the mean and variance are evaluated from their Taylor series in λ−12\lambda - \tfrac{1}{2} instead.

Important properties:

  • Support: [0,1][0, 1] (continuous, not just {0,1}\{0, 1\}).
  • Reduces to Uniform(0,1)\mathrm{Uniform}(0, 1) at λ=1/2\lambda = 1/2.
  • At λ→0\lambda \to 0 or λ→1\lambda \to 1 the density concentrates near 0 or 1 respectively.
  • Reparameterisable via inverse CDF.

The Continuous Bernoulli is the correct likelihood for decoders that output a value in [0,1][0, 1] (e.g. MNIST pixel intensities) when the model is to remain a valid probabilistic model. Using a plain Bernoulli on continuous data produces an improper density and a systematically biased ELBO.

Examples

>>> import lucid
>>> from lucid.distributions import ContinuousBernoulli
>>> d = ContinuousBernoulli(probs=0.7)
>>> d.rsample((4,)).shape
(4,)
>>> d.log_prob(lucid.tensor(0.5))
tensor(-0.02974)

Used by 2

Constructors

1
dunder

__init__

→None
__init__(probs: Tensor | float | None = None, logits: Tensor | float | None = None, lims: tuple[float, float] = (0.499, 0.501), validate_args: bool | None = None)

Initialise a Continuous Bernoulli distribution.

Parameters

probsTensor | float | None= None
Parameter 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.
limstuple[float, float]= (0.499, 0.501)
Interval of pp around 12\tfrac12 evaluated from series expansions rather than closed forms.
validate_argsbool | None= None
If True, validate parameter constraints at construction time.

Raises

ValueError
If lims is not a pair lower <= upper inside [0, 1].

Properties

2
prop

mean

→Tensor
mean: Tensor

Mean λ/(2λ−1)+1/(2tanh⁡−1(1−2λ))\lambda/(2\lambda - 1) + 1/(2\tanh^{-1}(1 - 2\lambda)).

The second term is −1/logit⁡λ-1/\operatorname{logit}\lambda. Near λ=12\lambda = \tfrac12 the two terms cancel, so there the Taylor series 12+t3+16t345+704t5945+27392t714175\tfrac12 + \tfrac{t}{3} + \tfrac{16 t^3}{45} + \tfrac{704 t^5}{945} + \tfrac{27392 t^7}{14175} in t=λ−12t = \lambda - \tfrac12 is used.

Examples

>>> import lucid
>>> from lucid.distributions import ContinuousBernoulli
>>> ContinuousBernoulli(probs=lucid.tensor([0.2, 0.5, 0.7])).mean
tensor([0.388, 0.5, 0.5698])
prop

variance

→Tensor
variance: Tensor

Variance λ(λ−1)/(1−2λ)2+1/ℓ2\lambda(\lambda-1)/(1-2\lambda)^2 + 1/\ell^2.

ℓ=logit⁡λ\ell = \operatorname{logit}\lambda. The two terms are each of order 1/(1−2λ)21/(1-2\lambda)^2 and cancel near λ=12\lambda = \tfrac12, so there the Taylor series 112−x15−128x2945−4864x314175\tfrac1{12} - \tfrac{x}{15} - \tfrac{128 x^2}{945} - \tfrac{4864 x^3}{14175} in x=(λ−12)2x = (\lambda - \tfrac12)^2 is used. Nothing is clamped: the variance is positive for every λ\lambda, and a negative value would be a defect to see, not to hide.

Examples

>>> import lucid
>>> from lucid.distributions import ContinuousBernoulli
>>> ContinuousBernoulli(probs=lucid.tensor([0.2, 0.5, 0.7])).variance
tensor([0.0759, 0.08333, 0.08043])

Instance methods

8
fn

cdf

→Tensor
cdf(value: Tensor)

Cumulative distribution function of the Continuous Bernoulli.

F(x)=λx(1−λ)1−x+λ−12λ−1=expm1⁡(ℓx)expm1⁡(ℓ)F(x) = \frac{\lambda^x (1 - \lambda)^{1 - x} + \lambda - 1} {2\lambda - 1} = \frac{\operatorname{expm1}(\ell x)}{\operatorname{expm1}(\ell)}

with ℓ=logit⁡λ\ell = \operatorname{logit}\lambda, and xx at λ=12\lambda = \tfrac12. The second form has no cancellation as λ→12\lambda \to \tfrac12, where the reference framework replaces the first with xx inside ∣λ−12∣<10−3|\lambda - \tfrac12| < 10^{-3}; there the two differ by up to ∣ℓ∣/8|\ell|/8, and this is the exact value.

Returns probabilities in [0,1][0, 1]; 0 at and below 0, 1 at and above 1.

Examples

>>> import lucid
>>> from lucid.distributions import ContinuousBernoulli
>>> ContinuousBernoulli(probs=0.7).cdf(lucid.tensor(0.5))
tensor(0.3956)
fn

entropy

→Tensor
entropy()

Shannon entropy of the Continuous Bernoulli (in nats).

H=softplus⁡(ℓ)−ℓ E[X]−ln⁡C(λ)H = \operatorname{softplus}(\ell) - \ell\,\mathbb E[X] - \ln C(\lambda)

−E[log⁡p(X)]-\mathbb E[\log p(X)] with log⁡p(x)=xℓ−softplus⁡(ℓ)+ln⁡C\log p(x) = x\ell - \operatorname{softplus}(\ell) + \ln C, so it reuses mean and the normaliser, each already evaluated by its series near λ=12\lambda = \tfrac12.

Returns

Tensor

Entropy in nats, shape batch_shape; 0 at λ=12\lambda = \tfrac12, where the distribution is uniform.

Examples

>>> import lucid
>>> from lucid.distributions import ContinuousBernoulli
>>> ContinuousBernoulli(probs=0.5).entropy()
tensor(0.)
fn

expand

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

Return a new ContinuousBernoulli with batch dimensions expanded to batch_shape.

fn

icdf

→Tensor
icdf(value: Tensor)

Quantile function of the Continuous Bernoulli.

Q(p)=log1p⁡ ⁣(pexpm1⁡(ℓ))ℓQ(p) = \frac{\operatorname{log1p}\!\left(p \operatorname{expm1}(\ell)\right)}{\ell}

for ℓ<0\ell < 0, reflected through Qℓ(p)=1−Q−ℓ(1−p)Q_\ell(p) = 1 - Q_{-\ell}(1 - p) for ℓ>0\ell > 0, and pp at ℓ=0\ell = 0.

Returns quantiles in [0,1][0, 1].

Examples

>>> import lucid
>>> from lucid.distributions import ContinuousBernoulli
>>> ContinuousBernoulli(probs=0.7).icdf(lucid.tensor(0.3956))
tensor(0.5)
fn

log_prob

→Tensor
log_prob(value: Tensor)

x · l − softplus(l) + log C(p).

softplus rather than log(1 + exp(l)), which reached inf at l ≈ 89 in float32; and the logit of a probs of exactly 0 or 1 is taken one epsilon inside [0, 1] (logits), so it is never multiplied by a zero x while infinite.

fn

logits

→Tensor
logits()

Log-odds of λ\lambda — as given, or derived on access.

Derived from probs clamped one epsilon inside [0,1][0, 1], as the reference framework derives it.

fn

probs

→Tensor
probs()

Parameter λ\lambda — as given, or sigmoid(logits).

fn

rsample

→Tensor
rsample(sample_shape: tuple[int, ...] = ())

Reparameterised sample via the closed-form icdf.