Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/inits/inits_components.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ function scale_radius!(reservoir_matrix::AbstractMatrix, radius::Nothing)
return reservoir_matrix
end

__expand_weight(::Type{T}, weight::Number, n::Integer) where {T} = fill(T(weight), n)
function __expand_weight(::Type{T}, weight::AbstractVector, n::Integer) where {T}
length(weight) == n || throw(
DimensionMismatch("weight vector must have length $n, got length $(length(weight))")
)
return T.(weight)
end

function scale_radius!(reservoir_matrix::AbstractMatrix, radius::Real)
return scale_radius!(reservoir_matrix, float(radius))
end

function __validate_add_jumps_arguments(reservoir_matrix, jump_size, start)
N = size(reservoir_matrix, 1)
N == size(reservoir_matrix, 2) || throw(
Expand Down Expand Up @@ -812,6 +824,10 @@ function permute_matrix!(
return reservoir_matrix
end

function permute_matrix!(reservoir_matrix::AbstractMatrix, args...)
return permute_matrix!(Utils.default_rng(), reservoir_matrix, args...)
end

function create_permutation_matrix(perm_array::AbstractVector{Int}, reservoir_matrix)
num_perm = length(perm_array)
T = eltype(reservoir_matrix)
Expand Down
22 changes: 15 additions & 7 deletions src/inits/inits_input.jl
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,9 @@ applying:
## Keyword arguments

- `amplitude`: Scaling factor used to initialize the first row.
This parameter adjusts the amplitude of the sine function. Default value is one.
This parameter adjusts the amplitude of the sine function. It must satisfy
`abs(amplitude) <= 1`, since the mapping applies the arccosine to the first row.
Default value is one.
- `sine_divisor`: Divisor applied in the sine function's phase. Default value is one.
- `chebyshev_parameter`: Control parameter for the Chebyshev mapping in
subsequent rows. This parameter influences the distribution of the
Expand All @@ -516,11 +518,17 @@ true
"""
function chebyshev_mapping(
rng::AbstractRNG, ::Type{T}, dims::Integer...;
amplitude::AbstractFloat = one(T), sine_divisor::AbstractFloat = one(T),
chebyshev_parameter::AbstractFloat = one(T),
amplitude::Real = one(T), sine_divisor::Real = one(T),
chebyshev_parameter::Real = one(T),
return_sparse::Bool = false
) where {T <: Number}
throw_sparse_error(return_sparse)
abs(amplitude) <= 1 || throw(
ArgumentError(
"amplitude must satisfy abs(amplitude) <= 1 for the arccosine in the " *
"Chebyshev mapping to be defined, got $amplitude"
)
)
input_matrix = DeviceAgnostic.zeros(rng, T, dims...)
n_rows, n_cols = dims[1], dims[2]

Expand Down Expand Up @@ -594,8 +602,8 @@ true
"""
function logistic_mapping(
rng::AbstractRNG, ::Type{T}, dims::Integer...;
amplitude::AbstractFloat = 0.3, sine_divisor::AbstractFloat = 5.9,
logistic_parameter::AbstractFloat = 3.7,
amplitude::Real = 0.3, sine_divisor::Real = 5.9,
logistic_parameter::Real = 3.7,
return_sparse::Bool = false
) where {T <: Number}
throw_sparse_error(return_sparse)
Expand Down Expand Up @@ -684,8 +692,8 @@ true
"""
function modified_lm(
rng::AbstractRNG, ::Type{T}, dims::Integer...;
factor::Integer, amplitude::AbstractFloat = 0.3,
sine_divisor::AbstractFloat = 5.9, logistic_parameter::AbstractFloat = 2.35,
factor::Integer, amplitude::Real = 0.3,
sine_divisor::Real = 5.9, logistic_parameter::Real = 2.35,
return_sparse::Bool = false
) where {T <: Number}
throw_sparse_error(return_sparse)
Expand Down
79 changes: 43 additions & 36 deletions src/inits/inits_reservoir.jl
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,11 @@ Returns an initializer to build a sparse reservoir matrix with the given

- `max_value`: The maximum absolute value of elements in the matrix.
Default is 1.0
- `sparsity`: The desired sparsity level of the reservoir matrix.
Default is 0.1
- `sparsity`: Threshold on the ratio of nonzero to zero entries of the matrix.
Random rotations are applied to the diagonal matrix until
`count(!iszero, W) / count(iszero, W) > sparsity`, so the fraction of nonzero
entries ends up slightly above `sparsity / (1 + sparsity)` (for example, about
9% for `0.1`, and about 37.5% for `0.6`). Default is 0.1
- `sorted`: A boolean indicating whether to sort the singular values before
creating the diagonal matrix. Default is `true`.
- `reverse_sort`: A boolean indicating whether to reverse the sorted
Expand Down Expand Up @@ -405,7 +408,7 @@ julia> size(res_matrix)
"""
function chaotic_init(
rng::AbstractRNG, ::Type{T}, dims::Integer...;
extra_edge_probability::AbstractFloat = T(0.1f0), radius::AbstractFloat = one(T),
extra_edge_probability::Real = T(0.1f0), radius::Real = one(T),
return_sparse::Bool = false
) where {T <: Number}
throw_sparse_error(return_sparse)
Expand Down Expand Up @@ -440,7 +443,7 @@ function chaotic_init(
reservoir_matrix = random_weight_matrix .* adjacency_matrix
current_spectral_radius = maximum(abs, eigvals(reservoir_matrix))
if current_spectral_radius != 0
reservoir_matrix .*= radius / current_spectral_radius
reservoir_matrix .*= float(radius) / current_spectral_radius
end
check_inf_nan(reservoir_matrix)

Expand All @@ -449,7 +452,7 @@ end

function digital_chaotic_adjacency(
rng::AbstractRNG, bit_precision::Integer;
extra_edge_probability::AbstractFloat = 0.1
extra_edge_probability::Real = 0.1
)
matrix_order = 2^(2 * bit_precision)
adjacency_matrix = DeviceAgnostic.zeros(rng, Int, matrix_order, matrix_order)
Expand All @@ -469,8 +472,8 @@ end

"""
low_connectivity([rng], [T], dims...;
connected=false, in_degree = 1, radius = 1.0,
cut_cycle = false, radius=nothing, return_sparse = false)
connected=false, in_degree = 1, cut_cycle = false,
radius=nothing, return_sparse = false)

Construct an internal reservoir connectivity matrix with low connectivity.

Expand All @@ -493,8 +496,6 @@ otherwise, it generates a random connectivity pattern.
Default is `false`.
- `in_degree`: The number of incoming connections per node.
Must not exceed the number of nodes. Default is 1.
- `radius`: The desired spectral radius of the reservoir.
Defaults to 1.0.
- `cut_cycle`: If `true`, removes one edge from the cycle to cut it.
Default is `false`.
- `radius`: The desired spectral radius of the reservoir.
Expand All @@ -516,7 +517,7 @@ true
function low_connectivity(
rng::AbstractRNG, ::Type{T}, dims::Integer...;
return_sparse::Bool = false, connected::Bool = false,
in_degree::Integer = 1, radius::Union{AbstractFloat, Nothing} = nothing,
in_degree::Integer = 1, radius::Union{Real, Nothing} = nothing,
kwargs...
) where {T <: Number}
check_res_size(dims...)
Expand Down Expand Up @@ -545,8 +546,7 @@ end

function build_cycle(
::Val{false}, rng::AbstractRNG, ::Type{T}, res_size::Int;
in_degree::Integer = 1, radius::Number = T(1.0f0),
cut_cycle::Bool = false
in_degree::Integer = 1, cut_cycle::Bool = false
) where {T <: Number}
reservoir_matrix = DeviceAgnostic.zeros(rng, T, res_size, res_size)
for idx in 1:res_size
Expand All @@ -555,13 +555,12 @@ function build_cycle(
reservoir_matrix[idx, jdx] = T(randn(rng))
end
end
reservoir_matrix = scale_radius!(reservoir_matrix, T(radius))
return reservoir_matrix
end

function build_cycle(
::Val{true}, rng::AbstractRNG, ::Type{T}, res_size::Int;
in_degree::Integer = 1, radius::Number = T(1.0f0), cut_cycle::Bool = false
in_degree::Integer = 1, cut_cycle::Bool = false
) where {
T <:
Number,
Expand All @@ -572,7 +571,6 @@ function build_cycle(
reservoir_matrix[perm[idx], perm[idx + 1]] = T(randn(rng))
end
reservoir_matrix[perm[res_size], perm[1]] = T(randn(rng))
reservoir_matrix = scale_radius!(reservoir_matrix, T(radius))
if cut_cycle
cut_cycle_edge!(reservoir_matrix, rng)
end
Expand Down Expand Up @@ -713,7 +711,7 @@ julia> res_matrix = delay_line(5, 5; return_sparse=true)
function delay_line(
rng::AbstractRNG, ::Type{T}, dims::Integer...;
delay_weight::Union{Number, AbstractVector} = T(0.1f0), delay_shift::Integer = 1,
return_sparse::Bool = false, radius::Union{AbstractFloat, Nothing} = nothing,
return_sparse::Bool = false, radius::Union{Real, Nothing} = nothing,
kwargs...
) where {T <: Number}
throw_sparse_error(return_sparse)
Expand Down Expand Up @@ -873,7 +871,7 @@ function delayline_backward(
delay_weight::Union{Number, AbstractVector} = T(0.1f0),
fb_weight::Union{Number, AbstractVector} = T(0.1f0), delay_shift::Integer = 1,
fb_shift::Integer = 1, return_sparse::Bool = false,
radius::Union{AbstractFloat, Nothing} = nothing,
radius::Union{Real, Nothing} = nothing,
delay_kwargs::NamedTuple = NamedTuple(),
fb_kwargs::NamedTuple = NamedTuple()
) where {T <: Number}
Expand Down Expand Up @@ -1031,7 +1029,7 @@ function cycle_jumps(
cycle_weight::Union{Number, AbstractVector} = T(0.1f0),
jump_weight::Union{Number, AbstractVector} = T(0.1f0),
jump_size::Integer = 3, return_sparse::Bool = false,
radius::Union{AbstractFloat, Nothing} = nothing,
radius::Union{Real, Nothing} = nothing,
cycle_kwargs::NamedTuple = NamedTuple(),
jump_kwargs::NamedTuple = NamedTuple()
) where {T <: Number}
Expand Down Expand Up @@ -1151,7 +1149,7 @@ julia> res_matrix = simple_cycle(5, 5; return_sparse=true)
function simple_cycle(
rng::AbstractRNG, ::Type{T}, dims::Integer...;
cycle_weight::Union{Number, AbstractVector} = T(0.1f0),
return_sparse::Bool = false, radius::Union{AbstractFloat, Nothing} = nothing,
return_sparse::Bool = false, radius::Union{Real, Nothing} = nothing,
kwargs...
) where {T <: Number}
throw_sparse_error(return_sparse)
Expand Down Expand Up @@ -1190,8 +1188,10 @@ W_{i,j} =
## Keyword arguments

- `cycle_weight`: Weight of the upper cycle connections in the reservoir matrix.
This can be provided as a single value or as a vector of length `res_size`.
Default is 0.1.
- `second_cycle_weight`: Weight of the lower cycle connections in the reservoir matrix.
This can be provided as a single value or as a vector of length `res_size`.
Default is 0.1.
- `radius`: The desired spectral radius of the reservoir.
If `nothing` is passed, no scaling takes place.
Expand Down Expand Up @@ -1230,22 +1230,25 @@ function double_cycle(
rng::AbstractRNG, ::Type{T}, dims::Integer...;
cycle_weight::Union{Number, AbstractVector} = T(0.1f0),
second_cycle_weight::Union{Number, AbstractVector} = T(0.1f0),
radius::Union{AbstractFloat, Nothing} = nothing,
radius::Union{Real, Nothing} = nothing,
return_sparse::Bool = false
) where {T <: Number}
throw_sparse_error(return_sparse)
check_res_size(dims...)
reservoir_matrix = DeviceAgnostic.zeros(rng, T, dims...)
res_size = size(reservoir_matrix, 1)
first_weights = __expand_weight(T, cycle_weight, res_size)
second_weights = __expand_weight(T, second_cycle_weight, res_size)

for uidx in first(axes(reservoir_matrix, 1)):(last(axes(reservoir_matrix, 1)) - 1)
reservoir_matrix[uidx + 1, uidx] = T.(cycle_weight)
reservoir_matrix[uidx + 1, uidx] = first_weights[uidx]
end
for lidx in (first(axes(reservoir_matrix, 1)) + 1):last(axes(reservoir_matrix, 1))
reservoir_matrix[lidx - 1, lidx] = T.(second_cycle_weight)
reservoir_matrix[lidx - 1, lidx] = second_weights[lidx - 1]
end

reservoir_matrix[1, dims[1]] = T.(second_cycle_weight)
reservoir_matrix[dims[1], 1] = T.(cycle_weight)
reservoir_matrix[1, dims[1]] = second_weights[end]
reservoir_matrix[dims[1], 1] = first_weights[end]
scale_radius!(reservoir_matrix, radius)
return return_init_as(Val(return_sparse), reservoir_matrix)
end
Expand Down Expand Up @@ -1371,7 +1374,7 @@ function true_doublecycle(
rng::AbstractRNG, ::Type{T}, dims::Integer...;
cycle_weight::Union{Number, AbstractVector} = T(0.1f0),
second_cycle_weight::Union{Number, AbstractVector} = T(0.1f0),
return_sparse::Bool = false, radius::Union{AbstractFloat, Nothing} = nothing,
return_sparse::Bool = false, radius::Union{Real, Nothing} = nothing,
cycle_kwargs::NamedTuple = NamedTuple(),
second_cycle_kwargs::NamedTuple = NamedTuple()
) where {T <: Number}
Expand Down Expand Up @@ -1517,7 +1520,7 @@ function selfloop_cycle(
rng::AbstractRNG, ::Type{T}, dims::Integer...;
cycle_weight::Union{Number, AbstractVector} = T(0.1f0),
selfloop_weight::Union{Number, AbstractVector} = T(0.1f0),
radius::Union{AbstractFloat, Nothing} = nothing,
radius::Union{Real, Nothing} = nothing,
return_sparse::Bool = false, selfloop_kwargs::NamedTuple = NamedTuple(),
cycle_kwargs::NamedTuple = NamedTuple()
) where {T <: Number}
Expand Down Expand Up @@ -1566,8 +1569,10 @@ W_{i,j} =
you want to populate.
Default is 0.1.
- `selfloop_weight`: Weight of the self loops in the reservoir matrix.
This can be provided as a single value or as a vector of length `res_size`.
Default is 0.1.
- `fb_weight`: Weight of the self loops in the reservoir matrix.
- `fb_weight`: Weight of the backward connections in the reservoir matrix.
This can be provided as a single value or as a vector of length `res_size`.
Default is 0.1.
- `radius`: The desired spectral radius of the reservoir.
If `nothing` is passed, no scaling takes place.
Expand Down Expand Up @@ -1601,23 +1606,25 @@ function selfloop_backward_cycle(
cycle_weight::Union{Number, AbstractVector} = T(0.1f0),
selfloop_weight::Union{Number, AbstractVector} = T(0.1f0),
fb_weight::Union{Number, AbstractVector} = T(0.1f0),
radius::Union{AbstractFloat, Nothing} = nothing,
radius::Union{Real, Nothing} = nothing,
return_sparse::Bool = false
) where {T <: Number}
throw_sparse_error(return_sparse)
check_res_size(dims...)
reservoir_matrix = simple_cycle(
rng, T, dims...;
cycle_weight = T.(cycle_weight), return_sparse = false
cycle_weight = __expand_weight(T, cycle_weight, first(dims)), return_sparse = false
)
selfloop_weights = __expand_weight(T, selfloop_weight, size(reservoir_matrix, 1))
fb_weights = __expand_weight(T, fb_weight, size(reservoir_matrix, 1))
for idx in axes(reservoir_matrix, 1)
if isodd(idx)
reservoir_matrix[idx, idx] = T.(selfloop_weight)
reservoir_matrix[idx, idx] = selfloop_weights[idx]
end
end
for idx in (first(axes(reservoir_matrix, 1)) + 1):last(axes(reservoir_matrix, 1))
if iseven(idx)
reservoir_matrix[idx - 1, idx] = T.(fb_weight)
reservoir_matrix[idx - 1, idx] = fb_weights[idx]
end
end
scale_radius!(reservoir_matrix, radius)
Expand Down Expand Up @@ -1790,7 +1797,7 @@ function selfloop_delayline_backward(
delay_weight::Union{Number, AbstractVector} = T(0.1f0),
fb_weight::Union{Number, AbstractVector} = delay_weight,
selfloop_weight::Union{Number, AbstractVector} = T(0.1f0),
return_sparse::Bool = false, radius::Union{AbstractFloat, Nothing} = nothing,
return_sparse::Bool = false, radius::Union{Real, Nothing} = nothing,
delay_kwargs::NamedTuple = NamedTuple(),
fb_kwargs::NamedTuple = NamedTuple(),
selfloop_kwargs::NamedTuple = NamedTuple()
Expand Down Expand Up @@ -1936,7 +1943,7 @@ function selfloop_forwardconnection(
rng::AbstractRNG, ::Type{T}, dims::Integer...;
forward_weight::Union{Number, AbstractVector} = T(0.1f0),
selfloop_weight::Union{Number, AbstractVector} = T(0.1f0), shift::Integer = 2,
return_sparse::Bool = false, radius::Union{AbstractFloat, Nothing} = nothing,
return_sparse::Bool = false, radius::Union{Real, Nothing} = nothing,
delay_kwargs::NamedTuple = NamedTuple(),
selfloop_kwargs::NamedTuple = NamedTuple()
) where {T <: Number}
Expand Down Expand Up @@ -2061,7 +2068,7 @@ julia> reservoir_matrix = forward_connection(10, 10; return_sparse=true)
function forward_connection(
rng::AbstractRNG, ::Type{T}, dims::Integer...;
forward_weight::Union{Number, AbstractVector} = T(0.1f0),
radius::Union{AbstractFloat, Nothing} = nothing, return_sparse::Bool = false,
radius::Union{Real, Nothing} = nothing, return_sparse::Bool = false,
kwargs...
) where {T <: Number}
throw_sparse_error(return_sparse)
Expand Down Expand Up @@ -2157,7 +2164,7 @@ julia> res_matrix = block_diagonal(10, 10; block_size=2, block_weight=[0.5, 2.0,
function block_diagonal(
rng::AbstractRNG, ::Type{T}, dims::Integer...;
block_weight::Union{Number, AbstractVector} = T(1),
block_size::Integer = 1, radius::Union{AbstractFloat, Nothing} = nothing,
block_size::Integer = 1, radius::Union{Real, Nothing} = nothing,
return_sparse::Bool = false
) where {T <: Number}
throw_sparse_error(return_sparse)
Expand Down Expand Up @@ -2289,7 +2296,7 @@ function permutation_init(
rng::AbstractRNG, ::Type{T}, dims::Integer...;
weight = T(0.1), return_sparse::Bool = false,
permutation_matrix::Union{Nothing, AbstractMatrix} = nothing,
radius::Union{AbstractFloat, Nothing} = nothing,
radius::Union{Real, Nothing} = nothing,
kwargs...
) where {T <: Number}
throw_sparse_error(return_sparse)
Expand Down
2 changes: 1 addition & 1 deletion src/inits/inits_topology.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function __topology_expand(name::Symbol, body)
end
push!(
sig,
__topology_typed_kw(:radius, :(Union{AbstractFloat, Nothing}), :nothing),
__topology_typed_kw(:radius, :(Union{Real, Nothing}), :nothing),
__topology_typed_kw(:return_sparse, :Bool, :false),
)
if n_signs == 1
Expand Down
Loading
Loading