xavier_normal_(tensor: Tensor, gain: float = 1.0, generator: _C_engine.Generator | None = None)Initialise tensor in-place with Xavier (Glorot) normal initialisation.
Gaussian counterpart of xavier_uniform_. Draws each entry
from with a standard deviation
chosen to preserve activation variance through a stack of linear /
mildly-nonlinear layers, as proposed in Glorot & Bengio (2010).
Prefer this for tanh / sigmoid networks; use
kaiming_normal_ for ReLU networks.
Parameters
Returns
TensorThe Xavier-normal-initialised tensor.
Notes
The standard deviation is
which yields variance
Examples
>>> import lucid
>>> from lucid.nn.init import xavier_normal_, calculate_gain
>>> w = lucid.empty(64, 32)
>>> lucid.manual_seed(0)
>>> _ = xavier_normal_(w, gain=calculate_gain('tanh'))
>>> std = calculate_gain('tanh') * (2 / (32 + 64)) ** 0.5
>>> abs(w.std().item() - std) < 0.1 * std
True