weighted_init

ReservoirComputing.weighted_initFunction
weighted_init([rng], [T], dims...;
    scaling=0.1, return_sparse=false)

Create and return a weighted input layer matrix. In this matrix, each of the input signals in_size connects to the reservoir nodes res_size/in_size. The nonzero entries are distributed uniformly within a range defined by scaling (Lu et al., 2017).

Please note that this initializer computes its own reservoir size! If the computed reservoir size is different than the provided one it will raise a warning.

Arguments

  • rng: Random number generator. Default is Utils.default_rng() from WeightInitializers.
  • T: Type of the elements in the reservoir matrix. Default is Float32.
  • dims: Dimensions of the matrix. Should follow res_size x in_size.

Keyword arguments

  • scaling: A scaling factor to define the range of the uniform distribution. The factor can be passed in three different ways:

    • A single number. In this case, the matrix elements will be randomly chosen from the range [-scaling, scaling]. Default option, with a the scaling value set to 0.1.

    • A tuple (lower, upper). The values define the range of the distribution. the matrix elements will be randomly created and scaled the range [lower, upper].

    • A vector of length = in_size. In this case, the columns will be scaled individually by the entries of the vector. The entries can be numbers or tuples, which will mirror the behavior described above.

    • return_sparse: flag for returning a sparse matrix. Default is false.

Examples

Standard call with scaling provided by a scalar:

julia> res_input = weighted_init(9, 3; scaling = 0.1)
9×3 Matrix{Float32}:
  0.0452399   0.0         0.0
 -0.0348047   0.0         0.0
 -0.0386004   0.0         0.0
  0.0         0.0577838   0.0
  0.0        -0.0562827   0.0
  0.0         0.0441522   0.0
  0.0         0.0         0.00627948
  0.0         0.0        -0.0293777
  0.0         0.0        -0.0352914

Scaling with a tuple, providing lower and upper bound of the uniform distribution from which the weights will be sampled:

julia> res_input = weighted_init(9, 3; scaling = (0.1, 0.5))
9×3 Matrix{Float32}:
 0.39048   0.0       0.0
 0.230391  0.0       0.0
 0.222799  0.0       0.0
 0.0       0.415568  0.0
 0.0       0.187435  0.0
 0.0       0.388304  0.0
 0.0       0.0       0.312559
 0.0       0.0       0.241245
 0.0       0.0       0.229417

Scaling with a vector of scalars, where each provides the upper bound and its negative provides the lower bound. Each column is scaled in order: first element provides bounds for the first column, and so on:

julia> res_input = weighted_init(9, 3; scaling = [0.1, 0.5, 0.9])
9×3 Matrix{Float32}:
  0.0452399   0.0        0.0
 -0.0348047   0.0        0.0
 -0.0386004   0.0        0.0
  0.0         0.288919   0.0
  0.0        -0.281413   0.0
  0.0         0.220761   0.0
  0.0         0.0        0.0565153
  0.0         0.0       -0.264399
  0.0         0.0       -0.317622

Scaling with a vector of tuples, each providing both upper and lower bound. Each column is scaled in order: first element provides bounds for the first column, and so on:

julia> res_input = weighted_init(9, 3; scaling = [(0.1, 0.2), (-0.2, -0.1), (0.3, 0.5)])
9×3 Matrix{Float32}:
 0.17262    0.0       0.0
 0.132598   0.0       0.0
 0.1307     0.0       0.0
 0.0       -0.121108  0.0
 0.0       -0.178141  0.0
 0.0       -0.127924  0.0
 0.0        0.0       0.40628
 0.0        0.0       0.370622
 0.0        0.0       0.364709

Example of matrix size change:

julia> res_input = weighted_init(8, 3)
┌ Warning: Reservoir size has changed!
│
│     Computed reservoir size (6) does not equal the provided reservoir size (8).
│
│     Using computed value (6). Make sure to modify the reservoir initializer accordingly.
│
└ @ ReservoirComputing ~/.julia/dev/ReservoirComputing/src/inits/inits_components.jl:20
6×3 Matrix{Float32}:
  0.0452399   0.0          0.0
 -0.0348047   0.0          0.0
  0.0        -0.0386004    0.0
  0.0         0.00981022   0.0
  0.0         0.0          0.0577838
  0.0         0.0         -0.0562827

Return sparse:

julia> using SparseArrays

julia> res_input = weighted_init(9, 3; return_sparse = true)
9×3 SparseMatrixCSC{Float32, Int64} with 9 stored entries:
  0.0452399    ⋅           ⋅
 -0.0348047    ⋅           ⋅
 -0.0386004    ⋅           ⋅
   ⋅          0.0577838    ⋅
   ⋅         -0.0562827    ⋅
   ⋅          0.0441522    ⋅
   ⋅           ⋅          0.00627948
   ⋅           ⋅         -0.0293777
   ⋅           ⋅         -0.0352914
source

References