Skip to content

Commit 248940c

Browse files
Merge pull request #279 from SciML/fm/regular
feat: added regular sample
2 parents e9df2fe + 1347487 commit 248940c

File tree

2 files changed

+116
-45
lines changed

2 files changed

+116
-45
lines changed

src/esn/esn_inits.jl

Lines changed: 89 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,17 @@ warning.
140140
If set to `:no_sample` the sign is unchanged. If set to `:bernoulli_sample!` then each
141141
`weight` can be positive with a probability set by `positive_prob`. If set to
142142
`:irrational_sample!` the `weight` is negative if the decimal number of the
143-
irrational number chosen is odd. Default is `:no_sample`.
143+
irrational number chosen is odd. If set to `:regular_sample!`, each weight will be
144+
assigned a negative sign after the chosen `strides`. `strides` can be a single
145+
number or an array. Default is `:no_sample`.
144146
- `positive_prob`: probability of the `weight` being positive when `sampling_type` is
145147
set to `:bernoulli_sample!`. Default is 0.5.
146148
- `irrational`: Irrational number whose decimals decide the sign of `weight`.
147149
Default is `pi`.
148150
- `start`: Which place after the decimal point the counting starts for the `irrational`
149151
sign counting. Default is 1.
152+
- `strides`: number of strides for assigning negative value to a weight. It can be an
153+
integer or an array. Default is 2.
150154
151155
# Examples
152156
@@ -305,13 +309,21 @@ The sign difference is randomly determined by the `sampling` chosen.
305309
# Keyword arguments
306310
307311
- `weight`: The weight used to fill the layer matrix. Default is 0.1.
308-
- `sampling_type`: The sampling parameters used to generate the input matrix.
309-
Default is `:bernoulli_sample!`.
310-
- `irrational`: Irrational number chosen for sampling if `sampling_type=:irrational`.
312+
- `sampling_type`: Sampling that decides the distribution of `weight` negative numbers.
313+
If set to `:no_sample` the sign is unchanged. If set to `:bernoulli_sample!` then each
314+
`weight` can be positive with a probability set by `positive_prob`. If set to
315+
`:irrational_sample!` the `weight` is negative if the decimal number of the
316+
irrational number chosen is odd. If set to `:regular_sample!`, each weight will be
317+
assigned a negative sign after the chosen `strides`. `strides` can be a single
318+
number or an array. Default is `:no_sample`.
319+
- `positive_prob`: probability of the `weight` being positive when `sampling_type` is
320+
set to `:bernoulli_sample!`. Default is 0.5.
321+
- `irrational`: Irrational number whose decimals decide the sign of `weight`.
311322
Default is `pi`.
312-
- `start`: Starting value for the irrational sample. Default is 1
313-
- `positive_prob`: Probability for the Bernoulli sampling. Lower probability increases negative
314-
value. Higher probability increases positive values. Default is 0.5
323+
- `start`: Which place after the decimal point the counting starts for the `irrational`
324+
sign counting. Default is 1.
325+
- `strides`: number of strides for assigning negative value to a weight. It can be an
326+
integer or an array. Default is 2.
315327
316328
# Examples
317329
@@ -1061,13 +1073,17 @@ Create and return a delay line reservoir matrix [^rodan2010].
10611073
If set to `:no_sample` the sign is unchanged. If set to `:bernoulli_sample!` then each
10621074
`weight` can be positive with a probability set by `positive_prob`. If set to
10631075
`:irrational_sample!` the `weight` is negative if the decimal number of the
1064-
irrational number chosen is odd. Default is `:no_sample`.
1065-
- `positive_prob`: probability of the `weight` being positive with `sampling_type`
1066-
set to `:bernoulli_sample!`. Default is 0.5
1076+
irrational number chosen is odd. If set to `:regular_sample!`, each weight will be
1077+
assigned a negative sign after the chosen `strides`. `strides` can be a single
1078+
number or an array. Default is `:no_sample`.
1079+
- `positive_prob`: probability of the `weight` being positive when `sampling_type` is
1080+
set to `:bernoulli_sample!`. Default is 0.5.
10671081
- `irrational`: Irrational number whose decimals decide the sign of `weight`.
10681082
Default is `pi`.
10691083
- `start`: Which place after the decimal point the counting starts for the `irrational`
10701084
sign counting. Default is 1.
1085+
- `strides`: number of strides for assigning negative value to a weight. It can be an
1086+
integer or an array. Default is 2.
10711087
10721088
# Examples
10731089
@@ -1148,13 +1164,17 @@ Creates a matrix with backward connections as described in [^rodan2010].
11481164
If set to `:no_sample` the sign is unchanged. If set to `:bernoulli_sample!` then each
11491165
`weight` can be positive with a probability set by `positive_prob`. If set to
11501166
`:irrational_sample!` the `weight` is negative if the decimal number of the
1151-
irrational number chosen is odd. Default is `:no_sample`.
1167+
irrational number chosen is odd. If set to `:regular_sample!`, each weight will be
1168+
assigned a negative sign after the chosen `strides`. `strides` can be a single
1169+
number or an array. Default is `:no_sample`.
11521170
+ `positive_prob`: probability of the `weight` being positive when `sampling_type` is
1153-
set to `:bernoulli_sample!`. Default is 0.5
1171+
set to `:bernoulli_sample!`. Default is 0.5.
11541172
+ `irrational`: Irrational number whose decimals decide the sign of `weight`.
11551173
Default is `pi`.
11561174
+ `start`: Which place after the decimal point the counting starts for the `irrational`
11571175
sign counting. Default is 1.
1176+
+ `strides`: number of strides for assigning negative value to a weight. It can be an
1177+
integer or an array. Default is 2.
11581178
11591179
# Examples
11601180
@@ -1232,13 +1252,17 @@ Create a cycle jumps reservoir [^Rodan2012].
12321252
If set to `:no_sample` the sign is unchanged. If set to `:bernoulli_sample!` then each
12331253
`weight` can be positive with a probability set by `positive_prob`. If set to
12341254
`:irrational_sample!` the `weight` is negative if the decimal number of the
1235-
irrational number chosen is odd. Default is `:no_sample`.
1255+
irrational number chosen is odd. If set to `:regular_sample!`, each weight will be
1256+
assigned a negative sign after the chosen `strides`. `strides` can be a single
1257+
number or an array. Default is `:no_sample`.
12361258
+ `positive_prob`: probability of the `weight` being positive when `sampling_type` is
1237-
set to `:bernoulli_sample!`. Default is 0.5
1259+
set to `:bernoulli_sample!`. Default is 0.5.
12381260
+ `irrational`: Irrational number whose decimals decide the sign of `weight`.
12391261
Default is `pi`.
12401262
+ `start`: Which place after the decimal point the counting starts for the `irrational`
12411263
sign counting. Default is 1.
1264+
+ `strides`: number of strides for assigning negative value to a weight. It can be an
1265+
integer or an array. Default is 2.
12421266
12431267
# Examples
12441268
@@ -1305,13 +1329,17 @@ Create a simple cycle reservoir [^rodan2010].
13051329
If set to `:no_sample` the sign is unchanged. If set to `:bernoulli_sample!` then each
13061330
`weight` can be positive with a probability set by `positive_prob`. If set to
13071331
`:irrational_sample!` the `weight` is negative if the decimal number of the
1308-
irrational number chosen is odd. Default is `:no_sample`.
1332+
irrational number chosen is odd. If set to `:regular_sample!`, each weight will be
1333+
assigned a negative sign after the chosen `strides`. `strides` can be a single
1334+
number or an array. Default is `:no_sample`.
13091335
- `positive_prob`: probability of the `weight` being positive when `sampling_type` is
1310-
set to `:bernoulli_sample!`. Default is 0.5
1336+
set to `:bernoulli_sample!`. Default is 0.5.
13111337
- `irrational`: Irrational number whose decimals decide the sign of `weight`.
13121338
Default is `pi`.
13131339
- `start`: Which place after the decimal point the counting starts for the `irrational`
13141340
sign counting. Default is 1.
1341+
- `strides`: number of strides for assigning negative value to a weight. It can be an
1342+
integer or an array. Default is 2.
13151343
13161344
# Examples
13171345
@@ -1450,13 +1478,17 @@ W_{i,j} =
14501478
If set to `:no_sample` the sign is unchanged. If set to `:bernoulli_sample!` then each
14511479
`weight` can be positive with a probability set by `positive_prob`. If set to
14521480
`:irrational_sample!` the `weight` is negative if the decimal number of the
1453-
irrational number chosen is odd. Default is `:no_sample`.
1481+
irrational number chosen is odd. If set to `:regular_sample!`, each weight will be
1482+
assigned a negative sign after the chosen `strides`. `strides` can be a single
1483+
number or an array. Default is `:no_sample`.
14541484
- `positive_prob`: probability of the `weight` being positive when `sampling_type` is
1455-
set to `:bernoulli_sample!`. Default is 0.5
1485+
set to `:bernoulli_sample!`. Default is 0.5.
14561486
- `irrational`: Irrational number whose decimals decide the sign of `weight`.
14571487
Default is `pi`.
14581488
- `start`: Which place after the decimal point the counting starts for the `irrational`
14591489
sign counting. Default is 1.
1490+
- `strides`: number of strides for assigning negative value to a weight. It can be an
1491+
integer or an array. Default is 2.
14601492
14611493
# Examples
14621494
@@ -1633,16 +1665,20 @@ W_{i,j} =
16331665
- `delay_kwargs`, `selfloop_kwargs`, and `fb_kwargs`: named tuples that control the kwargs
16341666
for the weights generation. The kwargs are as follows:
16351667
+ `sampling_type`: Sampling that decides the distribution of `weight` negative numbers.
1636-
If set to `:no_sample` the sign is unchanged. If set to `:bernoulli_sample!` then each
1637-
`weight` can be positive with a probability set by `positive_prob`. If set to
1638-
`:irrational_sample!` the `weight` is negative if the decimal number of the
1639-
irrational number chosen is odd. Default is `:no_sample`.
1640-
+ `positive_prob`: probability of the `weight` being positive when `sampling_type` is
1641-
set to `:bernoulli_sample!`. Default is 0.5
1642-
+ `irrational`: Irrational number whose decimals decide the sign of `weight`.
1643-
Default is `pi`.
1644-
+ `start`: Which place after the decimal point the counting starts for the `irrational`
1645-
sign counting. Default is 1.
1668+
If set to `:no_sample` the sign is unchanged. If set to `:bernoulli_sample!` then each
1669+
`weight` can be positive with a probability set by `positive_prob`. If set to
1670+
`:irrational_sample!` the `weight` is negative if the decimal number of the
1671+
irrational number chosen is odd. If set to `:regular_sample!`, each weight will be
1672+
assigned a negative sign after the chosen `strides`. `strides` can be a single
1673+
number or an array. Default is `:no_sample`.
1674+
+ `positive_prob`: probability of the `weight` being positive when `sampling_type` is
1675+
set to `:bernoulli_sample!`. Default is 0.5.
1676+
+ `irrational`: Irrational number whose decimals decide the sign of `weight`.
1677+
Default is `pi`.
1678+
+ `start`: Which place after the decimal point the counting starts for the `irrational`
1679+
sign counting. Default is 1.
1680+
+ `strides`: number of strides for assigning negative value to a weight. It can be an
1681+
integer or an array. Default is 2.
16461682
16471683
# Examples
16481684
@@ -1730,16 +1766,20 @@ W_{i,j} =
17301766
- `delay_kwargs` and `selfloop_kwargs`: named tuples that control the kwargs for the
17311767
delay line weight and self loop weights respectively. The kwargs are as follows:
17321768
+ `sampling_type`: Sampling that decides the distribution of `weight` negative numbers.
1733-
If set to `:no_sample` the sign is unchanged. If set to `:bernoulli_sample!` then each
1734-
`weight` can be positive with a probability set by `positive_prob`. If set to
1735-
`:irrational_sample!` the `weight` is negative if the decimal number of the
1736-
irrational number chosen is odd. Default is `:no_sample`.
1737-
+ `positive_prob`: probability of the `weight` being positive when `sampling_type` is
1738-
set to `:bernoulli_sample!`. Default is 0.5
1739-
+ `irrational`: Irrational number whose decimals decide the sign of `weight`.
1740-
Default is `pi`.
1741-
+ `start`: Which place after the decimal point the counting starts for the `irrational`
1742-
sign counting. Default is 1.
1769+
If set to `:no_sample` the sign is unchanged. If set to `:bernoulli_sample!` then each
1770+
`weight` can be positive with a probability set by `positive_prob`. If set to
1771+
`:irrational_sample!` the `weight` is negative if the decimal number of the
1772+
irrational number chosen is odd. If set to `:regular_sample!`, each weight will be
1773+
assigned a negative sign after the chosen `strides`. `strides` can be a single
1774+
number or an array. Default is `:no_sample`.
1775+
+ `positive_prob`: probability of the `weight` being positive when `sampling_type` is
1776+
set to `:bernoulli_sample!`. Default is 0.5.
1777+
+ `irrational`: Irrational number whose decimals decide the sign of `weight`.
1778+
Default is `pi`.
1779+
+ `start`: Which place after the decimal point the counting starts for the `irrational`
1780+
sign counting. Default is 1.
1781+
+ `strides`: number of strides for assigning negative value to a weight. It can be an
1782+
integer or an array. Default is 2.
17431783
17441784
# Examples
17451785
@@ -1813,16 +1853,20 @@ W_{i,j} =
18131853
- `return_sparse`: flag for returning a `sparse` matrix.
18141854
Default is `false`.
18151855
- `sampling_type`: Sampling that decides the distribution of `weight` negative numbers.
1816-
If set to `:no_sample` the sign is unchanged. If set to `:bernoulli_sample!` then each
1817-
`weight` can be positive with a probability set by `positive_prob`. If set to
1818-
`:irrational_sample!` the `weight` is negative if the decimal number of the
1819-
irrational number chosen is odd. Default is `:no_sample`.
1856+
If set to `:no_sample` the sign is unchanged. If set to `:bernoulli_sample!` then each
1857+
`weight` can be positive with a probability set by `positive_prob`. If set to
1858+
`:irrational_sample!` the `weight` is negative if the decimal number of the
1859+
irrational number chosen is odd. If set to `:regular_sample!`, each weight will be
1860+
assigned a negative sign after the chosen `strides`. `strides` can be a single
1861+
number or an array. Default is `:no_sample`.
18201862
- `positive_prob`: probability of the `weight` being positive when `sampling_type` is
1821-
set to `:bernoulli_sample!`. Default is 0.5
1863+
set to `:bernoulli_sample!`. Default is 0.5.
18221864
- `irrational`: Irrational number whose decimals decide the sign of `weight`.
1823-
Default is `pi`.
1865+
Default is `pi`.
18241866
- `start`: Which place after the decimal point the counting starts for the `irrational`
1825-
sign counting. Default is 1.
1867+
sign counting. Default is 1.
1868+
- `strides`: number of strides for assigning negative value to a weight. It can be an
1869+
integer or an array. Default is 2.
18261870
18271871
# Examples
18281872

