WeightInitializers
This package is a light dependency providing common weight initialization schemes for deep learning models.
Index
WeightInitializers.glorot_normalWeightInitializers.glorot_uniformWeightInitializers.identity_initWeightInitializers.kaiming_normalWeightInitializers.kaiming_uniformWeightInitializers.ones16WeightInitializers.ones32WeightInitializers.ones64WeightInitializers.onesC16WeightInitializers.onesC32WeightInitializers.onesC64WeightInitializers.orthogonalWeightInitializers.rand16WeightInitializers.rand32WeightInitializers.rand64WeightInitializers.randC16WeightInitializers.randC32WeightInitializers.randC64WeightInitializers.randn16WeightInitializers.randn32WeightInitializers.randn64WeightInitializers.randnC16WeightInitializers.randnC32WeightInitializers.randnC64WeightInitializers.sparse_initWeightInitializers.truncated_normalWeightInitializers.zeros16WeightInitializers.zeros32WeightInitializers.zeros64WeightInitializers.zerosC16WeightInitializers.zerosC32WeightInitializers.zerosC64
API Reference
Main Functions
glorot_normal([::AbstractRNG=_default_rng()], [T=Float32], size...;
gain = 1) -> AbstractArray{T, length(size)}Return an AbstractArray{T} of the given size containing random numbers drawn from a normal distribution with standard deviation gain * sqrt(2 / (fan_in + fan_out)). This method is described in [1] and also known as Xavier initialization.
References
[1] Glorot, Xavier, and Yoshua Bengio. "Understanding the difficulty of training deep feedforward neural networks." Proceedings of the thirteenth international conference on artificial intelligence and statistics. 2010.
glorot_uniform([::AbstractRNG=_default_rng()], [T=Float32], size...;
gain = 1) -> AbstractArray{T, length(size)}Return an AbstractArray{T} of the given size containing random numbers drawn from a uniform distribution on the interval x = gain * sqrt(6 / (fan_in + fan_out)). This method is described in [1] and also known as Xavier initialization.
References
[1] Glorot, Xavier, and Yoshua Bengio. "Understanding the difficulty of training deep feedforward neural networks." Proceedings of the thirteenth international conference on artificial intelligence and statistics. 2010.
identity_init([::AbstractRNG=_default_rng()], [T=Float32], size...; gain::Number=1,
shift::Union{Integer, Tuple{Integer, Integer}}=0) -> AbstractArray{T}Constructs an array that aims to provide an identity mapping when used as parameters in most layers of a neural network. The identity mapping is scaled by the gain parameter.
Behavior
1D: Returns a
Vectorof zeros (useful for biases in layers whereinput_size == output_size).2D: Returns an identity matrix (useful for fully connected layers with equal input and output sizes).
More than 2D: Returns a tensor where the central slice along the last two dimensions is an identity matrix, and the rest are zeros (useful for convolutional layers, simulating an identity convolution).
Caveats
Not all layers will result in an identity mapping when using this initializer. Exceptions include recurrent and normalization layers.
Layers must have
input_size == output_sizefor a perfect identity mapping. In cases where this condition is not met, the function pads extra dimensions with zeros.For convolutional layers to achieve an identity mapping, kernel sizes must be odd, and appropriate padding must be applied to ensure the output feature maps are the same size as the input feature maps.
Arguments
rng::AbstractRNG: An optional random number generator, included for consistency with other initializers but ignored since the output is deterministic.T::Type{<:Number}: The numeric type of the array elements.size...: The dimensions of the array to be initialized.gain::Number=1: A scaling factor applied to the identity mapping.shift::Union{Integer, Tuple{Integer, Integer}}=0: An integer or a tuple specifying the circular shift applied to the output array.
Returns
AbstractArray{T}: An array initialized to represent an identity mapping, scaled bygainand optionally shifted byshift.
Examples
using Random
# Identity matrix for fully connected layer
identity_matrix = identity_init(MersenneTwister(123), Float32, 5, 5)
# Identity tensor for convolutional layer
identity_tensor = identity_init(MersenneTwister(123),
Float32, # Bias initialization
3,
3,
5, # Matrix multiplication
5;
gain=1.5,
shift=(1, 0))kaiming_normal([::AbstractRNG=_default_rng()], [T=Float32], size...;
gain = √T(2)) -> AbstractArray{T, length(size)}Return an AbstractArray{T} of the given size containing random numbers taken from a normal distribution standard deviation gain / sqrt(fan_in)
References
[1] He, Kaiming, et al. "Delving deep into rectifiers: Surpassing human-level performance on imagenet classification." Proceedings of the IEEE international conference on computer vision. 2015.
kaiming_uniform([::AbstractRNG=_default_rng()], [T=Float32], size...;
gain = √T(2)) -> AbstractArray{T, length(size)}Return an AbstractArray{T} of the given size containing random numbers drawn from a uniform distribution on the interval [-x, x], where x = gain * sqrt(3/fan_in).
References
[1] He, Kaiming, et al. "Delving deep into rectifiers: Surpassing human-level performance on imagenet classification." Proceedings of the IEEE international conference on computer vision. 2015.
sparse_init([::AbstractRNG=_default_rng()], [T=Float32], dims::Integer...;
sparsity::Number, std::Number=0.01) -> AbstractArray{T}Creates a sparsely initialized weight matrix with a specified proportion of zeroed elements, using random numbers drawn from a normal distribution for the non-zero elements. This method is introduced in [^Martens2010]. Note: The sparsity parameter controls the proportion of the matrix that will be zeroed. For example, a sparsity of 0.3 means that approximately 30% of the elements will be set to zero. The non-zero elements are distributed according to a normal distribution, scaled by the std parameter.
Arguments
rng::AbstractRNG: The random number generator to use.T::Type{<:Number}: The numeric type of the elements in the returned array.dims::Integer...: The dimensions of the weight matrix to be generated.sparsity::Number: The proportion of elements to be zeroed. Must be between 0 and 1.std::Number=0.01: The standard deviation of the normal distribution before applyinggain.
Returns
AbstractArray{T}: A sparsely initialized weight matrix of dimensionsdimsand typeT.
Examples
using Random
# Initialize a 5x5 sparsely initialized matrix with 30% sparsity
rng = MersenneTwister(123)
matrix = sparse_init(rng, Float32, 5, 5; sparsity=0.3, std=0.01)5×5 Matrix{Float64}:
0.0 0.00273815 0.00592403 0.0 0.0
0.00459416 -0.000754831 -0.00888936 -0.0077507 0.0
0.0 -0.00194229 0.0 0.0 -0.00468489
0.0114265 0.0 0.0 -0.00734886 0.00277726
-0.00396679 0.0 0.00327215 -0.0071741 -0.00880897References
[^Martens2010] Martens, J, "Deep learning via Hessian-free optimization" Proceedings of the 27th International Conference on International Conference on Machine Learning. 2010.
truncated_normal([::AbstractRNG=_default_rng()], [T=Float32], size...; mean = 0,
std = 1, lo = -2, hi = 2) -> AbstractArray{T, length(size)}Return an AbstractArray{T} of the given size where each element is drawn from a truncated normal distribution. The numbers are distributed like filter(x -> lo ≤ x ≤ hi, mean .+ std .* randn(100)).
orthogonal([::AbstractRNG=_default_rng()], [T=Float32], dims::Integer...;
gain = 1) -> AbstractArray{T, length(dims)}Return an AbstractArray{T} of the given dimensions (dims) which is a (semi) orthogonal matrix, as described in [^Saxe14]
The function constructs an orthogonal or semi-orthogonal matrix depending on the specified dimensions. For two dimensions, it returns a matrix where dims = (rows, cols). For more than two dimensions, it computes an orthogonal matrix of size prod(dims[1:(end - 1)]) by dims[end] before reshaping it to the original dimensions.
Cannot construct a vector, i.e., length(dims) == 1 is forbidden.
Arguments
rng::AbstractRNG: Random number generator.T::Type{<:Real}: The type of the elements in the array.dims::Integer...: The dimensions of the array.gain::Number: Scaling factor for the elements of the orthogonal matrix.
References
[^Saxe14] Saxe, McClelland, Ganguli. "Exact solutions to the nonlinear dynamics of learning in deep linear neural networks", ICLR 2014, https://arxiv.org/abs/1312.6120
Commonly Used Wrappers
zeros16([::AbstractRNG=_default_rng()], size...;
kwargs...) -> AbstractArray{Float16, length(size)}Return an AbstractArray{Float16} of the given size containing an AbstractArray of zeros.
ones16([::AbstractRNG=_default_rng()], size...;
kwargs...) -> AbstractArray{Float16, length(size)}Return an AbstractArray{Float16} of the given size containing an AbstractArray of ones.
rand16([::AbstractRNG=_default_rng()], size...;
kwargs...) -> AbstractArray{Float16, length(size)}Return an AbstractArray{Float16} of the given size containing random numbers from a uniform distribution.
randn16([::AbstractRNG=_default_rng()], size...;
kwargs...) -> AbstractArray{Float16, length(size)}Return an AbstractArray{Float16} of the given size containing random numbers from a standard normal distribution.
zeros32([::AbstractRNG=_default_rng()], size...;
kwargs...) -> AbstractArray{Float32, length(size)}Return an AbstractArray{Float32} of the given size containing an AbstractArray of zeros.
ones32([::AbstractRNG=_default_rng()], size...;
kwargs...) -> AbstractArray{Float32, length(size)}Return an AbstractArray{Float32} of the given size containing an AbstractArray of ones.
rand32([::AbstractRNG=_default_rng()], size...;
kwargs...) -> AbstractArray{Float32, length(size)}Return an AbstractArray{Float32} of the given size containing random numbers from a uniform distribution.
randn32([::AbstractRNG=_default_rng()], size...;
kwargs...) -> AbstractArray{Float32, length(size)}Return an AbstractArray{Float32} of the given size containing random numbers from a standard normal distribution.
zeros64([::AbstractRNG=_default_rng()], size...;
kwargs...) -> AbstractArray{Float64, length(size)}Return an AbstractArray{Float64} of the given size containing an AbstractArray of zeros.
ones64([::AbstractRNG=_default_rng()], size...;
kwargs...) -> AbstractArray{Float64, length(size)}Return an AbstractArray{Float64} of the given size containing an AbstractArray of ones.
rand64([::AbstractRNG=_default_rng()], size...;
kwargs...) -> AbstractArray{Float64, length(size)}Return an AbstractArray{Float64} of the given size containing random numbers from a uniform distribution.
randn64([::AbstractRNG=_default_rng()], size...;
kwargs...) -> AbstractArray{Float64, length(size)}Return an AbstractArray{Float64} of the given size containing random numbers from a standard normal distribution.
zerosC16([::AbstractRNG=_default_rng()], size...;
kwargs...) -> AbstractArray{ComplexF16, length(size)}Return an AbstractArray{ComplexF16} of the given size containing an AbstractArray of zeros.
onesC16([::AbstractRNG=_default_rng()], size...;
kwargs...) -> AbstractArray{ComplexF16, length(size)}Return an AbstractArray{ComplexF16} of the given size containing an AbstractArray of ones.
randC16([::AbstractRNG=_default_rng()], size...;
kwargs...) -> AbstractArray{ComplexF16, length(size)}Return an AbstractArray{ComplexF16} of the given size containing random numbers from a uniform distribution.
randnC16([::AbstractRNG=_default_rng()], size...;
kwargs...) -> AbstractArray{ComplexF16, length(size)}Return an AbstractArray{ComplexF16} of the given size containing random numbers from a standard normal distribution.
zerosC32([::AbstractRNG=_default_rng()], size...;
kwargs...) -> AbstractArray{ComplexF32, length(size)}Return an AbstractArray{ComplexF32} of the given size containing an AbstractArray of zeros.
onesC32([::AbstractRNG=_default_rng()], size...;
kwargs...) -> AbstractArray{ComplexF32, length(size)}Return an AbstractArray{ComplexF32} of the given size containing an AbstractArray of ones.
randC32([::AbstractRNG=_default_rng()], size...;
kwargs...) -> AbstractArray{ComplexF32, length(size)}Return an AbstractArray{ComplexF32} of the given size containing random numbers from a uniform distribution.
randnC32([::AbstractRNG=_default_rng()], size...;
kwargs...) -> AbstractArray{ComplexF32, length(size)}Return an AbstractArray{ComplexF32} of the given size containing random numbers from a standard normal distribution.
zerosC64([::AbstractRNG=_default_rng()], size...;
kwargs...) -> AbstractArray{ComplexF64, length(size)}Return an AbstractArray{ComplexF64} of the given size containing an AbstractArray of zeros.
onesC64([::AbstractRNG=_default_rng()], size...;
kwargs...) -> AbstractArray{ComplexF64, length(size)}Return an AbstractArray{ComplexF64} of the given size containing an AbstractArray of ones.
randC64([::AbstractRNG=_default_rng()], size...;
kwargs...) -> AbstractArray{ComplexF64, length(size)}Return an AbstractArray{ComplexF64} of the given size containing random numbers from a uniform distribution.
randnC64([::AbstractRNG=_default_rng()], size...;
kwargs...) -> AbstractArray{ComplexF64, length(size)}Return an AbstractArray{ComplexF64} of the given size containing random numbers from a standard normal distribution.