braindecode.functional.init_weights#
- braindecode.functional.init_weights_(module, weights=None, norms=(<class 'torch.nn.modules.normalization.LayerNorm'>, ), init_parametrized_original=False)[source]#
Initialize one module by layer type; apply to a model with
model.apply.The first entry of
weightswhose type matchesmodulegives its rule, as in anif/elifchain; modules of a type innormsget a unit weight and a zero bias; nothing else is touched. The default is the timm/ViT scheme (truncated-normalnn.Linearweights with std 0.02, unit/zeronn.LayerNorm). Models pass their own rules to reproduce their reference initialization exactly.- Parameters:
module (torch.nn.Module)
weights (dict or None) –
{layer type or tuple of types: rule}. A rule is a function applied to theweight(thebiasis then set to zero), or a pair(weight_fn, bias_fn)where either may beNoneto leave that tensor as it is. For a layer without aweightattribute (e.g.nn.LSTM) the functions apply to every parameter whose name contains"weight"/"bias".Noneuses{nn.Linear: partial(nn.init.trunc_normal_, std=0.02)}.norms (tuple of type) – Normalization layer types set to weight 1 and bias 0 (missing affine parameters are skipped).
()leaves normalization layers alone.init_parametrized_original (bool) – For a layer whose
weightis parametrized (e.g. max-norm), initialize the unconstrainedparametrizations.weight.originalinstead of the computedweight.
Examples
>>> from functools import partial >>> from torch import nn >>> model = nn.Sequential(nn.Linear(4, 4), nn.LayerNorm(4)) >>> _ = model.apply(init_weights_) # timm/ViT default >>> kaiming = partial(nn.init.kaiming_normal_, mode="fan_out") >>> rule = {nn.Linear: (kaiming, None)} # keep the default bias >>> _ = model.apply(partial(init_weights_, weights=rule, norms=()))