From 7c50ee25eb3cc5a43b7b6b3bcb5d36a3a3cf2fde Mon Sep 17 00:00:00 2001 From: MartinuzziFrancesco Date: Mon, 6 Jan 2025 18:23:19 +0100 Subject: [PATCH] changed all defaults to Float32 --- src/esn/deepesn.jl | 8 +++----- src/esn/esn.jl | 4 ++-- src/esn/hybridesn.jl | 2 +- test/esn/deepesn.jl | 21 +++++++++++++-------- 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/esn/deepesn.jl b/src/esn/deepesn.jl index d5957acf..7ce458fd 100644 --- a/src/esn/deepesn.jl +++ b/src/esn/deepesn.jl @@ -37,7 +37,7 @@ temporal features. - `input_layer`: A function or an array of functions to initialize the input matrices for each layer. Default is `scaled_rand` for each layer. - `bias`: A function or an array of functions to initialize the bias vectors - for each layer. Default is `zeros64` for each layer. + for each layer. Default is `zeros32` for each layer. - `reservoir`: A function or an array of functions to initialize the reservoir matrices for each layer. Default is `rand_sparse` for each layer. - `reservoir_driver`: The driving system for the reservoir. @@ -50,8 +50,6 @@ temporal features. Default is 0. - `rng`: Random number generator used for initializing weights. Default is the package's default random number generator. - - `T`: The data type for the matrices (e.g., `Float64`). Influences computational - efficiency and precision. - `matrix_type`: The type of matrix used for storing the training data. Default is inferred from `train_data`. @@ -74,14 +72,13 @@ function DeepESN(train_data, res_size::Int; depth::Int=2, input_layer=fill(scaled_rand, depth), - bias=fill(zeros64, depth), + bias=fill(zeros32, depth), reservoir=fill(rand_sparse, depth), reservoir_driver=RNN(), nla_type=NLADefault(), states_type=StandardStates(), washout::Int=0, rng=Utils.default_rng(), - T=Float64, matrix_type=typeof(train_data)) if states_type isa AbstractPaddedStates in_size = size(train_data, 1) + 1 @@ -89,6 +86,7 @@ function DeepESN(train_data, train_data) end + T = eltype(train_data) reservoir_matrix = [reservoir[i](rng, T, res_size, res_size) for i in 1:depth] input_matrix = [i == 1 ? input_layer[i](rng, T, res_size, in_size) : input_layer[i](rng, T, res_size, res_size) for i in 1:depth] diff --git a/src/esn/esn.jl b/src/esn/esn.jl index 64d5d7b2..b585db2a 100644 --- a/src/esn/esn.jl +++ b/src/esn/esn.jl @@ -49,13 +49,12 @@ function ESN(train_data, res_size::Int; input_layer=scaled_rand, reservoir=rand_sparse, - bias=zeros64, + bias=zeros32, reservoir_driver=RNN(), nla_type=NLADefault(), states_type=StandardStates(), washout=0, rng=Utils.default_rng(), - T=Float32, matrix_type=typeof(train_data)) if states_type isa AbstractPaddedStates in_size = size(train_data, 1) + 1 @@ -63,6 +62,7 @@ function ESN(train_data, train_data) end + T = eltype(train_data) reservoir_matrix = reservoir(rng, T, res_size, res_size) input_matrix = input_layer(rng, T, res_size, in_size) bias_vector = bias(rng, res_size) diff --git a/src/esn/hybridesn.jl b/src/esn/hybridesn.jl index 172d719f..13c8b642 100644 --- a/src/esn/hybridesn.jl +++ b/src/esn/hybridesn.jl @@ -121,7 +121,7 @@ function HybridESN(model, res_size::Int; input_layer=scaled_rand, reservoir=rand_sparse, - bias=zeros64, + bias=zeros32, reservoir_driver=RNN(), nla_type=NLADefault(), states_type=StandardStates(), diff --git a/test/esn/deepesn.jl b/test/esn/deepesn.jl index 67b3e9e5..5d89cef6 100644 --- a/test/esn/deepesn.jl +++ b/test/esn/deepesn.jl @@ -7,14 +7,19 @@ const train_len = 400 const predict_len = 100 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 test_data = reduce(hcat, data[(train_len + 1):(train_len + predict_len)]) const reg = 10e-6 -#test_types = [Float64, Float32, Float16] -Random.seed!(77) -res = rand_sparse(; radius=1.2, sparsity=0.1) -esn = DeepESN(input_data, 1, res_size) +test_types = [Float64, Float32, Float16] +zeros_types = [zeros64, zeros32, zeros16] -output_layer = train(esn, target_data) -output = esn(Generative(length(test)), output_layer) -@test mean(abs.(test .- output)) ./ mean(abs.(test)) < 0.22 +for (tidx,t) in enumerate(test_types) + Random.seed!(77) + res = rand_sparse(; radius=1.2, sparsity=0.1) + esn = DeepESN(t.(input_data), 1, res_size; + bias=fill(zeros_types[tidx], 2)) + + output_layer = train(esn, t.(target_data)) + output = esn(Generative(length(test_data)), output_layer) + @test eltype(output) == t +end \ No newline at end of file