trunc_normal_(tensor: Tensor, mean: float = 0.0, std: float = 1.0, a: float = -2.0, b: float = 2.0, generator: _C_engine.Generator | None = None)Initialise tensor in-place with truncated normal samples.
Each entry is drawn from truncated to [a, b] via the inverse-CDF transform
(lucid.special.erfinv applied to uniform draws in
[2*Phi((a-mean)/std)-1, 2*Phi((b-mean)/std)-1]).
This is the preferred initialiser for transformer weights (e.g.
ViT, BERT) where unbounded Gaussian tails would otherwise produce
rare but disruptive activations.
Parameters
tensorTensorTensor to initialise in place; any shape is accepted.
meanfloat= 0.0Mean of the underlying (untruncated) Gaussian. Default
0.0.stdfloat= 1.0Standard deviation of the underlying Gaussian. Default
1.0.afloat= -2.0Lower truncation bound. Default
-2.0.bfloat= 2.0Upper truncation bound. Default
2.0.PRNG used for the inverse-CDF uniform quantile draw. Default
None.Returns
Tensortensor populated with truncated normal draws.
Notes
The conditional density is
where is the normalising constant.
Examples
>>> import lucid
>>> from lucid.nn.init import trunc_normal_
>>> w = lucid.empty(64, 32)
>>> _ = trunc_normal_(w, mean=0.0, std=0.02, a=-0.04, b=0.04)
>>> bool(((w >= -0.04) & (w <= 0.04)).all().item())
True