src/esn/inits_components.jl

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

46+
function regular_sample!(rng::AbstractRNG, vecormat::AbstractVecOrMat;
47+
strides::Union{Integer, AbstractVector{<:Integer}}=2)
48+
return _regular_sample!(rng, vecormat, strides)
49+
end
50+
51+
function _regular_sample!(rng::AbstractRNG, vecormat::AbstractVecOrMat, strides::Integer)
52+
for idx in eachindex(vecormat)
53+
if idx % strides == 0
54+
vecormat[idx] = -vecormat[idx]
55+
end
56+
end
57+
end
58+
59+
function _regular_sample!(
60+
rng::AbstractRNG, vecormat::AbstractVecOrMat, strides::AbstractVector{<:Integer})
61+
next_flip = strides[1]
62+
strides_idx = 1
63+
64+
for idx in eachindex(vecormat)
65+
if idx == next_flip
66+
vecormat[idx] = -vecormat[idx]
67+
strides_idx = (strides_idx % length(strides)) + 1
68+
next_flip += strides[strides_idx]
69+
end
70+
end
71+
end
72+
4673
function bernoulli_sample!(
4774
rng::AbstractRNG, vecormat::AbstractVecOrMat; positive_prob::Number=0.5)
4875
for idx in eachindex(vecormat)

0 commit comments

Comments
 (0)