Initialise tensor in-place with Xavier (Glorot) uniform initialisation.
Fills the tensor with values drawn uniformly from
where the limit a is chosen so that the variance of activations
is preserved across a stack of linear / mildly-nonlinear layers.
Introduced in Glorot & Bengio (2010), this scheme is well-suited to
tanh and sigmoid networks; for ReLU networks prefer
kaiming_uniform_, which corrects for the half-truncation of
negative pre-activations.
Parameters
tensorTensorTensor to initialise in place; must have at least 2 dimensions
so
fan_in and fan_out can be computed.gainfloat= 1.0Multiplicative gain factor — typically the value returned by
calculate_gain for the downstream nonlinearity.
Default 1.0.Returns
Tensortensor (mutated) for chaining.
Notes
Let and be the fan-in and
fan-out of tensor (see _calculate_fan_in_and_fan_out).
The uniform range is
which yields variance
This is the value that approximately preserves the variance of activations forwards and gradients backwards in a linear layer.
Examples
>>> import lucid
>>> from lucid.nn.init import xavier_uniform_, calculate_gain
>>> w = lucid.empty(64, 32)
>>> _ = xavier_uniform_(w, gain=calculate_gain('tanh'))
>>> bound = calculate_gain('tanh') * (6 / (32 + 64)) ** 0.5
>>> bool((w.abs() <= bound).all().item())
True