fn
bernoulli
→Tensorbernoulli(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)Return a tensor of independent Bernoulli trials with success probability p.
Each element is drawn from the Bernoulli distribution:
with mean and variance .
Parameters
pfloatSuccess probability. Must satisfy .
sizelist[int] or tuple[int, ...]Shape of the output tensor. Defaults to
(1,) when None.dtypelucid.dtypeData 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.deviceTarget device —
"cpu" or "metal".requires_gradboolEnable autograd tracking. Default:
False.generatorlucid._C.engine.GeneratorExplicit generator. Defaults to the global default.
Returns
TensorTensor of shape size with values in .
Notes
Internally sampled as where , giving exact Bernoulli probabilities.
Dropout (Srivastava et al., 2014) is the canonical use case. A dropout mask and the retained activations are scaled by to preserve expected magnitude:
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