Skip to content

Commit 97b460e

Browse files
added selfloop_feedback_cycle
1 parent b1ba131 commit 97b460e

File tree

4 files changed

+84
-10
lines changed

4 files changed

+84
-10
lines changed

docs/src/api/inits.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@
2525
low_connectivity
2626
double_cycle
2727
self_loop_cycle
28+
selfloop_feedback_cycle
2829
```

src/ReservoirComputing.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export scaled_rand, weighted_init, informed_init, minimal_init, chebyshev_mappin
4040
logistic_mapping, modified_lm
4141
export rand_sparse, delay_line, delay_line_backward, cycle_jumps,
4242
simple_cycle, pseudo_svd, chaotic_init, low_connectivity, double_cycle,
43-
self_loop_cycle
43+
self_loop_cycle, selfloop_feedback_cycle
4444
export RNN, MRNN, GRU, GRUParams, FullyGated, Minimal
4545
export train
4646
export ESN, HybridESN, KnowledgeModel, DeepESN

src/esn/esn_inits.jl

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,11 +1283,13 @@ end
12831283

12841284
"""
12851285
self_loop_cycle([rng], [T], dims...;
1286-
cycle_weight=0.1, self_loop_weight=0.1,
1286+
cycle_weight=0.1, selfloop_weight=0.1,
12871287
return_sparse=false)
12881288
12891289
Creates a simple cycle reservoir with the addition of self loops [^elsarraj2019].
12901290
1291+
This architecture is referred to as TP1 in the original paper.
1292+
12911293
# Arguments
12921294
12931295
- `rng`: Random number generator. Default is `Utils.default_rng()`
@@ -1297,9 +1299,9 @@ Creates a simple cycle reservoir with the addition of self loops [^elsarraj2019]
12971299
12981300
# Keyword arguments
12991301
1300-
- `cycle_weight`: Weight of the upper cycle connections in the reservoir matrix.
1302+
- `cycle_weight`: Weight of the cycle connections in the reservoir matrix.
13011303
Default is 0.1.
1302-
- `self_loop_weight`: Weight of the self loops in the reservoir matrix.
1304+
- `selfloop_weight`: Weight of the self loops in the reservoir matrix.
13031305
Default is 0.1.
13041306
- `return_sparse`: flag for returning a `sparse` matrix.
13051307
Default is `false`.
@@ -1315,7 +1317,7 @@ julia> reservoir_matrix = self_loop_cycle(5, 5)
13151317
0.0 0.0 0.1 0.1 0.0
13161318
0.0 0.0 0.0 0.1 0.1
13171319
1318-
julia> reservoir_matrix = self_loop_cycle(5, 5; weight=0.2, self_loop_weight=0.5)
1320+
julia> reservoir_matrix = self_loop_cycle(5, 5; weight=0.2, selfloop_weight=0.5)
13191321
5×5 Matrix{Float32}:
13201322
0.5 0.0 0.0 0.0 0.2
13211323
0.2 0.5 0.0 0.0 0.0
@@ -1329,12 +1331,81 @@ julia> reservoir_matrix = self_loop_cycle(5, 5; weight=0.2, self_loop_weight=0.5
13291331
International Journal of Computational Science and Engineering 19.3 (2019): 407-417.
13301332
"""
13311333
function self_loop_cycle(rng::AbstractRNG, ::Type{T}, dims::Integer...;
1332-
weight=T(0.1f0), self_loop_weight=T(0.1f0),
1334+
cycle_weight=T(0.1f0), selfloop_weight=T(0.1f0),
13331335
return_sparse::Bool=false) where {T <: Number}
13341336
throw_sparse_error(return_sparse)
13351337
reservoir_matrix = simple_cycle(rng, T, dims...;
1336-
weight=weight, return_sparse=false)
1337-
reservoir_matrix += T(self_loop_weight) .* I(dims[1])
1338+
weight=cycle_weight, return_sparse=false)
1339+
reservoir_matrix += T(selfloop_weight) .* I(dims[1])
1340+
return return_init_as(Val(return_sparse), reservoir_matrix)
1341+
end
1342+
1343+
"""
1344+
selfloop_feedback_cycle([rng], [T], dims...;
1345+
cycle_weight=0.1, selfloop_weight=0.1,
1346+
return_sparse=false)
1347+
1348+
Creates a cycle reservoir with feedback connections on even neurons and
1349+
self loops on odd neurons [^elsarraj2019].
1350+
1351+
This architecture is referred to as TP2 in the original paper.
1352+
1353+
# Arguments
1354+
1355+
- `rng`: Random number generator. Default is `Utils.default_rng()`
1356+
from WeightInitializers.
1357+
- `T`: Type of the elements in the reservoir matrix. Default is `Float32`.
1358+
- `dims`: Dimensions of the reservoir matrix.
1359+
1360+
# Keyword arguments
1361+
1362+
- `cycle_weight`: Weight of the cycle connections in the reservoir matrix.
1363+
Default is 0.1.
1364+
- `selfloop_weight`: Weight of the self loops in the reservoir matrix.
1365+
Default is 0.1.
1366+
- `return_sparse`: flag for returning a `sparse` matrix.
1367+
Default is `false`.
1368+
1369+
# Examples
1370+
1371+
```jldoctest
1372+
julia> reservoir_matrix = selfloop_feedback_cycle(5, 5)
1373+
5×5 Matrix{Float32}:
1374+
0.1 0.1 0.0 0.0 0.1
1375+
0.1 0.0 0.0 0.0 0.0
1376+
0.0 0.1 0.1 0.1 0.0
1377+
0.0 0.0 0.1 0.0 0.0
1378+
0.0 0.0 0.0 0.1 0.1
1379+
1380+
julia> reservoir_matrix = selfloop_feedback_cycle(5, 5; self_loop_weight=0.5)
1381+
5×5 Matrix{Float32}:
1382+
0.5 0.1 0.0 0.0 0.1
1383+
0.1 0.0 0.0 0.0 0.0
1384+
0.0 0.1 0.5 0.1 0.0
1385+
0.0 0.0 0.1 0.0 0.0
1386+
0.0 0.0 0.0 0.1 0.5
1387+
```
1388+
1389+
[^elsarraj2019]: Elsarraj, Duaa, et al.
1390+
"Demystifying echo state network with deterministic simple topologies."
1391+
International Journal of Computational Science and Engineering 19.3 (2019): 407-417.
1392+
"""
1393+
function selfloop_feedback_cycle(rng::AbstractRNG, ::Type{T}, dims::Integer...;
1394+
cycle_weight=T(0.1f0), selfloop_weight=T(0.1f0),
1395+
return_sparse::Bool=false) where {T <: Number}
1396+
throw_sparse_error(return_sparse)
1397+
reservoir_matrix = simple_cycle(rng, T, dims...;
1398+
weight=T(cycle_weight), return_sparse=false)
1399+
for idx in axes(reservoir_matrix, 1)
1400+
if isodd(idx)
1401+
reservoir_matrix[idx, idx] = T(selfloop_weight)
1402+
end
1403+
end
1404+
for idx in (first(axes(reservoir_matrix, 1)) + 1):last(axes(reservoir_matrix, 1))
1405+
if iseven(idx)
1406+
reservoir_matrix[idx - 1, idx] = T(cycle_weight)
1407+
end
1408+
end
13381409
return return_init_as(Val(return_sparse), reservoir_matrix)
13391410
end
13401411

@@ -1343,7 +1414,8 @@ end
13431414
for initializer in (:rand_sparse, :delay_line, :delay_line_backward, :cycle_jumps,
13441415
:simple_cycle, :pseudo_svd, :chaotic_init,
13451416
:scaled_rand, :weighted_init, :informed_init, :minimal_init, :chebyshev_mapping,
1346-
:logistic_mapping, :modified_lm, :low_connectivity, :double_cycle, :self_loop_cycle)
1417+
:logistic_mapping, :modified_lm, :low_connectivity, :double_cycle, :self_loop_cycle,
1418+
:selfloop_feedback_cycle)
13471419
@eval begin
13481420
function ($initializer)(dims::Integer...; kwargs...)
13491421
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
@@ -28,7 +28,8 @@ reservoir_inits = [
2828
chaotic_init,
2929
low_connectivity,
3030
double_cycle,
31-
self_loop_cycle
31+
self_loop_cycle,
32+
selfloop_feedback_cycle
3233
]
3334
input_inits = [
3435
scaled_rand,

0 commit comments

Comments
 (0)