diff --git a/src/esn/deepesn.jl b/src/esn/deepesn.jl index 3402d2b..5879aa0 100644 --- a/src/esn/deepesn.jl +++ b/src/esn/deepesn.jl @@ -24,6 +24,7 @@ function DeepESN(train_data, 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))), diff --git a/test/esn/test_drivers.jl b/test/esn/test_drivers.jl index 6dd7f7a..b703448 100644 --- a/test/esn/test_drivers.jl +++ b/test/esn/test_drivers.jl @@ -29,12 +29,13 @@ esn_configs = [ Dict(:reservoir => rand_sparse(; radius = 1.2), :reservoir_driver => GRU(variant = Minimal(), reservoir = rand_sparse(; radius = 1.0, sparsity = 0.5), - inner_layer = scaled_rand)), + inner_layer = scaled_rand, + bias = scaled_rand)), Dict(:reservoir => rand_sparse(; radius = 1.2), :reservoir_driver => MRNN(activation_function = (tanh, sigmoid), scaling_factor = (0.8, 0.1))), ] -for config in esn_configs +@testset "Test Drivers: $config" for config in esn_configs test_esn(input_data, target_data, training_method, config) end diff --git a/test/esn/test_nla.jl b/test/esn/test_nla.jl deleted file mode 100644 index f5ac42f..0000000 --- a/test/esn/test_nla.jl +++ /dev/null @@ -1,23 +0,0 @@ -using ReservoirComputing - -states = [1, 2, 3, 4, 5, 6, 7, 8, 9] -nla1_states = [1, 2, 9, 4, 25, 6, 49, 8, 81] -nla2_states = [1, 2, 2, 4, 12, 6, 30, 8, 9] -nla3_states = [1, 2, 8, 4, 24, 6, 48, 8, 9] - -test_types = [Float64, Float32, Float16] - -for tt in test_types - # test default - nla_states = ReservoirComputing.nla(NLADefault(), tt.(states)) - @test nla_states == tt.(states) - # test NLAT1 - nla_states = ReservoirComputing.nla(NLAT1(), tt.(states)) - @test nla_states = tt.(nla1_states) - # test nlat2 - nla_states = ReservoirComputing.nla(NLAT2(), tt.(states)) - @test nla_states = tt.(nla2_states) - # test nlat3 - nla_states = ReservoirComputing.nla(NLAT3(), tt.(states)) - @test nla_states = tt.(nla3_states) -end diff --git a/test/esn/test_states.jl b/test/esn/test_states.jl deleted file mode 100644 index 479d29c..0000000 --- a/test/esn/test_states.jl +++ /dev/null @@ -1,46 +0,0 @@ -using ReservoirComputing - -test_types = [Float64, Float32, Float16] -states = [1, 2, 3, 4, 5, 6, 7, 8, 9] -in_data = fill(1, 3) - -states_types = [StandardStates, ExtendedStates, PaddedStates, PaddedExtendedStates] - -# testing extension and padding -for tt in test_types - st_states = StandardStates()(NLADefault(), tt.(states), tt.(in_data)) - @test length(st_states) == length(states) - @test typeof(st_states) == typeof(tt.(states)) - - st_states = ExtendedStates()(NLADefault(), tt.(states), tt.(in_data)) - @test length(st_states) == length(states) + length(in_data) - @test typeof(st_states) == typeof(tt.(states)) - - st_states = PaddedStates()(NLADefault(), tt.(states), tt.(in_data)) - @test length(st_states) == length(states) + 1 - @test typeof(st_states[1]) == typeof(tt.(states)[1]) - - st_states = PaddedExtendedStates()(NLADefault(), tt.(states), tt.(in_data)) - @test length(st_states) == length(states) + length(in_data) + 1 - @test typeof(st_states[1]) == typeof(tt.(states)[1]) -end - -## testing non linear algos -nla1_states = [1, 2, 9, 4, 25, 6, 49, 8, 81] -nla2_states = [1, 2, 2, 4, 12, 6, 30, 8, 9] -nla3_states = [1, 2, 8, 4, 24, 6, 48, 8, 9] - -for tt in test_types - # test default - nla_states = ReservoirComputing.nla(NLADefault(), tt.(states)) - @test nla_states == tt.(states) - # test NLAT1 - nla_states = ReservoirComputing.nla(NLAT1(), tt.(states)) - @test nla_states == tt.(nla1_states) - # test nlat2 - nla_states = ReservoirComputing.nla(NLAT2(), tt.(states)) - @test nla_states == tt.(nla2_states) - # test nlat3 - nla_states = ReservoirComputing.nla(NLAT3(), tt.(states)) - @test nla_states == tt.(nla3_states) -end diff --git a/test/esn/test_train.jl b/test/esn/test_train.jl index e5140b9..a0f6a4c 100644 --- a/test/esn/test_train.jl +++ b/test/esn/test_train.jl @@ -9,10 +9,12 @@ const input_data = reduce(hcat, data[1:(train_len - 1)]) const target_data = reduce(hcat, data[2:train_len]) const test = reduce(hcat, data[(train_len + 1):(train_len + predict_len)]) const reg = 10e-6 +#test_types = [Float64, Float32, Float16] Random.seed!(77) -esn = ESN(input_data; - reservoir = RandSparseReservoir(res_size, 1.2, 0.1)) +res = rand_sparse(; radius=1.2, sparsity=0.1) +esn = ESN(input_data, 1, res_size; + reservoir = rand_sparse) training_methods = [ StandardRidge(regularization_coeff = reg), @@ -21,14 +23,9 @@ training_methods = [ EpsilonSVR(), ] -for t in training_methods - output_layer = train(esn, target_data, t) +# TODO check types +@testset "Training Algo Tests: $ta" for ta in training_methods + output_layer = train(esn, target_data, ta) output = esn(Predictive(input_data), output_layer) @test mean(abs.(target_data .- output)) ./ mean(abs.(target_data)) < 0.22 end - -for t in training_methods - output_layer = train(esn, target_data, t) - output, states = esn(Predictive(input_data), output_layer, save_states = true) - @test size(states) == (res_size, size(input_data, 2)) -end diff --git a/test/test_states.jl b/test/test_states.jl index 1215d47..3457066 100644 --- a/test/test_states.jl +++ b/test/test_states.jl @@ -3,6 +3,7 @@ using ReservoirComputing test_array = [1, 2, 3, 4, 5, 6, 7, 8, 9] extension = [0, 0, 0] padding = 10.0 +test_types = [Float64, Float32, Float16] nlas = [(NLADefault(), test_array), (NLAT1(), [1, 2, 9, 4, 25, 6, 49, 8, 81]), @@ -18,20 +19,15 @@ pes = [(StandardStates(), test_array), 1)), (ExtendedStates(), vcat(extension, test_array))] -function test_nla(algo, expected_output) - nla_array = ReservoirComputing.nla(algo, test_array) - @test nla_array == expected_output -end - -function test_states_type(state_type, expected_output) - states_output = state_type(NLADefault(), test_array, extension) - @test states_output == expected_output -end - -@testset "Nonlinear Algorithms Testing" for (algo, expected_output) in nlas - test_nla(algo, expected_output) -end - -@testset "States Testing" for (state_type, expected_output) in pes - test_states_type(state_type, expected_output) -end +@testset "States Testing" for T in test_types + @testset "Nonlinear Algorithms Testing: $algo $T" for (algo, expected_output) in nlas + nla_array = ReservoirComputing.nla(algo, T.(test_array)) + @test nla_array == expected_output + @test eltype(nla_array) == T + end + @testset "States Testing: $state_type $T" for (state_type, expected_output) in pes + states_output = state_type(NLADefault(), T.(test_array), T.(extension)) + @test states_output == expected_output + @test eltype(states_output) == T + end +end \ No newline at end of file