Lux.jl

WeightInitializers

This package is a light dependency providing common weight initialization schemes for deep learning models.

Index

API Reference

Main Functions

# WeightInitializers.glorot_normalFunction. ```julia 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.


# WeightInitializers.glorot_uniformFunction. ```julia 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, x]$, where `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.


# WeightInitializers.identity_initFunction. ```julia 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 `Vector` of zeros (useful for biases in layers where `input_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_size` for 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 by `gain` and optionally shifted by `shift`. **Examples** ```julia 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)) ```


# WeightInitializers.kaiming_normalFunction. ```julia 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.


# WeightInitializers.kaiming_uniformFunction. ```julia 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.


# WeightInitializers.sparse_initFunction. ```julia 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 applying `gain`. **Returns** * `AbstractArray{T}`: A sparsely initialized weight matrix of dimensions `dims` and type `T`. **Examples** ```julia 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.00880897 ``` **References** [^Martens2010] Martens, J, "Deep learning via Hessian-free optimization" *Proceedings of the 27th International Conference on International Conference on Machine Learning*. 2010.


# WeightInitializers.truncated_normalFunction. ```julia 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))`.


# WeightInitializers.orthogonalFunction. ```julia 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

# WeightInitializers.zeros16Function. ```julia zeros16([::AbstractRNG=_default_rng()], size...; kwargs...) -> AbstractArray{Float16, length(size)} ``` Return an `AbstractArray{Float16}` of the given `size` containing an AbstractArray of zeros.


# WeightInitializers.ones16Function. ```julia ones16([::AbstractRNG=_default_rng()], size...; kwargs...) -> AbstractArray{Float16, length(size)} ``` Return an `AbstractArray{Float16}` of the given `size` containing an AbstractArray of ones.


# WeightInitializers.rand16Function. ```julia 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.


# WeightInitializers.randn16Function. ```julia 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.


# WeightInitializers.zeros32Function. ```julia zeros32([::AbstractRNG=_default_rng()], size...; kwargs...) -> AbstractArray{Float32, length(size)} ``` Return an `AbstractArray{Float32}` of the given `size` containing an AbstractArray of zeros.


# WeightInitializers.ones32Function. ```julia ones32([::AbstractRNG=_default_rng()], size...; kwargs...) -> AbstractArray{Float32, length(size)} ``` Return an `AbstractArray{Float32}` of the given `size` containing an AbstractArray of ones.


# WeightInitializers.rand32Function. ```julia 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.


# WeightInitializers.randn32Function. ```julia 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.


# WeightInitializers.zeros64Function. ```julia zeros64([::AbstractRNG=_default_rng()], size...; kwargs...) -> AbstractArray{Float64, length(size)} ``` Return an `AbstractArray{Float64}` of the given `size` containing an AbstractArray of zeros.


# WeightInitializers.ones64Function. ```julia ones64([::AbstractRNG=_default_rng()], size...; kwargs...) -> AbstractArray{Float64, length(size)} ``` Return an `AbstractArray{Float64}` of the given `size` containing an AbstractArray of ones.


# WeightInitializers.rand64Function. ```julia 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.


# WeightInitializers.randn64Function. ```julia 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.


# WeightInitializers.zerosC16Function. ```julia zerosC16([::AbstractRNG=_default_rng()], size...; kwargs...) -> AbstractArray{ComplexF16, length(size)} ``` Return an `AbstractArray{ComplexF16}` of the given `size` containing an AbstractArray of zeros.


# WeightInitializers.onesC16Function. ```julia onesC16([::AbstractRNG=_default_rng()], size...; kwargs...) -> AbstractArray{ComplexF16, length(size)} ``` Return an `AbstractArray{ComplexF16}` of the given `size` containing an AbstractArray of ones.


# WeightInitializers.randC16Function. ```julia 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.


# WeightInitializers.randnC16Function. ```julia 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.


# WeightInitializers.zerosC32Function. ```julia zerosC32([::AbstractRNG=_default_rng()], size...; kwargs...) -> AbstractArray{ComplexF32, length(size)} ``` Return an `AbstractArray{ComplexF32}` of the given `size` containing an AbstractArray of zeros.


# WeightInitializers.onesC32Function. ```julia onesC32([::AbstractRNG=_default_rng()], size...; kwargs...) -> AbstractArray{ComplexF32, length(size)} ``` Return an `AbstractArray{ComplexF32}` of the given `size` containing an AbstractArray of ones.


# WeightInitializers.randC32Function. ```julia 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.


# WeightInitializers.randnC32Function. ```julia 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.


# WeightInitializers.zerosC64Function. ```julia zerosC64([::AbstractRNG=_default_rng()], size...; kwargs...) -> AbstractArray{ComplexF64, length(size)} ``` Return an `AbstractArray{ComplexF64}` of the given `size` containing an AbstractArray of zeros.


# WeightInitializers.onesC64Function. ```julia onesC64([::AbstractRNG=_default_rng()], size...; kwargs...) -> AbstractArray{ComplexF64, length(size)} ``` Return an `AbstractArray{ComplexF64}` of the given `size` containing an AbstractArray of ones.


# WeightInitializers.randC64Function. ```julia 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.


# WeightInitializers.randnC64Function. ```julia 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.