forked from SciML/ReservoirComputing.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinits_reservoir.jl
More file actions
2575 lines (2214 loc) · 90.9 KB
/
inits_reservoir.jl
File metadata and controls
2575 lines (2214 loc) · 90.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
rand_sparse([rng], [T], dims...;
radius=1.0, sparsity=0.1, std=1.0, return_sparse=false)
Create and return a random sparse reservoir matrix.
The matrix will be of size specified by `dims`, with specified `sparsity`
and scaled spectral radius according to `radius`.
## Arguments
- `rng`: Random number generator. Default is `Utils.default_rng()`from
[WeightInitializers](https://lux.csail.mit.edu/stable/api/Building_Blocks/WeightInitializers).
- `T`: Type of the elements in the reservoir matrix.
Default is `Float32`.
- `dims`: Dimensions of the reservoir matrix.
## Keyword arguments
- `radius`: The desired spectral radius of the reservoir.
Defaults to 1.0.
- `sparsity`: The sparsity level of the reservoir matrix,
controlling the fraction of zero elements. Defaults to 0.1.
- `return_sparse`: flag for returning a `sparse` matrix.
`true` requires `SparseArrays` to be loaded.
Default is `false`.
## Examples
Changing the sparsity:
```jldoctest randsparse
julia> res_matrix = rand_sparse(5, 5; sparsity = 0.5)
5×5 Matrix{Float32}:
0.0 0.0 0.0 0.0 0.0
0.0 0.794565 0.0 0.26164 0.0
0.0 0.0 -0.931294 0.0 0.553706
0.723235 -0.524727 0.0 0.0 0.0
1.23723 0.0 0.181824 -1.5478 0.465328
julia> res_matrix = rand_sparse(5, 5; sparsity = 0.2)
5×5 Matrix{Float32}:
0.0 0.0 0.0 0.0 0.0
0.0 0.853184 0.0 0.0 0.0
0.0 0.0 -1.0 0.0 0.0
0.776591 0.0 0.0 0.0 0.0
0.0 0.0 0.0 -1.66199 0.499657
julia> res_matrix = rand_sparse(5, 5; sparsity = 0.8)
5×5 Matrix{Float32}:
0.0 0.229011 0.625026 -0.660061 -1.39078
-0.295761 0.32544 0.0 0.107163 0.0
0.766352 1.44836 -0.381442 -0.435473 0.226788
0.296224 -0.214919 0.00956791 0.0 0.210393
0.506746 0.0 0.0744718 -0.633951 0.19059
```
Returning a sparse matrix:
```jldoctest randsparse
julia> using SparseArrays
julia> res_matrix = rand_sparse(5, 5; sparsity = 0.4, return_sparse = true)
5×5 SparseMatrixCSC{Float32, Int64} with 10 stored entries:
⋅ ⋅ ⋅ ⋅ ⋅
⋅ 0.794565 ⋅ 0.26164 ⋅
⋅ ⋅ -0.931294 ⋅ 0.553706
0.723235 -0.524727 ⋅ ⋅ ⋅
1.23723 ⋅ 0.181824 -1.5478 0.465328
```
"""
function rand_sparse(
rng::AbstractRNG, ::Type{T}, dims::Integer...;
radius::Number = T(1.0), sparsity::Number = T(0.1), std::Number = T(1.0),
return_sparse::Bool = false
) where {T <: Number}
throw_sparse_error(return_sparse)
check_res_size(dims...)
lcl_sparsity = T(1) - sparsity #consistency with current implementations
reservoir_matrix = sparse_init(rng, T, dims...; sparsity = lcl_sparsity, std = std)
reservoir_matrix = scale_radius!(reservoir_matrix, T(radius))
check_inf_nan(reservoir_matrix)
return return_init_as(Val(return_sparse), reservoir_matrix)
end
"""
pseudo_svd([rng], [T], dims...;
max_value=1.0, sparsity=0.1, sorted=true, reverse_sort=false,
return_sparse=false)
Returns an initializer to build a sparse reservoir matrix with the given
`sparsity` by using a pseudo-SVD approach as described in [Yang2018](@cite).
## Arguments
- `rng`: Random number generator. Default is `Utils.default_rng()`from
[WeightInitializers](https://lux.csail.mit.edu/stable/api/Building_Blocks/WeightInitializers).
- `T`: Type of the elements in the reservoir matrix.
Default is `Float32`.
- `dims`: Dimensions of the reservoir matrix.
## Keyword arguments
- `max_value`: The maximum absolute value of elements in the matrix.
Default is 1.0
- `sparsity`: The desired sparsity level of the reservoir matrix.
Default is 0.1
- `sorted`: A boolean indicating whether to sort the singular values before
creating the diagonal matrix. Default is `true`.
- `reverse_sort`: A boolean indicating whether to reverse the sorted
singular values. Default is `false`.
- `return_sparse`: flag for returning a `sparse` matrix.
`true` requires `SparseArrays` to be loaded.
Default is `false`.
- `return_diag`: flag for returning a `Diagonal` matrix. If both `return_diag`
and `return_sparse` are set to `true` priority is given to `return_diag`.
Default is `false`.
## Examples
Default call:
```jldoctest psvd
julia> res_matrix = pseudo_svd(5, 5)
5×5 Matrix{Float32}:
0.306998 0.0 0.0 0.0 0.0
0.0 0.325977 0.0 0.0 0.0
0.0 0.0 0.549051 0.0 0.0
0.0 0.0 0.0 0.726199 0.0
0.0 0.0 0.0 0.0 1.0
```
With reversed sorting:
```jldoctest psvd
julia> pseudo_svd(5, 5; reverse_sort = true)
5×5 Matrix{Float32}:
1.0 0.0 0.0 0.0 0.0
0.0 0.726199 0.0 0.0 0.0
0.0 0.0 0.549051 0.0 0.0
0.0 0.0 0.0 0.325977 0.0
0.0 0.0 0.0 0.0 0.306998
```
With no sorting
```jldoctest psvd
julia> pseudo_svd(5, 5; sorted = false)
5×5 Matrix{Float32}:
0.726199 0.0 0.0 0.0 0.0
0.0 0.325977 0.0 0.0 0.0
0.0 0.0 0.306998 0.0 0.0
0.0 0.0 0.0 0.549051 0.0
0.0 0.0 0.0 0.0 0.788919
```
Returning as a `Diagonal` or a `sparse` matrix:
```jldoctest psvd
julia> pseudo_svd(5, 5; return_diag = true)
5×5 LinearAlgebra.Diagonal{Float32, Vector{Float32}}:
0.306998 ⋅ ⋅ ⋅ ⋅
⋅ 0.325977 ⋅ ⋅ ⋅
⋅ ⋅ 0.549051 ⋅ ⋅
⋅ ⋅ ⋅ 0.726199 ⋅
⋅ ⋅ ⋅ ⋅ 1.0
julia> using SparseArrays
julia> pseudo_svd(5, 5; return_sparse = true)
5×5 SparseMatrixCSC{Float32, Int64} with 5 stored entries:
0.306998 ⋅ ⋅ ⋅ ⋅
⋅ 0.325977 ⋅ ⋅ ⋅
⋅ ⋅ 0.549051 ⋅ ⋅
⋅ ⋅ ⋅ 0.726199 ⋅
⋅ ⋅ ⋅ ⋅ 1.0
```
"""
function pseudo_svd(
rng::AbstractRNG, ::Type{T}, dims::Integer...;
max_value::Number = T(1),
sparsity::Number = 0.1f0,
sorted::Bool = true,
reverse_sort::Bool = false,
return_sparse::Bool = false,
return_diag::Bool = false
) where {T <: Number}
@assert !isempty(dims) "expected at least one dimension"
res_dim = Int(dims[1])
reservoir_matrix = create_diag(
rng, T, res_dim, T(max_value);
sorted = sorted, reverse_sort = reverse_sort
)
tmp = get_sparsity(reservoir_matrix, res_dim)
while tmp <= sparsity
i = rand_range(rng, res_dim)
j = rand_range(rng, res_dim)
θ = DeviceAgnostic.rand(rng, T) * T(2) .- T(1)
reservoir_matrix = reservoir_matrix * create_qmatrix(rng, T, res_dim, i, j, θ)
tmp = get_sparsity(reservoir_matrix, res_dim)
end
check_inf_nan(reservoir_matrix)
if return_diag
return Diagonal(diag(reservoir_matrix))
else
return return_init_as(Val(return_sparse), reservoir_matrix)
end
end
rand_range(rng::AbstractRNG, n::Integer) = rand(rng, 1:n)
function get_sparsity(reservoir_matrix::AbstractMatrix, res_dim::Integer)
num_notzeros = count(!iszero, reservoir_matrix)
num_zeros = res_dim * res_dim - num_notzeros
return num_notzeros / num_zeros
end
function create_diag(
rng::AbstractRNG, ::Type{T}, res_dim::Integer, max_value::Number;
sorted::Bool = true, reverse_sort::Bool = false
) where {T <: Number}
diag_matrix = DeviceAgnostic.rand(rng, T, Int(res_dim)) .* T(max_value)
if sorted
sort!(diag_matrix)
if reverse_sort
reverse!(diag_matrix)
diag_matrix[1] = T(max_value)
else
diag_matrix[end] = T(max_value)
end
end
full_diag = DeviceAgnostic.zeros(rng, T, Int(res_dim), Int(res_dim))
@inbounds for i in 1:res_dim
full_diag[i, i] = diag_matrix[i]
end
return full_diag
end
function create_qmatrix(
rng::AbstractRNG, ::Type{T}, n::Integer,
i::Integer, j::Integer, θ::Number
) where {T <: Number}
Q = DeviceAgnostic.zeros(rng, T, Int(n), Int(n))
@inbounds for k in 1:n
Q[k, k] = one(T)
end
c = cos(T(θ))
s = sin(T(θ))
@inbounds begin
Q[i, i] = c
Q[j, j] = c
Q[i, j] = -s
Q[j, i] = s
end
return Q
end
"""
chaotic_init([rng], [T], dims...;
extra_edge_probability=T(0.1), radius=one(T),
return_sparse=false)
Construct a chaotic reservoir matrix using a digital chaotic system [Xie2024](@cite).
The matrix topology is derived from a strongly connected adjacency
matrix based on a digital chaotic system operating at finite precision.
If the requested matrix order does not exactly match a valid order the
closest valid order is used.
## Arguments
- `rng`: Random number generator. Default is `Utils.default_rng()`from
[WeightInitializers](https://lux.csail.mit.edu/stable/api/Building_Blocks/WeightInitializers).
- `T`: Type of the elements in the reservoir matrix.
Default is `Float32`.
- `dims`: Dimensions of the reservoir matrix.
## Keyword arguments
- `extra_edge_probability`: Probability of adding extra random edges in
the adjacency matrix to enhance connectivity. Default is 0.1.
- `radius`: The target spectral radius for the
reservoir matrix. Default is one.
- `return_sparse`: flag for returning a `sparse` matrix.
`true` requires `SparseArrays` to be loaded.
Default is `false`.
## Examples
```jldoctest
julia> res_matrix = chaotic_init(8, 8)
┌ Warning:
│
│ Adjusting reservoir matrix order:
│ from 8 (requested) to 4
│ based on computed bit precision = 1.
│
└ @ ReservoirComputing ~/.julia/dev/ReservoirComputing/src/esn/esn_inits.jl:805
4×4 SparseArrays.SparseMatrixCSC{Float32, Int64} with 6 stored entries:
⋅ -0.600945 ⋅ ⋅
⋅ ⋅ 0.132667 2.21354
⋅ -2.60383 ⋅ -2.90391
-0.578156 ⋅ ⋅ ⋅
```
"""
function chaotic_init(
rng::AbstractRNG, ::Type{T}, dims::Integer...;
extra_edge_probability::AbstractFloat = T(0.1f0), radius::AbstractFloat = one(T),
return_sparse::Bool = false
) where {T <: Number}
throw_sparse_error(return_sparse)
check_res_size(dims...)
requested_order = first(dims)
if length(dims) > 1 && dims[2] != requested_order
@warn """\n
Using dims[1] = $requested_order for the chaotic reservoir matrix order.\n
"""
end
d_estimate = log2(requested_order) / 2
d_floor = max(floor(Int, d_estimate), 1)
d_ceil = ceil(Int, d_estimate)
candidate_order_floor = 2^(2 * d_floor)
candidate_order_ceil = 2^(2 * d_ceil)
chosen_bit_precision = abs(candidate_order_floor - requested_order) <=
abs(candidate_order_ceil - requested_order) ? d_floor : d_ceil
actual_matrix_order = 2^(2 * chosen_bit_precision)
if actual_matrix_order != requested_order
@warn """\n
Adjusting reservoir matrix order:
from $requested_order (requested) to $actual_matrix_order
based on computed bit precision = $chosen_bit_precision. \n
"""
end
random_weight_matrix = T(2) * rand(rng, T, actual_matrix_order, actual_matrix_order) .-
T(1)
adjacency_matrix = digital_chaotic_adjacency(
rng, chosen_bit_precision; extra_edge_probability = extra_edge_probability
)
reservoir_matrix = random_weight_matrix .* adjacency_matrix
current_spectral_radius = maximum(abs, eigvals(reservoir_matrix))
if current_spectral_radius != 0
reservoir_matrix .*= radius / current_spectral_radius
end
check_inf_nan(reservoir_matrix)
return return_init_as(Val(return_sparse), reservoir_matrix)
end
function digital_chaotic_adjacency(
rng::AbstractRNG, bit_precision::Integer;
extra_edge_probability::AbstractFloat = 0.1
)
matrix_order = 2^(2 * bit_precision)
adjacency_matrix = DeviceAgnostic.zeros(rng, Int, matrix_order, matrix_order)
for row_index in 1:(matrix_order - 1)
adjacency_matrix[row_index, row_index + 1] = 1
end
adjacency_matrix[matrix_order, 1] = 1
for row_index in 1:matrix_order, column_index in 1:matrix_order
if row_index != column_index && rand(rng) < extra_edge_probability
adjacency_matrix[row_index, column_index] = 1
end
end
return adjacency_matrix
end
"""
low_connectivity([rng], [T], dims...;
connected=false, in_degree = 1, radius = 1.0,
cut_cycle = false, radius=nothing, return_sparse = false)
Construct an internal reservoir connectivity matrix with low connectivity.
This function creates a reservoir matrix with the specified in-degree
for each node [Griffith2019](@cite). When `in_degree` is 1, the function can enforce
a fully connected cycle if `connected` is `true`;
otherwise, it generates a random connectivity pattern.
## Arguments
- `rng`: Random number generator. Default is `Utils.default_rng()`from
[WeightInitializers](https://lux.csail.mit.edu/stable/api/Building_Blocks/WeightInitializers).
- `T`: Type of the elements in the reservoir matrix.
Default is `Float32`.
- `dims`: Dimensions of the reservoir matrix.
## Keyword Arguments
- `connected`: For `in_degree == 1`, if `true` a connected cycle is enforced.
Default is `false`.
- `in_degree`: The number of incoming connections per node.
Must not exceed the number of nodes. Default is 1.
- `radius`: The desired spectral radius of the reservoir.
Defaults to 1.0.
- `cut_cycle`: If `true`, removes one edge from the cycle to cut it.
Default is `false`.
- `radius`: The desired spectral radius of the reservoir.
If `nothing` is passed, no scaling takes place.
Defaults to `nothing`.
- `return_sparse`: flag for returning a `sparse` matrix.
`true` requires `SparseArrays` to be loaded.
Default is `false`.
## Examples
```jldoctest lowcon
julia> low_connectivity(10, 10)
10×10 Matrix{Float32}:
0.0 0.0 0.0 … 0.0 0.0 0.2207
0.0 0.0 0.0 0.0 0.0 0.564821
0.318999 0.0 0.0 0.0 0.0 0.0
0.670023 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 1.79705 0.0 0.0
0.0 -1.95711 0.0 … 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 -0.650657 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 -1.0
```
"""
function low_connectivity(
rng::AbstractRNG, ::Type{T}, dims::Integer...;
return_sparse::Bool = false, connected::Bool = false,
in_degree::Integer = 1, radius::Union{AbstractFloat, Nothing} = nothing,
kwargs...
) where {T <: Number}
check_res_size(dims...)
res_size = dims[1]
if in_degree > res_size
error(
"""
In-degree k (got k=$(in_degree)) cannot exceed number of nodes N=$(res_size)
"""
)
end
if in_degree == 1
reservoir_matrix = build_cycle(
Val(connected), rng, T, res_size; in_degree = in_degree, kwargs...
)
else
reservoir_matrix = build_cycle(
Val(false), rng, T, res_size; in_degree = in_degree, kwargs...
)
end
scale_radius!(reservoir_matrix, radius)
check_inf_nan(reservoir_matrix)
return return_init_as(Val(return_sparse), reservoir_matrix)
end
function build_cycle(
::Val{false}, rng::AbstractRNG, ::Type{T}, res_size::Int;
in_degree::Integer = 1, radius::Number = T(1.0f0),
cut_cycle::Bool = false
) where {T <: Number}
reservoir_matrix = DeviceAgnostic.zeros(rng, T, res_size, res_size)
for idx in 1:res_size
selected = randperm(rng, res_size)[1:in_degree]
for jdx in selected
reservoir_matrix[idx, jdx] = T(randn(rng))
end
end
reservoir_matrix = scale_radius!(reservoir_matrix, T(radius))
return reservoir_matrix
end
function build_cycle(
::Val{true}, rng::AbstractRNG, ::Type{T}, res_size::Int;
in_degree::Integer = 1, radius::Number = T(1.0f0), cut_cycle::Bool = false
) where {
T <:
Number,
}
reservoir_matrix = DeviceAgnostic.zeros(rng, T, res_size, res_size)
perm = randperm(rng, res_size)
for idx in 1:(res_size - 1)
reservoir_matrix[perm[idx], perm[idx + 1]] = T(randn(rng))
end
reservoir_matrix[perm[res_size], perm[1]] = T(randn(rng))
reservoir_matrix = scale_radius!(reservoir_matrix, T(radius))
if cut_cycle
cut_cycle_edge!(reservoir_matrix, rng)
end
return reservoir_matrix
end
function cut_cycle_edge!(
reservoir_matrix::AbstractMatrix{T}, rng::AbstractRNG
) where {T <: Number}
res_size = size(reservoir_matrix, 1)
row = rand(rng, 1:res_size)
for idx in 1:res_size
if reservoir_matrix[row, idx] != zero(T)
reservoir_matrix[row, idx] = zero(T)
break
end
end
return reservoir_matrix
end
@doc raw"""
delay_line([rng], [T], dims...;
delay_weight=0.1, delay_shift=1,
return_sparse=false, radius=nothing, kwargs...)
Create and return a delay line reservoir matrix [Rodan2011](@cite).
```math
W_{i,j} =
\begin{cases}
r, & \text{if } i = j + 1, j \in [1, D_{\mathrm{res}} - 1], \\[6pt]
0, & \text{otherwise.}
\end{cases}
```
## Arguments
- `rng`: Random number generator. Default is `Utils.default_rng()`from
[WeightInitializers](https://lux.csail.mit.edu/stable/api/Building_Blocks/WeightInitializers).
- `T`: Type of the elements in the reservoir matrix.
Default is `Float32`.
- `dims`: Dimensions of the reservoir matrix.
## Keyword arguments
- `delay_weight`: Determines the value of all connections in the reservoir.
This can be provided as a single value or an array. In case it is provided as an
array please make sure that the length of the array matches the length of the sub-diagonal
you want to populate.
Default is 0.1.
- `delay_shift`: delay line shift. Default is 1.
- `radius`: The desired spectral radius of the reservoir.
If `nothing` is passed, no scaling takes place.
Defaults to `nothing`.
- `return_sparse`: flag for returning a `sparse` matrix.
`true` requires `SparseArrays` to be loaded.
Default is `false`.
- `sampling_type`: Sampling that decides the distribution of `weight` negative numbers.
If set to `:no_sample` the sign is unchanged. If set to `:bernoulli_sample!` then each
`weight` can be positive with a probability set by `positive_prob`. If set to
`:irrational_sample!` the `weight` is negative if the decimal number of the
irrational number chosen is odd. If set to `:regular_sample!`, each weight will be
assigned a negative sign after the chosen `strides`. `strides` can be a single
number or an array. Default is `:no_sample`.
- `positive_prob`: probability of the `weight` being positive when `sampling_type` is
set to `:bernoulli_sample!`. Default is 0.5.
- `irrational`: Irrational number whose decimals decide the sign of `weight`.
Default is `pi`.
- `start`: Which place after the decimal point the counting starts for the `irrational`
sign counting. Default is 1.
- `strides`: number of strides for assigning negative value to a weight. It can be an
integer or an array. Default is 2.
## Examples
Default call:
```jldoctest delayline
julia> res_matrix = delay_line(5, 5)
5×5 Matrix{Float32}:
0.0 0.0 0.0 0.0 0.0
0.1 0.0 0.0 0.0 0.0
0.0 0.1 0.0 0.0 0.0
0.0 0.0 0.1 0.0 0.0
0.0 0.0 0.0 0.1 0.0
```
Changing weights:
```jldoctest delayline
julia> res_matrix = delay_line(5, 5; delay_weight = 1)
5×5 Matrix{Float32}:
0.0 0.0 0.0 0.0 0.0
1.0 0.0 0.0 0.0 0.0
0.0 1.0 0.0 0.0 0.0
0.0 0.0 1.0 0.0 0.0
0.0 0.0 0.0 1.0 0.0
```
Changing weights to a custom array:
```jldoctest delayline
julia> res_matrix = delay_line(5, 5; delay_weight = rand(Float32, 4))
5×5 Matrix{Float32}:
0.0 0.0 0.0 0.0 0.0
0.398408 0.0 0.0 0.0 0.0
0.0 0.624473 0.0 0.0 0.0
0.0 0.0 0.66302 0.0 0.0
0.0 0.0 0.0 0.0780818 0.0
```
Changing sign of the weights with different samplings:
```jldoctest delayline
julia> res_matrix = delay_line(5, 5; sampling_type=:irrational_sample!)
5×5 Matrix{Float32}:
0.0 0.0 0.0 0.0 0.0
-0.1 0.0 0.0 0.0 0.0
0.0 0.1 0.0 0.0 0.0
0.0 0.0 -0.1 0.0 0.0
0.0 0.0 0.0 -0.1 0.0
julia> res_matrix = delay_line(5, 5; sampling_type=:bernoulli_sample!)
5×5 Matrix{Float32}:
0.0 0.0 0.0 0.0 0.0
0.1 0.0 0.0 0.0 0.0
0.0 -0.1 0.0 0.0 0.0
0.0 0.0 0.1 0.0 0.0
0.0 0.0 0.0 -0.1 0.0
```
Shifting the delay line:
```jldoctest delayline
julia> res_matrix = delay_line(5, 5; delay_shift = 3)
5×5 Matrix{Float32}:
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
0.1 0.0 0.0 0.0 0.0
0.0 0.1 0.0 0.0 0.0
```
Returning as sparse:
```jldoctest delayline
julia> using SparseArrays
julia> res_matrix = delay_line(5, 5; return_sparse=true)
5×5 SparseMatrixCSC{Float32, Int64} with 4 stored entries:
⋅ ⋅ ⋅ ⋅ ⋅
0.1 ⋅ ⋅ ⋅ ⋅
⋅ 0.1 ⋅ ⋅ ⋅
⋅ ⋅ 0.1 ⋅ ⋅
⋅ ⋅ ⋅ 0.1 ⋅
```
"""
function delay_line(
rng::AbstractRNG, ::Type{T}, dims::Integer...;
delay_weight::Union{Number, AbstractVector} = T(0.1f0), delay_shift::Integer = 1,
return_sparse::Bool = false, radius::Union{AbstractFloat, Nothing} = nothing,
kwargs...
) where {T <: Number}
throw_sparse_error(return_sparse)
check_res_size(dims...)
reservoir_matrix = DeviceAgnostic.zeros(rng, T, dims...)
delay_line!(rng, reservoir_matrix, T.(delay_weight), delay_shift; kwargs...)
scale_radius!(reservoir_matrix, radius)
return return_init_as(Val(return_sparse), reservoir_matrix)
end
@doc raw"""
delayline_backward([rng], [T], dims...;
delay_weight=0.1, fb_weight=0.1,
delay_shift=1, fb_shift=1, return_sparse=false,
radius=nothing, delay_kwargs=(), fb_kwargs=())
Create a delay line backward reservoir with the specified by `dims` and weights.
Creates a matrix with backward connections as described in [Rodan2011](@cite).
```math
W_{i,j} =
\begin{cases}
r, & \text{if } i = j + 1,\;\; j \in [1, D_{\mathrm{res}} - 1], \\[4pt]
b, & \text{if } j = i + 1,\;\; i \in [1, D_{\mathrm{res}} - 1], \\[6pt]
0, & \text{otherwise.}
\end{cases}
```
## Arguments
- `rng`: Random number generator. Default is `Utils.default_rng()`from
[WeightInitializers](https://lux.csail.mit.edu/stable/api/Building_Blocks/WeightInitializers).
- `T`: Type of the elements in the reservoir matrix.
Default is `Float32`.
- `dims`: Dimensions of the reservoir matrix.
## Keyword arguments
- `delay_weight`: The weight determines the absolute value of
forward connections in the reservoir.
This can be provided as a single value or an array. In case it is provided as an
array please make sure that the length of the array matches the length of the sub-diagonal
you want to populate.
Default is 0.1.
- `fb_weight`: Determines the absolute value of backward connections
in the reservoir.
This can be provided as a single value or an array. In case it is provided as an
array please make sure that the length of the array matches the length of the sub-diagonal
you want to populate.
Default is 0.1.
- `fb_shift`: How far the backward connection will be from the diagonal.
Default is 1.
- `delay_shift`: delay line shift relative to the diagonal. Default is 1.
- `radius`: The desired spectral radius of the reservoir.
If `nothing` is passed, no scaling takes place.
Defaults to `nothing`.
- `return_sparse`: flag for returning a `sparse` matrix.
`true` requires `SparseArrays` to be loaded.
Default is `false`.
- `delay_kwargs` and `fb_kwargs`: named tuples that control the kwargs for the
delay line weight and feedback weights respectively. The kwargs are as follows:
+ `sampling_type`: Sampling that decides the distribution of `weight` negative numbers.
If set to `:no_sample` the sign is unchanged. If set to `:bernoulli_sample!` then each
`weight` can be positive with a probability set by `positive_prob`. If set to
`:irrational_sample!` the `weight` is negative if the decimal number of the
irrational number chosen is odd. If set to `:regular_sample!`, each weight will be
assigned a negative sign after the chosen `strides`. `strides` can be a single
number or an array. Default is `:no_sample`.
+ `positive_prob`: probability of the `weight` being positive when `sampling_type` is
set to `:bernoulli_sample!`. Default is 0.5.
+ `irrational`: Irrational number whose decimals decide the sign of `weight`.
Default is `pi`.
+ `start`: Which place after the decimal point the counting starts for the `irrational`
sign counting. Default is 1.
+ `strides`: number of strides for assigning negative value to a weight. It can be an
integer or an array. Default is 2.
## Examples
Default call:
```jldoctest dlbackward
julia> res_matrix = delayline_backward(5, 5)
5×5 Matrix{Float32}:
0.0 0.1 0.0 0.0 0.0
0.1 0.0 0.1 0.0 0.0
0.0 0.1 0.0 0.1 0.0
0.0 0.0 0.1 0.0 0.1
0.0 0.0 0.0 0.1 0.0
```
Changing weights:
```jldoctest dlbackward
julia> res_matrix = delayline_backward(5, 5; delay_weight = 0.99, fb_weight=-1.0)
5×5 Matrix{Float32}:
0.0 -1.0 0.0 0.0 0.0
0.99 0.0 -1.0 0.0 0.0
0.0 0.99 0.0 -1.0 0.0
0.0 0.0 0.99 0.0 -1.0
0.0 0.0 0.0 0.99 0.0
```
Changing weights to custom arrays:
```jldoctest dlbackward
julia> res_matrix = delayline_backward(5, 5; delay_weight = rand(4), fb_weight=.-rand(4))
5×5 Matrix{Float32}:
0.0 -0.294809 0.0 0.0 0.0
0.736006 0.0 -0.449479 0.0 0.0
0.0 0.10892 0.0 -0.60118 0.0
0.0 0.0 0.482435 0.0 -0.673392
0.0 0.0 0.0 0.177982 0.0
```
Changing sign of the weights with different samplings:
```jldoctest dlbackward
julia> res_matrix = delayline_backward(5, 5; delay_kwargs=(;sampling_type=:irrational_sample!))
5×5 Matrix{Float32}:
0.0 0.1 0.0 0.0 0.0
-0.1 0.0 0.1 0.0 0.0
0.0 0.1 0.0 0.1 0.0
0.0 0.0 -0.1 0.0 0.1
0.0 0.0 0.0 -0.1 0.0
julia> res_matrix = delayline_backward(5, 5; fb_kwargs=(;sampling_type=:bernoulli_sample!))
5×5 Matrix{Float32}:
0.0 0.1 0.0 0.0 0.0
0.1 0.0 -0.1 0.0 0.0
0.0 0.1 0.0 0.1 0.0
0.0 0.0 0.1 0.0 -0.1
0.0 0.0 0.0 0.1 0.0
```
Shifting:
```jldoctest dlbackward
julia> res_matrix = delayline_backward(5, 5; delay_shift=3, fb_shift=2)
5×5 Matrix{Float32}:
0.0 0.0 0.1 0.0 0.0
0.0 0.0 0.0 0.1 0.0
0.0 0.0 0.0 0.0 0.1
0.1 0.0 0.0 0.0 0.0
0.0 0.1 0.0 0.0 0.0
```
Returning as sparse:
```jldoctest dlbackward
julia> using SparseArrays
julia> res_matrix = delayline_backward(5, 5; return_sparse=true)
5×5 SparseMatrixCSC{Float32, Int64} with 8 stored entries:
⋅ 0.1 ⋅ ⋅ ⋅
0.1 ⋅ 0.1 ⋅ ⋅
⋅ 0.1 ⋅ 0.1 ⋅
⋅ ⋅ 0.1 ⋅ 0.1
⋅ ⋅ ⋅ 0.1 ⋅
```
"""
function delayline_backward(
rng::AbstractRNG, ::Type{T}, dims::Integer...;
delay_weight::Union{Number, AbstractVector} = T(0.1f0),
fb_weight::Union{Number, AbstractVector} = T(0.1f0), delay_shift::Integer = 1,
fb_shift::Integer = 1, return_sparse::Bool = false,
radius::Union{AbstractFloat, Nothing} = nothing,
delay_kwargs::NamedTuple = NamedTuple(),
fb_kwargs::NamedTuple = NamedTuple()
) where {T <: Number}
throw_sparse_error(return_sparse)
check_res_size(dims...)
reservoir_matrix = DeviceAgnostic.zeros(rng, T, dims...)
delay_line!(rng, reservoir_matrix, T.(delay_weight), delay_shift; delay_kwargs...)
backward_connection!(rng, reservoir_matrix, T.(fb_weight), fb_shift; fb_kwargs...)
scale_radius!(reservoir_matrix, radius)
return return_init_as(Val(return_sparse), reservoir_matrix)
end
@doc raw"""
cycle_jumps([rng], [T], dims...;
cycle_weight=0.1, jump_weight=0.1, jump_size=3, return_sparse=false,
radius=nothing, cycle_kwargs=(), jump_kwargs=())
Create a cycle reservoir with jumps [Rodan2012](@cite).
```math
W_{i,j} =
\begin{cases}
r, & \text{if } i = j + 1,\;\; j \in [1, D_{\mathrm{res}} - 1], \\[4pt]
r, & \text{if } i = 1,\;\; j = D_{\mathrm{res}}, \\[8pt]
r_j, & \text{if } i = j + \ell, \\[4pt]
r_j, & \text{if } j = i + \ell, \\[4pt]
r_j, & \text{if } (i,j) = (1+\ell, 1), \\[4pt]
r_j, & \text{if } (i,j) = (1,\, D_{\mathrm{res}}+1-\ell), \\[8pt]
0, & \text{otherwise.}
\end{cases}
```
## Arguments
- `rng`: Random number generator. Default is `Utils.default_rng()`from
[WeightInitializers](https://lux.csail.mit.edu/stable/api/Building_Blocks/WeightInitializers).
- `T`: Type of the elements in the reservoir matrix.
Default is `Float32`.
- `dims`: Dimensions of the reservoir matrix.
## Keyword arguments
- `cycle_weight`: The weight of cycle connections.
This can be provided as a single value or an array. In case it is provided as an
array please make sure that the length of the array matches the length of the cycle
you want to populate.
Default is 0.1.
- `jump_weight`: The weight of jump connections.
This can be provided as a single value or an array. In case it is provided as an
array please make sure that the length of the array matches the length of the jumps
you want to populate.
Default is 0.1.
- `jump_size`: The number of steps between jump connections.
Default is 3.
- `radius`: The desired spectral radius of the reservoir.
If `nothing` is passed, no scaling takes place.
Defaults to `nothing`.
- `return_sparse`: flag for returning a `sparse` matrix.
`true` requires `SparseArrays` to be loaded.
Default is `false`.
- `cycle_kwargs` and `jump_kwargs`: named tuples that control the kwargs for the
cycle and jump weights respectively. The kwargs are as follows:
+ `sampling_type`: Sampling that decides the distribution of `weight` negative numbers.
If set to `:no_sample` the sign is unchanged. If set to `:bernoulli_sample!` then each
`weight` can be positive with a probability set by `positive_prob`. If set to
`:irrational_sample!` the `weight` is negative if the decimal number of the
irrational number chosen is odd. If set to `:regular_sample!`, each weight will be
assigned a negative sign after the chosen `strides`. `strides` can be a single
number or an array. Default is `:no_sample`.
+ `positive_prob`: probability of the `weight` being positive when `sampling_type` is
set to `:bernoulli_sample!`. Default is 0.5.
+ `irrational`: Irrational number whose decimals decide the sign of `weight`.
Default is `pi`.
+ `start`: Which place after the decimal point the counting starts for the `irrational`
sign counting. Default is 1.
+ `strides`: number of strides for assigning negative value to a weight. It can be an
integer or an array. Default is 2.
## Examples
Default call:
```jldoctest cyclejumps
julia> res_matrix = cycle_jumps(5, 5)
5×5 Matrix{Float32}:
0.0 0.0 0.0 0.1 0.1
0.1 0.0 0.0 0.0 0.0
0.0 0.1 0.0 0.0 0.0
0.1 0.0 0.1 0.0 0.0
0.0 0.0 0.0 0.1 0.0
```
Changing weights:
```jldoctest cyclejumps
julia> res_matrix = cycle_jumps(5, 5; jump_weight = 2, cycle_weight=-1)
5×5 Matrix{Float32}:
0.0 0.0 0.0 2.0 -1.0
-1.0 0.0 0.0 0.0 0.0
0.0 -1.0 0.0 0.0 0.0
2.0 0.0 -1.0 0.0 0.0
0.0 0.0 0.0 -1.0 0.0
```
Changing weights to custom arrays:
```jldoctest cyclejumps
julia> res_matrix = cycle_jumps(5, 5; jump_weight = .-rand(3), cycle_weight=rand(5))
5×5 Matrix{Float32}:
0.0 0.0 0.0 -0.453905 0.443731
0.434804 0.0 0.0 0.0 0.0
0.0 0.520551 0.0 0.0 0.0
-0.453905 0.0 0.0665751 0.0 0.0
0.0 0.0 0.0 0.57811 0.0
```
Changing sign of the weights with different samplings:
```jldoctest cyclejumps
julia> res_matrix = cycle_jumps(5, 5; cycle_kwargs = (;sampling_type=:bernoulli_sample!))
5×5 Matrix{Float32}:
0.0 0.0 0.0 0.1 0.1
0.1 0.0 0.0 0.0 0.0
0.0 -0.1 0.0 0.0 0.0
0.1 0.0 0.1 0.0 0.0
0.0 0.0 0.0 -0.1 0.0
julia> res_matrix = cycle_jumps(5, 5; jump_kwargs = (;sampling_type=:irrational_sample!))
5×5 Matrix{Float32}:
0.0 0.0 0.0 -0.1 0.1
0.1 0.0 0.0 0.0 0.0
0.0 0.1 0.0 0.0 0.0
-0.1 0.0 0.1 0.0 0.0
0.0 0.0 0.0 0.1 0.0
```
Changing cycle jumps length:
```jldoctest cyclejumps
julia> res_matrix = cycle_jumps(5, 5; jump_size = 2)
5×5 Matrix{Float32}:
0.0 0.0 0.1 0.0 0.1
0.1 0.0 0.0 0.0 0.0
0.1 0.1 0.0 0.0 0.1
0.0 0.0 0.1 0.0 0.0
0.0 0.0 0.1 0.1 0.0
julia> res_matrix = cycle_jumps(5, 5; jump_size = 4)
5×5 Matrix{Float32}:
0.0 0.0 0.0 0.0 0.1
0.1 0.0 0.0 0.0 0.0
0.0 0.1 0.0 0.0 0.0
0.0 0.0 0.1 0.0 0.0
0.1 0.0 0.0 0.1 0.0
```
Return as a sparse matrix:
```jldoctest cyclejumps
julia> using SparseArrays
julia> res_matrix = cycle_jumps(5, 5; return_sparse=true)
5×5 SparseMatrixCSC{Float32, Int64} with 7 stored entries:
⋅ ⋅ ⋅ 0.1 0.1
0.1 ⋅ ⋅ ⋅ ⋅
⋅ 0.1 ⋅ ⋅ ⋅
0.1 ⋅ 0.1 ⋅ ⋅
⋅ ⋅ ⋅ 0.1 ⋅
```
"""
function cycle_jumps(
rng::AbstractRNG, ::Type{T}, dims::Integer...;
cycle_weight::Union{Number, AbstractVector} = T(0.1f0),
jump_weight::Union{Number, AbstractVector} = T(0.1f0),
jump_size::Integer = 3, return_sparse::Bool = false,
radius::Union{AbstractFloat, Nothing} = nothing,
cycle_kwargs::NamedTuple = NamedTuple(),
jump_kwargs::NamedTuple = NamedTuple()
) where {T <: Number}
throw_sparse_error(return_sparse)
check_res_size(dims...)
res_size = first(dims)