rand_sparse

ReservoirComputing.rand_sparseFunction
rand_sparse([rng], [T], dims...;
    radius=1.0, sparsity=0.1, std=1.0, return_sparse=false)

Create and return a random sparse reservoir matrix. The matrix will be of size specified by dims, with specified sparsity and scaled spectral radius according to radius.

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 reservoir matrix.

Keyword arguments

  • radius: The desired spectral radius of the reservoir. Defaults to 1.0.
  • sparsity: The sparsity level of the reservoir matrix, controlling the fraction of zero elements. Defaults to 0.1.
  • return_sparse: flag for returning a sparse matrix. true requires SparseArrays to be loaded. Default is false.

Examples

Changing the sparsity:

julia> rng = MersenneTwister(123);

julia> sparse_matrix = rand_sparse(rng, 5, 5; sparsity = 0.2);

julia> dense_matrix = rand_sparse(rng, 5, 5; sparsity = 0.8);

julia> size(sparse_matrix) == size(dense_matrix) == (5, 5) &&
       count(!iszero, sparse_matrix) < count(!iszero, dense_matrix)
true

Returning a sparse matrix:

julia> using SparseArrays

julia> res_matrix = rand_sparse(MersenneTwister(123), 5, 5;
           sparsity = 0.4, return_sparse = true);

julia> res_matrix isa SparseMatrixCSC{Float32} && size(res_matrix) == (5, 5)
true
source