From bffced8d17f45c4729b0278d147724a4972cd969 Mon Sep 17 00:00:00 2001 From: Francesco Martinuzzi Date: Wed, 19 May 2021 15:25:13 +0200 Subject: [PATCH 1/6] Added ESNfitted function --- src/echostatenetwork.jl | 60 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/src/echostatenetwork.jl b/src/echostatenetwork.jl index 5f3d9ac2..dac46dec 100644 --- a/src/echostatenetwork.jl +++ b/src/echostatenetwork.jl @@ -225,3 +225,63 @@ function ESNpredict_h_steps(esn::AbstractLeakyESN, end return output end + +""" + ESNfitted(esn::AbstractLeakyESN, W_out::Matrix; autonomous=false) + +Return the prediction for the training data using the trained output layer. The autonomous trigger can be used to have have it return an autonomous prediction starting from the first point if true, or a point by point prediction if false. +""" + +function ESNfitted(esn::AbstractLeakyESN, W_out::Matrix; autonomous=false) + train_len = size(esn.train_data, 2) + output = zeros(Float64, esn.in_size, train_len) + x = zeros(esn.res_size) + + if autonomous + out = esn.train_data[:,1] + return _fitted!(output, esn, x, out) + else + return _fitted!(output, esn, x, esn.train_data) + end +end + +function _fitted!(output, esn, state, vector::Vector) + if esn.extended_states == false + for i=1:train_len + state = ReservoirComputing.leaky_fixed_rnn(esn.activation, esn.alpha, esn.W, esn.W_in, state, vector) + x_new = nla(esn.nla_type, state) + vector = (W_out*x_new) + output[:, i] = vector + end + elseif esn.extended_states == true + for i=1:train_len + state = ReservoirComputing.leaky_fixed_rnn(esn.activation, esn.alpha, esn.W, esn.W_in, state, vector) + x_new = nla(esn.nla_type, state) + vector = (W_out*x_new) + output[:, i] = vector + end + end + return output +end + +function _fitted!(output, esn, state, vector::Matrix) + if esn.extended_states == false + for i=1:train_len + state = ReservoirComputing.leaky_fixed_rnn(esn.activation, esn.alpha, esn.W, esn.W_in, state, vector[:,i]) + x_new = nla(esn.nla_type, state) + out = (W_out*x_new) + output[:, i] = out + end + elseif esn.extended_states == true + for i=1:train_len + state = ReservoirComputing.leaky_fixed_rnn(esn.activation, esn.alpha, esn.W, esn.W_in, state, vector[:,i]) + x_new = nla(esn.nla_type, state) + out = (W_out*x_new) + output[:, i] = out + end + end + return output +end + + + From 3355df3085682e22c63d6c245d1ddf8a7bd7c75d Mon Sep 17 00:00:00 2001 From: Francesco Martinuzzi Date: Wed, 19 May 2021 15:28:00 +0200 Subject: [PATCH 2/6] New version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index ffa51e9a..cd2d94fd 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ReservoirComputing" uuid = "7c2d2b1e-3dd4-11ea-355a-8f6a8116e294" authors = ["Francesco Martinuzzi"] -version = "0.6.2" +version = "0.6.3" [deps] Distances = "b4f34e82-e78d-54a5-968a-f98e89d6e8f7" From df6a0057f5039c66432053fb6dd9d2154155eb46 Mon Sep 17 00:00:00 2001 From: Francesco Martinuzzi Date: Wed, 19 May 2021 15:41:41 +0200 Subject: [PATCH 3/6] Export function, added tests --- src/ReservoirComputing.jl | 2 +- src/echostatenetwork.jl | 8 +++---- test/runtests.jl | 1 + test/training/test_esnfitted.jl | 41 +++++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 test/training/test_esnfitted.jl diff --git a/src/ReservoirComputing.jl b/src/ReservoirComputing.jl index 961679bb..3919757f 100644 --- a/src/ReservoirComputing.jl +++ b/src/ReservoirComputing.jl @@ -28,7 +28,7 @@ include("esn_reservoirs.jl") export init_reservoir_givendeg, init_reservoir_givensp, pseudoSVD, DLR, DLRB, SCR, CRJ include("echostatenetwork.jl") -export ESN, ESNpredict, ESNpredict_h_steps +export ESN, ESNpredict, ESNpredict_h_steps, ESNfitted include("dafesn.jl") export dafESN, dafESNpredict, dafESNpredict_h_steps diff --git a/src/echostatenetwork.jl b/src/echostatenetwork.jl index dac46dec..039bc179 100644 --- a/src/echostatenetwork.jl +++ b/src/echostatenetwork.jl @@ -248,14 +248,14 @@ end function _fitted!(output, esn, state, vector::Vector) if esn.extended_states == false for i=1:train_len - state = ReservoirComputing.leaky_fixed_rnn(esn.activation, esn.alpha, esn.W, esn.W_in, state, vector) + state = leaky_fixed_rnn(esn.activation, esn.alpha, esn.W, esn.W_in, state, vector) x_new = nla(esn.nla_type, state) vector = (W_out*x_new) output[:, i] = vector end elseif esn.extended_states == true for i=1:train_len - state = ReservoirComputing.leaky_fixed_rnn(esn.activation, esn.alpha, esn.W, esn.W_in, state, vector) + state = leaky_fixed_rnn(esn.activation, esn.alpha, esn.W, esn.W_in, state, vector) x_new = nla(esn.nla_type, state) vector = (W_out*x_new) output[:, i] = vector @@ -267,14 +267,14 @@ end function _fitted!(output, esn, state, vector::Matrix) if esn.extended_states == false for i=1:train_len - state = ReservoirComputing.leaky_fixed_rnn(esn.activation, esn.alpha, esn.W, esn.W_in, state, vector[:,i]) + state = leaky_fixed_rnn(esn.activation, esn.alpha, esn.W, esn.W_in, state, vector[:,i]) x_new = nla(esn.nla_type, state) out = (W_out*x_new) output[:, i] = out end elseif esn.extended_states == true for i=1:train_len - state = ReservoirComputing.leaky_fixed_rnn(esn.activation, esn.alpha, esn.W, esn.W_in, state, vector[:,i]) + state = leaky_fixed_rnn(esn.activation, esn.alpha, esn.W, esn.W_in, state, vector[:,i]) x_new = nla(esn.nla_type, state) out = (W_out*x_new) output[:, i] = out diff --git a/test/runtests.jl b/test/runtests.jl index 22b0f7f7..452003c1 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -17,3 +17,4 @@ using SafeTestsets @time @safetestset "reca gol predict" begin include("training/test_recagol.jl") end @time @safetestset "RMM constructors" begin include("constructors/test_rmm_constructors.jl") end @time @safetestset "GRUESN constructors" begin include("constructors/test_gruesn_constructors.jl") end +@time @safetestset "ESN fitted" begin include("training/test_esnfitted.jl") end diff --git a/test/training/test_esnfitted.jl b/test/training/test_esnfitted.jl new file mode 100644 index 00000000..9fce63de --- /dev/null +++ b/test/training/test_esnfitted.jl @@ -0,0 +1,41 @@ +using ReservoirComputing +using MLJLinearModels +#model parameters +const approx_res_size = 30 +const radius = 1.2 +const activation = tanh +const degree = 6 +const sigma = 0.1 +const beta = 0.0 +const alpha = 1.0 +const nla_type = NLADefault() +const in_size = 3 +const out_size = 3 +const extended_states = false +const delta = 0.5 + + +const train_len = 50 +const predict_len = 12 +data = ones(Float64, in_size, 100) +train = data[:, 1:1+train_len-1] +test = data[:, train_len:train_len+predict_len-1] + +#constructor 1 +esn = ESN(approx_res_size, + train, + degree, + radius, + activation = activation, + sigma = sigma, + alpha = alpha, + nla_type = nla_type, + extended_states = extended_states) + +W_out = ESNtrain(esn, beta) + +fit1 = ESNfitted(esn, W_out; autonomous=false) +@test size(fit1) == size(train) + +fit2 = ESNfitted(esn, W_out; autonomous=true) +@test size(fit1) == size(train) From c6dec0d2bfcc687aaac60d197ff733e6916c501d Mon Sep 17 00:00:00 2001 From: Francesco Martinuzzi Date: Wed, 19 May 2021 15:51:03 +0200 Subject: [PATCH 4/6] Small fixes --- src/echostatenetwork.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/echostatenetwork.jl b/src/echostatenetwork.jl index 039bc179..ddd8828b 100644 --- a/src/echostatenetwork.jl +++ b/src/echostatenetwork.jl @@ -239,13 +239,13 @@ function ESNfitted(esn::AbstractLeakyESN, W_out::Matrix; autonomous=false) if autonomous out = esn.train_data[:,1] - return _fitted!(output, esn, x, out) + return _fitted!(output, esn, x, train_len, W_out, out) else - return _fitted!(output, esn, x, esn.train_data) + return _fitted!(output, esn, x, train_len, W_out, esn.train_data) end end -function _fitted!(output, esn, state, vector::Vector) +function _fitted!(output, esn, state, train_len, W_out, vector::Vector) if esn.extended_states == false for i=1:train_len state = leaky_fixed_rnn(esn.activation, esn.alpha, esn.W, esn.W_in, state, vector) @@ -264,7 +264,7 @@ function _fitted!(output, esn, state, vector::Vector) return output end -function _fitted!(output, esn, state, vector::Matrix) +function _fitted!(output, esn, state, train_len, W_out, vector::Matrix) if esn.extended_states == false for i=1:train_len state = leaky_fixed_rnn(esn.activation, esn.alpha, esn.W, esn.W_in, state, vector[:,i]) From 473d06886eaf9f6ccd13c9fd6756950bdb6b5090 Mon Sep 17 00:00:00 2001 From: Francesco Martinuzzi Date: Wed, 19 May 2021 16:01:15 +0200 Subject: [PATCH 5/6] Final tests --- test/extras/test_extended_states.jl | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/extras/test_extended_states.jl b/test/extras/test_extended_states.jl index 4bf51e4f..d0a5a2c4 100644 --- a/test/extras/test_extended_states.jl +++ b/test/extras/test_extended_states.jl @@ -41,6 +41,13 @@ output = ESNpredict(esn, predict_len, W_out) output = ESNpredict_h_steps(esn, predict_len, h_steps, test, W_out) @test size(output) == (out_size, predict_len) +#test esnfitted +fit1 = ESNfitted(esn, W_out; autonomous=false) +@test size(fit1) == size(train) + +fit2 = ESNfitted(esn, W_out; autonomous=true) +@test size(fit1) == size(train) + #test esgp mean = MeanZero() kernel = Lin(1.0) From 6f7647e7246c860235795b20c1541e79b9b5dbd1 Mon Sep 17 00:00:00 2001 From: Francesco Martinuzzi Date: Wed, 19 May 2021 16:20:59 +0200 Subject: [PATCH 6/6] Final Fixes --- src/echostatenetwork.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/echostatenetwork.jl b/src/echostatenetwork.jl index ddd8828b..72cc2ff2 100644 --- a/src/echostatenetwork.jl +++ b/src/echostatenetwork.jl @@ -235,7 +235,7 @@ Return the prediction for the training data using the trained output layer. The function ESNfitted(esn::AbstractLeakyESN, W_out::Matrix; autonomous=false) train_len = size(esn.train_data, 2) output = zeros(Float64, esn.in_size, train_len) - x = zeros(esn.res_size) + x = zeros(size(esn.states, 1)) if autonomous out = esn.train_data[:,1] @@ -255,7 +255,7 @@ function _fitted!(output, esn, state, train_len, W_out, vector::Vector) end elseif esn.extended_states == true for i=1:train_len - state = leaky_fixed_rnn(esn.activation, esn.alpha, esn.W, esn.W_in, state, vector) + state = vcat(leaky_fixed_rnn(esn.activation, esn.alpha, esn.W, esn.W_in, state[1:esn.res_size], vector), vector) x_new = nla(esn.nla_type, state) vector = (W_out*x_new) output[:, i] = vector @@ -274,7 +274,7 @@ function _fitted!(output, esn, state, train_len, W_out, vector::Matrix) end elseif esn.extended_states == true for i=1:train_len - state = leaky_fixed_rnn(esn.activation, esn.alpha, esn.W, esn.W_in, state, vector[:,i]) + state = vcat(leaky_fixed_rnn(esn.activation, esn.alpha, esn.W, esn.W_in, state[1:esn.res_size], vector[:,i]), vector[:,i]) x_new = nla(esn.nla_type, state) out = (W_out*x_new) output[:, i] = out