Skip to content

Commit a1a8e4f

Browse files
added tp5
1 parent ed3838a commit a1a8e4f

File tree

4 files changed

+63
-4
lines changed

4 files changed

+63
-4
lines changed

docs/src/api/inits.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@
2828
selfloop_feedback_cycle
2929
selfloop_delayline_backward
3030
selfloop_forward_connection
31+
forward_connection
3132
```

src/ReservoirComputing.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export scaled_rand, weighted_init, informed_init, minimal_init, chebyshev_mappin
4141
export rand_sparse, delay_line, delay_line_backward, cycle_jumps,
4242
simple_cycle, pseudo_svd, chaotic_init, low_connectivity, double_cycle,
4343
self_loop_cycle, selfloop_feedback_cycle, selfloop_delayline_backward,
44-
selfloop_forward_connection
44+
selfloop_forward_connection, forward_connection
4545
export RNN, MRNN, GRU, GRUParams, FullyGated, Minimal
4646
export train
4747
export ESN, HybridESN, KnowledgeModel, DeepESN

src/esn/esn_inits.jl

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,7 +1518,6 @@ julia> reservoir_matrix = selfloop_forward_connection(5, 5; weight=0.5)
15181518
0.5 0.0 0.1 0.0 0.0
15191519
0.0 0.5 0.0 0.1 0.0
15201520
0.0 0.0 0.5 0.0 0.1
1521-
15221521
```
15231522
15241523
[^elsarraj2019]: Elsarraj, Duaa, et al.
@@ -1537,13 +1536,71 @@ function selfloop_forward_connection(rng::AbstractRNG, ::Type{T}, dims::Integer.
15371536
return return_init_as(Val(return_sparse), reservoir_matrix)
15381537
end
15391538

1539+
"""
1540+
forward_connection([rng], [T], dims...;
1541+
weight=0.1, selfloop_weight=0.1,
1542+
return_sparse=false)
1543+
1544+
Creates a reservoir based on a forward connection of weights [^elsarraj2019].
1545+
1546+
This architecture is referred to as TP5 in the original paper.
1547+
1548+
# Arguments
1549+
1550+
- `rng`: Random number generator. Default is `Utils.default_rng()`
1551+
from WeightInitializers.
1552+
- `T`: Type of the elements in the reservoir matrix. Default is `Float32`.
1553+
- `dims`: Dimensions of the reservoir matrix.
1554+
1555+
# Keyword arguments
1556+
1557+
- `weight`: Weight of the cycle connections in the reservoir matrix.
1558+
Default is 0.1.
1559+
- `return_sparse`: flag for returning a `sparse` matrix.
1560+
Default is `false`.
1561+
1562+
# Examples
1563+
1564+
```jldoctest
1565+
julia> reservoir_matrix = forward_connection(5, 5)
1566+
5×5 Matrix{Float32}:
1567+
0.0 0.0 0.0 0.0 0.0
1568+
0.0 0.0 0.0 0.0 0.0
1569+
0.1 0.0 0.0 0.0 0.0
1570+
0.0 0.1 0.0 0.0 0.0
1571+
0.0 0.0 0.1 0.0 0.0
1572+
1573+
julia> reservoir_matrix = forward_connection(5, 5; weight=0.5)
1574+
5×5 Matrix{Float32}:
1575+
0.0 0.0 0.0 0.0 0.0
1576+
0.0 0.0 0.0 0.0 0.0
1577+
0.5 0.0 0.0 0.0 0.0
1578+
0.0 0.5 0.0 0.0 0.0
1579+
0.0 0.0 0.5 0.0 0.0
1580+
```
1581+
1582+
[^elsarraj2019]: Elsarraj, Duaa, et al.
1583+
"Demystifying echo state network with deterministic simple topologies."
1584+
International Journal of Computational Science and Engineering 19.3 (2019): 407-417.
1585+
"""
1586+
function forward_connection(rng::AbstractRNG, ::Type{T}, dims::Integer...;
1587+
weight=T(0.1f0), return_sparse::Bool=false) where {T <: Number}
1588+
throw_sparse_error(return_sparse)
1589+
reservoir_matrix = DeviceAgnostic.zeros(rng, T, dims...)
1590+
for idx in first(axes(reservoir_matrix, 1)):(last(axes(reservoir_matrix, 1)) - 2)
1591+
reservoir_matrix[idx + 2, idx] = T(weight)
1592+
end
1593+
return return_init_as(Val(return_sparse), reservoir_matrix)
1594+
end
1595+
15401596
### fallbacks
15411597
#fallbacks for initializers #eventually to remove once migrated to WeightInitializers.jl
15421598
for initializer in (:rand_sparse, :delay_line, :delay_line_backward, :cycle_jumps,
15431599
:simple_cycle, :pseudo_svd, :chaotic_init,
15441600
:scaled_rand, :weighted_init, :informed_init, :minimal_init, :chebyshev_mapping,
15451601
:logistic_mapping, :modified_lm, :low_connectivity, :double_cycle, :self_loop_cycle,
1546-
:selfloop_feedback_cycle, :selfloop_delayline_backward, :selfloop_forward_connection)
1602+
:selfloop_feedback_cycle, :selfloop_delayline_backward, :selfloop_forward_connection,
1603+
:forward_connection)
15471604
@eval begin
15481605
function ($initializer)(dims::Integer...; kwargs...)
15491606
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
@@ -31,7 +31,8 @@ reservoir_inits = [
3131
self_loop_cycle,
3232
selfloop_feedback_cycle,
3333
selfloop_delayline_backward,
34-
selfloop_forward_connection
34+
selfloop_forward_connection,
35+
forward_connection
3536
]
3637
input_inits = [
3738
scaled_rand,

0 commit comments

Comments
 (0)