torch.nn.init
- torch.nn.init.calculate_gain(nonlinearity,
Return the recommended gain value for the given nonlinearity function.
The values are as follows:
nonlinearity | gain |
|---|---|
Linear / Identity | |
Conv{1,2,3}D | |
Sigmoid | |
Tanh | |
ReLU | |
Leaky Relu | |
SELU |
>>> gain = nn.init.calculate_gain(
... "leaky_relu", 0.2
... ) # leaky_relu with negative_slope=0.2
Fill the input Tensor with values drawn from the uniform distribution.
.
>>> w = torch.empty(3, 5)
>>> nn.init.uniform_(w)
Fill the input Tensor with values drawn from the normal distribution.
.
>>> w = torch.empty(3, 5)
>>> nn.init.normal_(w)
Fill the 2-dimensional input Tensor with the identity matrix.
Preserves the identity of the inputs in Linear layers, where as many inputs are preserved as possible.
>>> w = torch.empty(3, 5)
>>> nn.init.eye_(w)
Fill the {3, 4, 5}-dimensional input Tensor with the Dirac delta function.
Preserves the identity of the inputs in Convolutional layers, where as many input channels are preserved as possible. In case of groups>1, each group of channels preserves identity
>>> w = torch.empty(3, 16, 5, 5)
>>> nn.init.dirac_(w)
>>> w = torch.empty(3, 24, 5,
Fill the input Tensor with values using a Xavier uniform distribution.
The method is described in Understanding the difficulty of training deep feedforward neural networks - Glorot, X. & Bengio, Y. (2010). The resulting tensor will have values sampled from where
Also known as Glorot initialization.
>>> w = torch.empty(3, 5)
>>> nn.init.xavier_uniform_(w, gain=nn.init.calculate_gain("relu"))
Fill the input Tensor with values using a Xavier normal distribution.
The method is described in Understanding the difficulty of training deep feedforward neural networks - Glorot, X. & Bengio, Y. (2010). The resulting tensor will have values sampled from where
Also known as Glorot initialization.
>>> w = torch.empty(3, 5)
>>> nn.init.xavier_normal_(w)
Fill the input Tensor with values using a Kaiming uniform distribution.
The method is described in Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification - He, K. et al. (2015). The resulting tensor will have values sampled from where
Also known as He initialization.
tensor (Tensor) – an n-dimensional torch.Tensor
a (float) – the negative slope of the rectifier used after this layer (only
used with 'leaky_relu')
mode (Literal['fan_in', 'fan_out']) – either 'fan_in' (default) or 'fan_out'. Choosing 'fan_in'
preserves the magnitude of the variance of the weights in the
forward pass. Choosing preserves the magnitudes in the
backwards pass.
>>> w = torch.empty(3, 5)
>>> nn.init.kaiming_uniform_(w, mode="fan_in", nonlinearity="relu")
Fill the input Tensor with values using a Kaiming normal distribution.
The method is described in Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification - He, K. et al. (2015). The resulting tensor will have values sampled from where
Also known as He initialization.
tensor (Tensor) – an n-dimensional torch.Tensor
a (float) – the negative slope of the rectifier used after this layer (only
used with 'leaky_relu')
mode (Literal['fan_in', 'fan_out']) – either 'fan_in' (default) or 'fan_out'. Choosing 'fan_in'
preserves the magnitude of the variance of the weights in the
forward pass. Choosing preserves the magnitudes in the
backwards pass.
>>> w = torch.empty(3, 5)
>>> nn.init.kaiming_normal_(w, mode="fan_out", nonlinearity="relu")
Fill the input Tensor with values drawn from a truncated normal distribution.
The values are effectively drawn from the normal distribution with values outside redrawn until they are within the bounds. The method used for generating the random values works best when .
For reduced-precision types (torch.float16 and torch.bfloat16),
sampling quality depends on the underlying normal_() and uniform_()
implementations which operate at higher internal precision to avoid
quantization artifacts.
tensor (Tensor) – an n-dimensional torch.Tensor
mean (float) – the mean of the normal distribution
std (float) – the standard deviation of the normal distribution
a (float) – the minimum cutoff value
b (float) – the maximum cutoff value
generator () – the torch Generator to sample from (default: None)
>>> w = torch.empty(3, 5)
>>> nn.init.trunc_normal_(w)
Fill the input Tensor with a (semi) orthogonal matrix.
Described in Exact solutions to the nonlinear dynamics of learning in deep linear neural networks - Saxe, A. et al. (2013). The input tensor must have at least 2 dimensions, and for tensors with more than 2 dimensions the trailing dimensions are flattened.
>>> w = torch.empty(3, 5)
>>> nn.init.orthogonal_(w)
Fill the 2D input Tensor as a sparse matrix.
The non-zero elements will be drawn from the normal distribution , as described in Deep learning via Hessian-free optimization - Martens, J. (2010).
tensor (Tensor) – an n-dimensional torch.Tensor
sparsity (float) – The fraction of elements in each column to be set to zero
std (float) – the standard deviation of the normal distribution used to generate the non-zero values
generator (Generator | None) – the torch Generator to sample from (default: None)
>>> w = torch.empty(3, 5)
>>> nn.init.sparse_(w, sparsity=0.1)
'fan_out'nonlinearity (Literal['linear', 'conv1d', 'conv2d', 'conv3d', 'conv_transpose1d', 'conv_transpose2d', 'conv_transpose3d', 'sigmoid', 'tanh', 'relu', 'leaky_relu', 'selu']) – the non-linear function (nn.functional name),
recommended to use only with 'relu' or 'leaky_relu' (default).
generator (Generator | None) – the torch Generator to sample from (default: None)
'fan_out'nonlinearity (Literal['linear', 'conv1d', 'conv2d', 'conv3d', 'conv_transpose1d', 'conv_transpose2d', 'conv_transpose3d', 'sigmoid', 'tanh', 'relu', 'leaky_relu', 'selu']) – the non-linear function (nn.functional name),
recommended to use only with 'relu' or 'leaky_relu' (default).
generator (Generator | None) – the torch Generator to sample from (default: None)