Skip to content

Commit d9ecef2

Browse files
Merge pull request #260 from SciML/fm/dc
added double_cycle initializer
2 parents f454ef5 + dbdc30c commit d9ecef2

File tree

5 files changed

+63
-4
lines changed

5 files changed

+63
-4
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ReservoirComputing"
22
uuid = "7c2d2b1e-3dd4-11ea-355a-8f6a8116e294"
33
authors = ["Francesco Martinuzzi"]
4-
version = "0.10.12"
4+
version = "0.10.13"
55

66
[deps]
77
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"

docs/src/api/inits.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@
2323
pseudo_svd
2424
chaotic_init
2525
low_connectivity
26+
double_cycle
2627
```

src/ReservoirComputing.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export StandardRidge
3939
export scaled_rand, weighted_init, informed_init, minimal_init, chebyshev_mapping,
4040
logistic_mapping, modified_lm
4141
export rand_sparse, delay_line, delay_line_backward, cycle_jumps,
42-
simple_cycle, pseudo_svd, chaotic_init, low_connectivity
42+
simple_cycle, pseudo_svd, chaotic_init, low_connectivity, double_cycle
4343
export RNN, MRNN, GRU, GRUParams, FullyGated, Minimal
4444
export train
4545
export ESN, HybridESN, KnowledgeModel, DeepESN

src/esn/esn_inits.jl

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1222,12 +1222,69 @@ function cut_cycle_edge!(
12221222
return reservoir_matrix
12231223
end
12241224

1225+
"""
1226+
double_cycle([rng], [T], dims...;
1227+
cycle_weight=0.1, second_cycle_weight=0.1,
1228+
return_sparse=false)
1229+
1230+
Creates a double cycle reservoir[^fu2023] with the specified dimensions and weights.
1231+
1232+
# Arguments
1233+
1234+
- `rng`: Random number generator. Default is `Utils.default_rng()`
1235+
from WeightInitializers.
1236+
- `T`: Type of the elements in the reservoir matrix. Default is `Float32`.
1237+
- `dims`: Dimensions of the reservoir matrix.
1238+
1239+
# Keyword arguments
1240+
1241+
- `cycle_weight`: Weight of the upper cycle connections in the reservoir matrix.
1242+
Default is 0.1.
1243+
- `second_cycle_weight`: Weight of the lower cycle connections in the reservoir matrix.
1244+
Default is 0.1.
1245+
- `return_sparse`: flag for returning a `sparse` matrix.
1246+
Default is `false`.
1247+
1248+
# Examples
1249+
1250+
```jldoctest
1251+
julia> reservoir_matrix = double_cycle(5, 5; cycle_weight=0.1, second_cycle_weight=0.3)
1252+
5×5 Matrix{Float32}:
1253+
0.0 0.3 0.0 0.0 0.3
1254+
0.1 0.0 0.3 0.0 0.0
1255+
0.0 0.1 0.0 0.3 0.0
1256+
0.0 0.0 0.1 0.0 0.3
1257+
0.1 0.0 0.0 0.1 0.0
1258+
```
1259+
1260+
[^fu2023]: Fu, Jun, et al.
1261+
"A double-cycle echo state network topology for time series prediction."
1262+
Chaos: An Interdisciplinary Journal of Nonlinear Science 33.9 (2023).
1263+
"""
1264+
function double_cycle(rng::AbstractRNG, ::Type{T}, dims::Integer...;
1265+
cycle_weight=T(0.1), second_cycle_weight=T(0.1),
1266+
return_sparse::Bool=false) where {T <: Number}
1267+
throw_sparse_error(return_sparse)
1268+
reservoir_matrix = DeviceAgnostic.zeros(rng, T, dims...)
1269+
1270+
for uidx in 1:(dims[1] - 1)
1271+
reservoir_matrix[uidx + 1, uidx] = cycle_weight
1272+
end
1273+
for lidx in 2:dims[1]
1274+
reservoir_matrix[lidx - 1, lidx] = second_cycle_weight
1275+
end
1276+
1277+
reservoir_matrix[1, dims[1]] = second_cycle_weight
1278+
reservoir_matrix[dims[1], 1] = cycle_weight
1279+
return return_init_as(Val(return_sparse), reservoir_matrix)
1280+
end
1281+
12251282
### fallbacks
12261283
#fallbacks for initializers #eventually to remove once migrated to WeightInitializers.jl
12271284
for initializer in (:rand_sparse, :delay_line, :delay_line_backward, :cycle_jumps,
12281285
:simple_cycle, :pseudo_svd, :chaotic_init,
12291286
:scaled_rand, :weighted_init, :informed_init, :minimal_init, :chebyshev_mapping,
1230-
:logistic_mapping, :modified_lm, :low_connectivity)
1287+
:logistic_mapping, :modified_lm, :low_connectivity, :double_cycle)
12311288
@eval begin
12321289
function ($initializer)(dims::Integer...; kwargs...)
12331290
return $initializer(Utils.default_rng(), Float32, dims...; kwargs...)

test/esn/test_inits.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ reservoir_inits = [
2626
simple_cycle,
2727
pseudo_svd,
2828
chaotic_init,
29-
low_connectivity
29+
low_connectivity,
30+
double_cycle
3031
]
3132
input_inits = [
3233
scaled_rand,

0 commit comments

Comments
 (0)