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 isXoshiro(1234).T: Type of the elements in the reservoir matrix. Default isFloat32.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 aboutsparsity * nnonzero entries for ann-by-nmatrix and never fewer than one, so reservoirs withn < 1 / sparsityget a single nonzero entry per column. Defaults to 0.1.return_sparse: flag for returning asparsematrix.truerequiresSparseArraysto be loaded. Default isfalse.
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)
trueSmall 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))
trueReturning 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