Skip to content

Commit

Permalink
small changes to inits, start of streamline
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinuzziFrancesco committed Feb 4, 2024
1 parent 6ecceb2 commit 4c3925b
Show file tree
Hide file tree
Showing 18 changed files with 394 additions and 478 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Now that we have the data we can initialize the ESN with the chosen parameters.
input_size = 3
res_size = 300
esn = ESN(input_data, input_size, res_size;
reservoir = rand_sparse(;radius = 1.2, sparsity = 6 / res_size),
reservoir = rand_sparse(; radius = 1.2, sparsity = 6 / res_size),
input_layer = weighted_init,
nla_type = NLAT2())
```
Expand Down Expand Up @@ -104,6 +104,7 @@ If you use this library in your work, please cite:
url = {http://jmlr.org/papers/v23/22-0611.html}
}
```

## Acknowledgements

This project was possible thanks to initial funding through the [Google summer of code](https://summerofcode.withgoogle.com/) 2020 program. Francesco M. further acknowledges [ScaDS.AI](https://scads.ai/) and [RSC4Earth](https://rsc4earth.de/) for supporting the current progress on the library.
3 changes: 1 addition & 2 deletions docs/src/esn_tutorials/hybrid.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ km = KnowledgeModel(prior_model_data_generator, u0, tspan_train, train_len)
in_size = 3
res_size = 300
hesn = HybridESN(
km,
hesn = HybridESN(km,
input_data,
in_size,
res_size;
Expand Down
4 changes: 2 additions & 2 deletions docs/src/esn_tutorials/lorenz_basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ input_scaling = 0.1
#build ESN struct
esn = ESN(input_data, in_size, res_size;
reservoir = rand_sparse(;radius = res_radius, sparsity = res_sparsity),
input_layer = weighted_init(;scaling = input_scaling),
reservoir = rand_sparse(; radius = res_radius, sparsity = res_sparsity),
input_layer = weighted_init(; scaling = input_scaling),
reservoir_driver = RNN(),
nla_type = NLADefault(),
states_type = StandardStates())
Expand Down
8 changes: 5 additions & 3 deletions src/ReservoirComputing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export NLADefault, NLAT1, NLAT2, NLAT3
export StandardStates, ExtendedStates, PaddedStates, PaddedExtendedStates
export StandardRidge, LinearModel
export AbstractLayer, create_layer
export scaled_rand, weighted_init, sparse_layer, informed_layer, bernoulli_sample_layer, irrational_sample_layer
export rand_sparse, delay_line, delay_line_backward_reservoir, cycle_jumps_reservoir, simple_cycle_reservoir, pseudo_svd_reservoir
export scaled_rand, weighted_init, sparse_init, informed_init, minimal_init
export rand_sparse, delay_line, delay_line_backward, cycle_jumps, simple_cycle, pseudo_svd
export RNN, MRNN, GRU, GRUParams, FullyGated, Minimal
export ESN, train
export HybridESN, KnowledgeModel
Expand Down Expand Up @@ -75,7 +75,9 @@ function Predictive(prediction_data)
end

#fallbacks for initializers
for initializer in (:rand_sparse, :delay_line, :delay_line_backward_reservoir, :cycle_jumps_reservoir, :simple_cycle_reservoir, :pseudo_svd_reservoir, :scaled_rand, :weighted_init, :sparse_layer, :informed_layer, :bernoulli_sample_layer, :irrational_sample_layer)
for initializer in (:rand_sparse, :delay_line, :delay_line_backward, :cycle_jumps,
:simple_cycle, :pseudo_svd,
:scaled_rand, :weighted_init, :sparse_init, :informed_init, :minimal_init)
NType = ifelse(initializer === :rand_sparse, Real, Number)
@eval function ($initializer)(dims::Integer...; kwargs...)
return $initializer(_default_rng(), Float32, dims...; kwargs...)
Expand Down
92 changes: 44 additions & 48 deletions src/esn/deepesn.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
struct DeepESN{I, S, V, N, T, O, M, B, ST, W, IS} <: AbstractEchoStateNetwork
struct DeepESN{I, S, N, T, O, M, B, ST, W, IS} <: AbstractEchoStateNetwork
res_size::I
train_data::S
variation::V
nla_type::N
input_matrix::T
reservoir_driver::O
Expand All @@ -12,22 +11,19 @@ struct DeepESN{I, S, V, N, T, O, M, B, ST, W, IS} <: AbstractEchoStateNetwork
states::IS
end

function DeepESN(
train_data,
in_size::Int,
res_size::AbstractArray;
input_layer = scaled_rand,
reservoir = rand_sparse,
bias = zeros64,
reservoir_driver = RNN(),
nla_type = NLADefault(),
states_type = StandardStates(),
washout = 0,
rng = _default_rng(),
T=Float64,
matrix_type = typeof(train_data)
)

function DeepESN(train_data,
in_size::Int,
res_size::AbstractArray;
input_layer = scaled_rand,
reservoir = rand_sparse,
bias = zeros64,
reservoir_driver = RNN(),
nla_type = NLADefault(),
states_type = StandardStates(),
washout = 0,
rng = _default_rng(),
T = Float64,
matrix_type = typeof(train_data))
if states_type isa AbstractPaddedStates
in_size = size(train_data, 1) + 1
train_data = vcat(Adapt.adapt(matrix_type, ones(1, size(train_data, 2))),
Expand All @@ -42,43 +38,43 @@ function DeepESN(
input_matrix, bias_vector)
train_data = train_data[:, (washout + 1):end]

ESN(sum(res_size), train_data, variation, nla_type, input_matrix,
DeepESN(sum(res_size), train_data, variation, nla_type, input_matrix,
inner_res_driver, reservoir_matrix, bias_vector, states_type, washout,
states)
end

function obtain_layers(in_size,
input_layer,
reservoir::Vector,
bias;
matrix_type = Matrix{Float64})
esn_depth = length(reservoir)
input_res_sizes = [get_ressize(reservoir[i]) for i in 1:esn_depth]
in_sizes = zeros(Int, esn_depth)
in_sizes[2:end] = input_res_sizes[1:(end - 1)]
in_sizes[1] = in_size
input_layer,
reservoir::Vector,
bias;
matrix_type = Matrix{Float64})
esn_depth = length(reservoir)
input_res_sizes = [get_ressize(reservoir[i]) for i in 1:esn_depth]
in_sizes = zeros(Int, esn_depth)
in_sizes[2:end] = input_res_sizes[1:(end - 1)]
in_sizes[1] = in_size

if input_layer isa Array
input_matrix = [create_layer(input_layer[j], input_res_sizes[j], in_sizes[j],
matrix_type = matrix_type) for j in 1:esn_depth]
else
_input_layer = fill(input_layer, esn_depth)
input_matrix = [create_layer(_input_layer[k], input_res_sizes[k], in_sizes[k],
matrix_type = matrix_type) for k in 1:esn_depth]
end

if input_layer isa Array
input_matrix = [create_layer(input_layer[j], input_res_sizes[j], in_sizes[j],
matrix_type = matrix_type) for j in 1:esn_depth]
else
_input_layer = fill(input_layer, esn_depth)
input_matrix = [create_layer(_input_layer[k], input_res_sizes[k], in_sizes[k],
res_sizes = [get_ressize(input_matrix[j]) for j in 1:esn_depth]
reservoir_matrix = [create_reservoir(reservoir[k], res_sizes[k],
matrix_type = matrix_type) for k in 1:esn_depth]
end

res_sizes = [get_ressize(input_matrix[j]) for j in 1:esn_depth]
reservoir_matrix = [create_reservoir(reservoir[k], res_sizes[k],
matrix_type = matrix_type) for k in 1:esn_depth]
if bias isa Array
bias_vector = [create_layer(bias[j], res_sizes[j], 1, matrix_type = matrix_type)
for j in 1:esn_depth]
else
_bias = fill(bias, esn_depth)
bias_vector = [create_layer(_bias[k], res_sizes[k], 1, matrix_type = matrix_type)
for k in 1:esn_depth]
end

if bias isa Array
bias_vector = [create_layer(bias[j], res_sizes[j], 1, matrix_type = matrix_type)
for j in 1:esn_depth]
else
_bias = fill(bias, esn_depth)
bias_vector = [create_layer(_bias[k], res_sizes[k], 1, matrix_type = matrix_type)
for k in 1:esn_depth]
return input_matrix, reservoir_matrix, bias_vector, res_sizes
end

return input_matrix, reservoir_matrix, bias_vector, res_sizes
end
31 changes: 14 additions & 17 deletions src/esn/esn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,27 @@ train_data = rand(10, 100) # 10 features, 100 time steps
esn = ESN(train_data, reservoir=RandSparseReservoir(200), washout=10)
```
"""
function ESN(
train_data,
in_size::Int,
res_size::Int;
input_layer = scaled_rand,
reservoir = rand_sparse,
bias = zeros64,
reservoir_driver = RNN(),
nla_type = NLADefault(),
states_type = StandardStates(),
washout = 0,
rng = _default_rng(),
T = Float32,
matrix_type = typeof(train_data)
)

function ESN(train_data,
in_size::Int,
res_size::Int;
input_layer = scaled_rand,
reservoir = rand_sparse,
bias = zeros64,
reservoir_driver = RNN(),
nla_type = NLADefault(),
states_type = StandardStates(),
washout = 0,
rng = _default_rng(),
T = Float32,
matrix_type = typeof(train_data))
if states_type isa AbstractPaddedStates
in_size = size(train_data, 1) + 1
train_data = vcat(Adapt.adapt(matrix_type, ones(1, size(train_data, 2))),
train_data)
end

reservoir_matrix = reservoir(rng, T, res_size, res_size)
input_matrix = input_layer(rng, T, in_size, res_size)
input_matrix = input_layer(rng, T, res_size, in_size)
bias_vector = bias(rng, res_size)
inner_res_driver = reservoir_driver_params(reservoir_driver, res_size, in_size)
states = create_states(inner_res_driver, train_data, washout, reservoir_matrix,
Expand Down
Loading

0 comments on commit 4c3925b

Please sign in to comment.