rand_sparse

ReservoirComputing.rand_sparse — Function
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.

Every column keeps at least one nonzero entry, whatever the size and sparsity, so the matrix is never all zero and can be scaled to radius.

Arguments

  • rng: Random number generator. Default is Xoshiro(1234).
  • 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: Fraction of nonzero entries in each column of the reservoir matrix. Each column has about sparsity * n nonzero entries for an n-by-n matrix and never fewer than one, so reservoirs with n < 1 / sparsity get a single nonzero entry per column. 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

Small reservoirs keep one nonzero entry per column:

julia> small_matrix = rand_sparse(MersenneTwister(123), 5, 5);

julia> all(==(1), count(!iszero, small_matrix; dims = 1))
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