fn
calculate_gain
→floatcalculate_gain(nonlinearity: str, param: float | None = None)Return the recommended variance-preserving gain for an activation.
The gain is a multiplicative correction applied to the standard deviation of Xavier / Kaiming initialisation so the per-layer activation variance is preserved after the nonlinearity. Values follow the original recommendations in Glorot & Bengio (2010) and He et al. (2015).
Parameters
nonlinearitystrName of the activation function. One of
'linear',
'conv1d', 'conv2d', 'conv3d', 'sigmoid',
'tanh', 'relu', 'leaky_relu', 'selu'.paramfloat= NoneNegative slope of the rectifier — only consulted when
nonlinearity='leaky_relu'. Default 0.01.Returns
floatRecommended gain factor.
Raises
ValueErrorIf
nonlinearity is not in the supported set.Notes
Mapping table:
=============== ============================================
nonlinearity gain
=============== ============================================
linear
conv{1,2,3}d
sigmoid
tanh
relu
leaky_relu
selu
=============== ============================================
Examples
>>> from lucid.nn.init import calculate_gain
>>> calculate_gain('relu')
1.4142135623730951
>>> calculate_gain('leaky_relu', 0.2)
1.3867504905630728