fn

bernoulli

Tensor
bernoulli(p: _float, size: list[_int] | tuple[_int, ...] | None = None, dtype: DTypeLike = None, device: DeviceLike = None, requires_grad: _bool = False, generator: _C_engine.Generator | None = None)
source

Return a tensor of independent Bernoulli trials with success probability p.

Each element is drawn from the Bernoulli distribution:

XiBernoulli(p),P(Xi=1)=p,P(Xi=0)=1pX_i \sim \text{Bernoulli}(p), \quad P(X_i = 1) = p, \quad P(X_i = 0) = 1 - p

with mean E[Xi]=p\mathbb{E}[X_i] = p and variance Var[Xi]=p(1p)\operatorname{Var}[X_i] = p(1-p).

Parameters

pfloat
Success probability. Must satisfy 0p10 \le p \le 1.
sizelist[int] or tuple[int, ...]
Shape of the output tensor. Defaults to (1,) when None.
dtypelucid.dtype
Data type of the output. Defaults to the global default dtype. Use lucid.bool_ for boolean masks or lucid.float32 for soft weights.
devicestr or lucid.device
Target device — "cpu" or "metal".
requires_gradbool
Enable autograd tracking. Default: False.
generatorlucid._C.engine.Generator
Explicit generator. Defaults to the global default.

Returns

Tensor

Tensor of shape size with values in {0,1}\{0, 1\}.

Notes

Internally sampled as Xi=1[Ui<p]X_i = \mathbf{1}[U_i < p] where UiU[0,1)U_i \sim U[0,1), giving exact Bernoulli probabilities.

Dropout (Srivastava et al., 2014) is the canonical use case. A dropout mask mBernoulli(1drop_rate)nm \sim \text{Bernoulli}(1-\text{drop\_rate})^n and the retained activations are scaled by 1/(1drop_rate)1/(1-\text{drop\_rate}) to preserve expected magnitude:

x~i=ximi1pdrop\tilde{x}_i = \frac{x_i \cdot m_i}{1 - p_{\text{drop}}}

Examples

>>> import lucid
>>> lucid.manual_seed(0)
>>> lucid.bernoulli(0.3, size=(10,)).tolist()   # ≈ 30 % ones
[0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0]
Dropout mask for a hidden layer:
>>> h = lucid.randn(32, 512)
>>> keep = lucid.bernoulli(0.8, size=(32, 512))
>>> h_dropped = h * keep / 0.8