Skip to content

Commit 69524fd

Browse files
docs: added relevant docstrings for new init kwargs
1 parent 35ba80f commit 69524fd

File tree

5 files changed

+118
-23
lines changed

5 files changed

+118
-23
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.13"
4+
version = "0.11.0"
55

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

docs/src/esn_tutorials/change_layers.md

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Using different layers
22

3-
A great deal of efforts in the ESNs field are devoted to finding an ideal construction for the reservoir matrices. ReservoirComputing.jl offers multiple implementation of reservoir and input matrices initializations found in the literature. The API is standardized, and follows by [WeightInitializers.jl](https://github.com/LuxDL/Lux.jl/tree/main/lib/WeightInitializers):
3+
A great deal of efforts in the ESNs field are devoted to finding an ideal construction
4+
for the reservoir matrices. ReservoirComputing.jl offers multiple implementation of
5+
reservoir and input matrices initializations found in the literature.
6+
The API is standardized, and follows
7+
[WeightInitializers.jl](https://github.com/LuxDL/Lux.jl/tree/main/lib/WeightInitializers):
48

59
```julia
610
weights = init(rng, dims...)
@@ -22,9 +26,13 @@ Custom layers only need to follow these APIs to be compatible with ReservoirComp
2226

2327
## Example of minimally complex ESN
2428

25-
Using [^rodan2012] and [^rodan2010] as references this section will provide an example on how to change both the input layer and the reservoir for ESNs.
29+
Using [^rodan2012] and [^rodan2010] as references this section will provide an
30+
example on how to change both the input layer and the reservoir for ESNs.
2631

27-
The task for this example will be the one step ahead prediction of the Henon map. To obtain the data one can leverage the package [PredefinedDynamicalSystems.jl](https://juliadynamics.github.io/PredefinedDynamicalSystems.jl/dev/). The data is scaled to be between -1 and 1.
32+
The task for this example will be the one step ahead prediction of the Henon map.
33+
To obtain the data one can leverage the package
34+
[PredefinedDynamicalSystems.jl](https://juliadynamics.github.io/PredefinedDynamicalSystems.jl/dev/).
35+
The data is scaled to be between -1 and 1.
2836

2937
```@example minesn
3038
using PredefinedDynamicalSystems
@@ -43,14 +51,16 @@ testing_input = data[:, (shift + train_len):(shift + train_len + predict_len - 1
4351
testing_target = data[:, (shift + train_len + 1):(shift + train_len + predict_len)]
4452
```
4553

46-
Now it is possible to define the input layers and reservoirs we want to compare and run the comparison in a simple for loop. The accuracy will be tested using the mean squared deviation msd from StatsBase.
54+
Now it is possible to define the input layers and reservoirs we want to compare and run
55+
the comparison in a simple for loop. The accuracy will be tested using the mean squared
56+
deviation msd from StatsBase.
4757

4858
```@example minesn
4959
using ReservoirComputing, StatsBase
5060
5161
res_size = 300
52-
input_layer = [minimal_init(; weight=0.85, sampling_type=:irrational),
53-
minimal_init(; weight=0.95, sampling_type=:irrational)]
62+
input_layer = [minimal_init(; weight=0.85, sampling_type=:irrational_sample!),
63+
minimal_init(; weight=0.95, sampling_type=:irrational_sample!)]
5464
reservoirs = [simple_cycle(; weight=0.7),
5565
cycle_jumps(; cycle_weight=0.7, jump_weight=0.2, jump_size=5)]
5666
@@ -64,9 +74,14 @@ for i in 1:length(reservoirs)
6474
end
6575
```
6676

67-
As it is possible to see, changing layers in ESN models is straightforward. Be sure to check the API documentation for a full list of reservoir and layers.
77+
As it is possible to see, changing layers in ESN models is straightforward.
78+
Be sure to check the API documentation for a full list of reservoir and layers.
6879

6980
## Bibliography
7081

71-
[^rodan2012]: Rodan, Ali, and Peter Tiňo. “Simple deterministically constructed cycle reservoirs with regular jumps.” Neural computation 24.7 (2012): 1822-1852.
72-
[^rodan2010]: Rodan, Ali, and Peter Tiňo. “Minimum complexity echo state network.” IEEE transactions on neural networks 22.1 (2010): 131-144.
82+
[^rodan2012]: Rodan, Ali, and Peter Tiňo.
83+
“Simple deterministically constructed cycle reservoirs with regular jumps.”
84+
Neural computation 24.7 (2012): 1822-1852.
85+
[^rodan2010]: Rodan, Ali, and Peter Tiňo.
86+
“Minimum complexity echo state network.”
87+
IEEE transactions on neural networks 22.1 (2010): 131-144.

src/ReservoirComputing.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ export rand_sparse, delay_line, delay_line_backward, cycle_jumps,
4343
simple_cycle, pseudo_svd, chaotic_init, low_connectivity, double_cycle,
4444
selfloop_cycle, selfloop_feedback_cycle, selfloop_delayline_backward,
4545
selfloop_forward_connection, forward_connection
46-
export scale_radius!, delay_line!, backward_connection!, simple_cycle!, add_jumps!
46+
export scale_radius!, delay_line!, backward_connection!, simple_cycle!, add_jumps!,
47+
self_loop!
4748
export RNN, MRNN, GRU, GRUParams, FullyGated, Minimal
4849
export train
4950
export ESN, HybridESN, KnowledgeModel, DeepESN

src/esn/esn_inits.jl

Lines changed: 88 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ julia> res_input = minimal_init(8, 3; p=0.8)# higher p -> more positive signs
243243
"""
244244
function minimal_init(rng::AbstractRNG, ::Type{T}, dims::Integer...;
245245
weight::Number=T(0.1), sampling_type::Symbol=:bernoulli_sample!, kwargs...) where {T <:
246-
Number}
246+
Number}
247247
res_size, in_size = dims
248248
input_matrix = DeviceAgnostic.zeros(rng, T, res_size, in_size)
249249
input_matrix .+= T(weight)
@@ -577,7 +577,8 @@ end
577577

578578
"""
579579
delay_line([rng], [T], dims...;
580-
weight=0.1, return_sparse=false)
580+
weight=0.1, return_sparse=false,
581+
kwargs...)
581582
582583
Create and return a delay line reservoir matrix [^rodan2010].
583584
@@ -608,7 +609,6 @@ Create and return a delay line reservoir matrix [^rodan2010].
608609
- `start`: Which place after the decimal point the counting starts for the `irrational`
609610
sign counting. Default is 1.
610611
611-
612612
# Examples
613613
614614
```jldoctest
@@ -648,7 +648,8 @@ end
648648

649649
"""
650650
delay_line_backward([rng], [T], dims...;
651-
weight=0.1, fb_weight=0.2, return_sparse=false)
651+
weight=0.1, fb_weight=0.2, return_sparse=false,
652+
delay_kwargs=(), fb_kwargs=())
652653
653654
Create a delay line backward reservoir with the specified by `dims` and weights.
654655
Creates a matrix with backward connections as described in [^rodan2010].
@@ -669,6 +670,19 @@ Creates a matrix with backward connections as described in [^rodan2010].
669670
in the reservoir. Default is 0.2
670671
- `return_sparse`: flag for returning a `sparse` matrix.
671672
Default is `false`.
673+
- `delay_kwargs` and `fb_kwargs`: named tuples that control the kwargs for the
674+
delay line weight and feedback weights respectively. The kwargs are as follows:
675+
+ `sampling_type`: Sampling that decides the distribution of `weight` negative numbers.
676+
If set to `:no_sample` the sign is unchanged. If set to `:bernoulli_sample!` then each
677+
`weight` can be positive with a probability set by `positive_prob`. If set to
678+
`:irrational_sample!` the `weight` is negative if the decimal number of the
679+
irrational number chosen is odd. Default is `:no_sample`.
680+
+ `positive_prob`: probability of the `weight` being positive when `sampling_type` is
681+
set to `:bernoulli_sample!`. Default is 0.5
682+
+ `irrational`: Irrational number whose decimals decide the sign of `weight`.
683+
Default is `pi`.
684+
+ `start`: Which place after the decimal point the counting starts for the `irrational`
685+
sign counting. Default is 1.
672686
673687
# Examples
674688
@@ -707,7 +721,8 @@ end
707721

708722
"""
709723
cycle_jumps([rng], [T], dims...;
710-
cycle_weight=0.1, jump_weight=0.1, jump_size=3, return_sparse=false)
724+
cycle_weight=0.1, jump_weight=0.1, jump_size=3, return_sparse=false,
725+
cycle_kwargs=(), jump_kwargs=())
711726
712727
Create a cycle jumps reservoir [^Rodan2012].
713728
@@ -729,6 +744,19 @@ Create a cycle jumps reservoir [^Rodan2012].
729744
Default is 3.
730745
- `return_sparse`: flag for returning a `sparse` matrix.
731746
Default is `false`.
747+
- `cycle_kwargs` and `jump_kwargs`: named tuples that control the kwargs for the
748+
cycle and jump weights respectively. The kwargs are as follows:
749+
+ `sampling_type`: Sampling that decides the distribution of `weight` negative numbers.
750+
If set to `:no_sample` the sign is unchanged. If set to `:bernoulli_sample!` then each
751+
`weight` can be positive with a probability set by `positive_prob`. If set to
752+
`:irrational_sample!` the `weight` is negative if the decimal number of the
753+
irrational number chosen is odd. Default is `:no_sample`.
754+
+ `positive_prob`: probability of the `weight` being positive when `sampling_type` is
755+
set to `:bernoulli_sample!`. Default is 0.5
756+
+ `irrational`: Irrational number whose decimals decide the sign of `weight`.
757+
Default is `pi`.
758+
+ `start`: Which place after the decimal point the counting starts for the `irrational`
759+
sign counting. Default is 1.
732760
733761
# Examples
734762
@@ -769,7 +797,8 @@ end
769797

770798
"""
771799
simple_cycle([rng], [T], dims...;
772-
weight=0.1, return_sparse=false)
800+
weight=0.1, return_sparse=false,
801+
kwargs...)
773802
774803
Create a simple cycle reservoir [^rodan2010].
775804
@@ -786,6 +815,17 @@ Create a simple cycle reservoir [^rodan2010].
786815
Default is 0.1.
787816
- `return_sparse`: flag for returning a `sparse` matrix.
788817
Default is `false`.
818+
- `sampling_type`: Sampling that decides the distribution of `weight` negative numbers.
819+
If set to `:no_sample` the sign is unchanged. If set to `:bernoulli_sample!` then each
820+
`weight` can be positive with a probability set by `positive_prob`. If set to
821+
`:irrational_sample!` the `weight` is negative if the decimal number of the
822+
irrational number chosen is odd. Default is `:no_sample`.
823+
- `positive_prob`: probability of the `weight` being positive when `sampling_type` is
824+
set to `:bernoulli_sample!`. Default is 0.5
825+
- `irrational`: Irrational number whose decimals decide the sign of `weight`.
826+
Default is `pi`.
827+
- `start`: Which place after the decimal point the counting starts for the `irrational`
828+
sign counting. Default is 1.
789829
790830
# Examples
791831
@@ -1210,7 +1250,7 @@ end
12101250
@doc raw"""
12111251
selfloop_cycle([rng], [T], dims...;
12121252
cycle_weight=0.1, selfloop_weight=0.1,
1213-
return_sparse=false)
1253+
return_sparse=false, kwargs...)
12141254
12151255
Creates a simple cycle reservoir with the addition of self loops [^elsarraj2019].
12161256
@@ -1243,6 +1283,17 @@ W_{i,j} =
12431283
Default is 0.1.
12441284
- `return_sparse`: flag for returning a `sparse` matrix.
12451285
Default is `false`.
1286+
- `sampling_type`: Sampling that decides the distribution of `weight` negative numbers.
1287+
If set to `:no_sample` the sign is unchanged. If set to `:bernoulli_sample!` then each
1288+
`weight` can be positive with a probability set by `positive_prob`. If set to
1289+
`:irrational_sample!` the `weight` is negative if the decimal number of the
1290+
irrational number chosen is odd. Default is `:no_sample`.
1291+
- `positive_prob`: probability of the `weight` being positive when `sampling_type` is
1292+
set to `:bernoulli_sample!`. Default is 0.5
1293+
- `irrational`: Irrational number whose decimals decide the sign of `weight`.
1294+
Default is `pi`.
1295+
- `start`: Which place after the decimal point the counting starts for the `irrational`
1296+
sign counting. Default is 1.
12461297
12471298
# Examples
12481299
@@ -1363,7 +1414,8 @@ end
13631414
@doc raw"""
13641415
selfloop_delayline_backward([rng], [T], dims...;
13651416
weight=0.1, selfloop_weight=0.1,
1366-
return_sparse=false)
1417+
return_sparse=false, fb_kwargs=(), selfloop_kwargs=(),
1418+
delay_kwargs=())
13671419
13681420
Creates a reservoir based on a delay line with the addition of self loops and
13691421
backward connections shifted by one [^elsarraj2019].
@@ -1397,6 +1449,19 @@ W_{i,j} =
13971449
Default is 0.1.
13981450
- `return_sparse`: flag for returning a `sparse` matrix.
13991451
Default is `false`.
1452+
- `delay_kwargs`, `selfloop_kwargs`, and `fb_kwargs`: named tuples that control the kwargs
1453+
for the weights generation. The kwargs are as follows:
1454+
+ `sampling_type`: Sampling that decides the distribution of `weight` negative numbers.
1455+
If set to `:no_sample` the sign is unchanged. If set to `:bernoulli_sample!` then each
1456+
`weight` can be positive with a probability set by `positive_prob`. If set to
1457+
`:irrational_sample!` the `weight` is negative if the decimal number of the
1458+
irrational number chosen is odd. Default is `:no_sample`.
1459+
+ `positive_prob`: probability of the `weight` being positive when `sampling_type` is
1460+
set to `:bernoulli_sample!`. Default is 0.5
1461+
+ `irrational`: Irrational number whose decimals decide the sign of `weight`.
1462+
Default is `pi`.
1463+
+ `start`: Which place after the decimal point the counting starts for the `irrational`
1464+
sign counting. Default is 1.
14001465
14011466
# Examples
14021467
@@ -1438,7 +1503,8 @@ end
14381503
@doc raw"""
14391504
selfloop_forward_connection([rng], [T], dims...;
14401505
weight=0.1, selfloop_weight=0.1,
1441-
return_sparse=false)
1506+
return_sparse=false, selfloop_kwargs=(),
1507+
delay_kwargs=())
14421508
14431509
Creates a reservoir based on a forward connection of weights between even nodes
14441510
with the addition of self loops [^elsarraj2019].
@@ -1471,6 +1537,19 @@ W_{i,j} =
14711537
Default is 0.1.
14721538
- `return_sparse`: flag for returning a `sparse` matrix.
14731539
Default is `false`.
1540+
- `delay_kwargs` and `selfloop_kwargs`: named tuples that control the kwargs for the
1541+
delay line weight and self loop weights respectively. The kwargs are as follows:
1542+
+ `sampling_type`: Sampling that decides the distribution of `weight` negative numbers.
1543+
If set to `:no_sample` the sign is unchanged. If set to `:bernoulli_sample!` then each
1544+
`weight` can be positive with a probability set by `positive_prob`. If set to
1545+
`:irrational_sample!` the `weight` is negative if the decimal number of the
1546+
irrational number chosen is odd. Default is `:no_sample`.
1547+
+ `positive_prob`: probability of the `weight` being positive when `sampling_type` is
1548+
set to `:bernoulli_sample!`. Default is 0.5
1549+
+ `irrational`: Irrational number whose decimals decide the sign of `weight`.
1550+
Default is `pi`.
1551+
+ `start`: Which place after the decimal point the counting starts for the `irrational`
1552+
sign counting. Default is 1.
14741553
14751554
# Examples
14761555

src/esn/inits_components.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ function no_sample(rng::AbstractRNG, vecormat::AbstractVecOrMat)
4343
return vecormat
4444
end
4545

46-
function bernoulli_sample!(rng::AbstractRNG, vecormat::AbstractVecOrMat; positive_prob::Number=0.5)
46+
function bernoulli_sample!(
47+
rng::AbstractRNG, vecormat::AbstractVecOrMat; positive_prob::Number=0.5)
4748
for idx in eachindex(vecormat)
4849
if rand(rng) > positive_prob
4950
vecormat[idx] = -vecormat[idx]
@@ -328,7 +329,7 @@ Adds jumps to a given `reservoir_matrix` with chosen `weight` and determined `ju
328329
- `start`: Which place after the decimal point the counting starts for the `irrational`
329330
sign counting. Default is 1.
330331
331-
# Examples
332+
# Examples
332333
333334
```jldoctest
334335
julia> matrix = zeros(Float32, 5, 5)
@@ -423,7 +424,6 @@ julia> self_loop!(matrix, 1.0)
423424
0.0 0.0 0.0 0.0 1.0
424425
```
425426
"""
426-
427427
function self_loop!(rng::AbstractRNG, reservoir_matrix::AbstractMatrix,
428428
weight::Number; kwargs...)
429429
weights = fill(weight, size(reservoir_matrix, 1))

0 commit comments

Comments
 (0)