From 7a085842f6c0379aa76a52d37f8550bdf7f35b21 Mon Sep 17 00:00:00 2001 From: Matthieu Zimmer Date: Fri, 16 May 2014 19:24:17 +0200 Subject: [PATCH 1/3] portage to eigen3 seems ok --- af.hpp | 9 ++-- esn.hpp | 6 +-- esn_2.hpp | 7 +-- io_trait.hpp | 4 -- pf.hpp | 14 ++---- test_dnn.cpp | 50 +++++++++--------- test_dnn_ff.cpp | 42 ++++++++-------- test_hyper_nn.cpp | 126 +++++++++++++++++++++++----------------------- trait.hpp | 2 - wscript | 16 +++--- 10 files changed, 134 insertions(+), 142 deletions(-) diff --git a/af.hpp b/af.hpp index fb9b6aa..f98af91 100644 --- a/af.hpp +++ b/af.hpp @@ -36,6 +36,7 @@ #define _NN_AF_HPP_ #include "params.hpp" +#include // classic activation functions namespace nn @@ -59,7 +60,7 @@ namespace nn struct AfTanh : public Af

{ typedef P params_t; - static const float lambda = 5.0f; + SFERES_CONST float lambda = 5.0f; AfTanh() { assert(trait

::size(this->_params) == 1); } float operator()(float p) const { @@ -72,7 +73,7 @@ namespace nn struct AfTanhNoBias : public Af

{ typedef params::Dummy params_t; - static const float lambda = 5.0f; + SFERES_CONST float lambda = 5.0f; AfTanhNoBias() { } float operator()(float p) const { @@ -86,7 +87,7 @@ namespace nn struct AfSigmoidNoBias : public Af<> { typedef params::Dummy params_t; - static const float lambda = 5.0f; + SFERES_CONST float lambda = 5.0f; AfSigmoidNoBias() { } float operator()(float p) const { return 1.0 / (exp(-p * lambda) + 1); } protected: @@ -96,7 +97,7 @@ namespace nn struct AfSigmoidBias : public Af

{ typedef P params_t; - static const float lambda = 5.0f; + SFERES_CONST float lambda = 5.0f; AfSigmoidBias() { assert(this->_params.size() == 1); } float operator()(float p) const { diff --git a/esn.hpp b/esn.hpp index 42fdd1d..2e7b557 100644 --- a/esn.hpp +++ b/esn.hpp @@ -39,8 +39,8 @@ #define _NN_ESN_HPP_ #include -#include #include +#include #include @@ -93,8 +93,8 @@ namespace nn else conn(i, j) = 0.0f; // spectral radius - float max = (conn.eigenvalues().real().cwise().square() - + conn.eigenvalues().imag().cwise().square() + float max = (conn.eigenvalues().real().array().square() + + conn.eigenvalues().imag().array().square() ).maxCoeff(); assert(max > 0); conn *= alpha / sqrtf(max); diff --git a/esn_2.hpp b/esn_2.hpp index ede5419..3a706a7 100644 --- a/esn_2.hpp +++ b/esn_2.hpp @@ -39,8 +39,9 @@ #define _NN_ESN_HPP_ #include -#include #include +#include + #include #include @@ -164,8 +165,8 @@ namespace nn if (sferes::misc::rand() < alpha) conn(i, j) = sferes::misc::rand(-1.0f, 1.0f); // spectral radius - float max = (conn.eigenvalues().real().cwise().square() - + conn.eigenvalues().imag().cwise().square() + float max = (conn.eigenvalues().real().array().square() + + conn.eigenvalues().imag().array().square() ).maxCoeff(); assert(max > 0); conn *= alpha / sqrtf(max); diff --git a/io_trait.hpp b/io_trait.hpp index 6e618b0..b44da2a 100644 --- a/io_trait.hpp +++ b/io_trait.hpp @@ -42,9 +42,7 @@ #include #include -#ifdef EIGEN2_ENABLED #include -#endif namespace nn { @@ -57,7 +55,6 @@ namespace nn typedef std::valarray vector_t; }; -#ifdef EIGEN2_ENABLED // go with eigen with float (TODO : double) template<> struct io_trait @@ -67,7 +64,6 @@ namespace nn static vector_t zero(size_t k) { return Eigen::VectorXf::Zero(k); } }; -#endif template<> struct io_trait > diff --git a/pf.hpp b/pf.hpp index fb14ab4..cf9aa8f 100644 --- a/pf.hpp +++ b/pf.hpp @@ -38,6 +38,7 @@ #include #include +#include #include "params.hpp" #include "trait.hpp" @@ -86,12 +87,7 @@ namespace nn //_w_cache.dot(inputs)< no vectorization of pwfsum" - return (_w_cache * inputs).sum(); -#endif + return inputs.dot(_w_cache); } protected: trait::vector_t _w_cache; @@ -112,9 +108,9 @@ namespace nn { typedef std::pair weight_t; typedef P params_t; - static const float dt = 0.01; - static const float a_r = 20.0f; - static const float a_x = 20.0f; + SFERES_CONST float dt = 0.01; + SFERES_CONST float a_r = 20.0f; + SFERES_CONST float a_x = 20.0f; void set_r(float r) { _r = r; } void set_x(float x) { _x = x; } void set_omega(float o) { _omega = o; } diff --git a/test_dnn.cpp b/test_dnn.cpp index 8c8c5a4..0ccdd4c 100644 --- a/test_dnn.cpp +++ b/test_dnn.cpp @@ -112,39 +112,39 @@ struct Params { struct evo_float { - static const float mutation_rate = 0.1f; - static const float cross_rate = 0.1f; - static const mutation_t mutation_type = polynomial; - static const cross_over_t cross_over_type = sbx; - static const float eta_m = 15.0f; - static const float eta_c = 15.0f; + SFERES_CONST float mutation_rate = 0.1f; + SFERES_CONST float cross_rate = 0.1f; + SFERES_CONST mutation_t mutation_type = polynomial; + SFERES_CONST cross_over_t cross_over_type = sbx; + SFERES_CONST float eta_m = 15.0f; + SFERES_CONST float eta_c = 15.0f; }; struct parameters { // maximum value of parameters - static const float min = -5.0f; + SFERES_CONST float min = -5.0f; // minimum value - static const float max = 5.0f; + SFERES_CONST float max = 5.0f; }; struct dnn { - static const size_t nb_inputs = 4; - static const size_t nb_outputs = 1; - static const size_t min_nb_neurons = 4; - static const size_t max_nb_neurons = 5; - static const size_t min_nb_conns = 100; - static const size_t max_nb_conns = 101; - static const float max_weight = 2.0f; - static const float max_bias = 2.0f; - - static const float m_rate_add_conn = 1.0f; - static const float m_rate_del_conn = 1.0f; - static const float m_rate_change_conn = 1.0f; - static const float m_rate_add_neuron = 1.0f; - static const float m_rate_del_neuron = 1.0f; - - static const int io_param_evolving = true; - static const init_t init = random_topology; + SFERES_CONST size_t nb_inputs = 4; + SFERES_CONST size_t nb_outputs = 1; + SFERES_CONST size_t min_nb_neurons = 4; + SFERES_CONST size_t max_nb_neurons = 5; + SFERES_CONST size_t min_nb_conns = 100; + SFERES_CONST size_t max_nb_conns = 101; + SFERES_CONST float max_weight = 2.0f; + SFERES_CONST float max_bias = 2.0f; + + SFERES_CONST float m_rate_add_conn = 1.0f; + SFERES_CONST float m_rate_del_conn = 1.0f; + SFERES_CONST float m_rate_change_conn = 1.0f; + SFERES_CONST float m_rate_add_neuron = 1.0f; + SFERES_CONST float m_rate_del_neuron = 1.0f; + + SFERES_CONST int io_param_evolving = true; + SFERES_CONST init_t init = random_topology; }; }; diff --git a/test_dnn_ff.cpp b/test_dnn_ff.cpp index 6a116fe..1c253fe 100644 --- a/test_dnn_ff.cpp +++ b/test_dnn_ff.cpp @@ -60,37 +60,37 @@ struct Params { struct evo_float { - static const float mutation_rate = 0.1f; - static const float cross_rate = 0.1f; - static const mutation_t mutation_type = polynomial; - static const cross_over_t cross_over_type = sbx; - static const float eta_m = 15.0f; - static const float eta_c = 15.0f; + SFERES_CONST float mutation_rate = 0.1f; + SFERES_CONST float cross_rate = 0.1f; + SFERES_CONST mutation_t mutation_type = polynomial; + SFERES_CONST cross_over_t cross_over_type = sbx; + SFERES_CONST float eta_m = 15.0f; + SFERES_CONST float eta_c = 15.0f; }; struct parameters { // maximum value of parameters - static const float min = -5.0f; + SFERES_CONST float min = -5.0f; // minimum value - static const float max = 5.0f; + SFERES_CONST float max = 5.0f; }; struct dnn { - static const size_t nb_inputs = 4; - static const size_t nb_outputs = 2; - static const size_t min_nb_neurons = 4; - static const size_t max_nb_neurons = 5; - static const size_t min_nb_conns = 100; - static const size_t max_nb_conns = 101; + SFERES_CONST size_t nb_inputs = 4; + SFERES_CONST size_t nb_outputs = 2; + SFERES_CONST size_t min_nb_neurons = 4; + SFERES_CONST size_t max_nb_neurons = 5; + SFERES_CONST size_t min_nb_conns = 100; + SFERES_CONST size_t max_nb_conns = 101; - static const float m_rate_add_conn = 1.0f; - static const float m_rate_del_conn = 0.1f; - static const float m_rate_change_conn = 1.0f; - static const float m_rate_add_neuron = 1.0f; - static const float m_rate_del_neuron = 1.0f; + SFERES_CONST float m_rate_add_conn = 1.0f; + SFERES_CONST float m_rate_del_conn = 0.1f; + SFERES_CONST float m_rate_change_conn = 1.0f; + SFERES_CONST float m_rate_add_neuron = 1.0f; + SFERES_CONST float m_rate_del_neuron = 1.0f; - static const int io_param_evolving = true; - static const init_t init = ff; + SFERES_CONST int io_param_evolving = true; + SFERES_CONST init_t init = ff; }; }; diff --git a/test_hyper_nn.cpp b/test_hyper_nn.cpp index 62cde61..35388ed 100644 --- a/test_hyper_nn.cpp +++ b/test_hyper_nn.cpp @@ -52,30 +52,30 @@ struct Params1 { struct dnn { - static const size_t nb_inputs = 3; - static const size_t nb_outputs = 1; + SFERES_CONST size_t nb_inputs = 3; + SFERES_CONST size_t nb_outputs = 1; - static const float m_rate_add_conn = 1.0f; - static const float m_rate_del_conn = 0.3f; - static const float m_rate_change_conn = 1.0f; - static const float m_rate_add_neuron = 1.0f; - static const float m_rate_del_neuron = 0.2f; + SFERES_CONST float m_rate_add_conn = 1.0f; + SFERES_CONST float m_rate_del_conn = 0.3f; + SFERES_CONST float m_rate_change_conn = 1.0f; + SFERES_CONST float m_rate_add_neuron = 1.0f; + SFERES_CONST float m_rate_del_neuron = 0.2f; - static const init_t init = ff; + SFERES_CONST init_t init = ff; }; struct evo_float { - static const float mutation_rate = 0.1f; - static const float cross_rate = 0.1f; - static const mutation_t mutation_type = polynomial; - static const cross_over_t cross_over_type = sbx; - static const float eta_m = 15.0f; - static const float eta_c = 15.0f; + SFERES_CONST float mutation_rate = 0.1f; + SFERES_CONST float cross_rate = 0.1f; + SFERES_CONST mutation_t mutation_type = polynomial; + SFERES_CONST cross_over_t cross_over_type = sbx; + SFERES_CONST float eta_m = 15.0f; + SFERES_CONST float eta_c = 15.0f; }; struct parameters { - static const float min = -2.0f; - static const float max = 2.0f; + SFERES_CONST float min = -2.0f; + SFERES_CONST float max = 2.0f; }; struct cppn @@ -84,18 +84,18 @@ struct Params1 struct sampled { SFERES_ARRAY(float, values, 0, 1, 2); - static const float mutation_rate = 0.1f; - static const float cross_rate = 0.25f; - static const bool ordered = false; + SFERES_CONST float mutation_rate = 0.1f; + SFERES_CONST float cross_rate = 0.25f; + SFERES_CONST bool ordered = false; }; struct evo_float { - static const float mutation_rate = 0.1f; - static const float cross_rate = 0.1f; - static const mutation_t mutation_type = polynomial; - static const cross_over_t cross_over_type = sbx; - static const float eta_m = 15.0f; - static const float eta_c = 15.0f; + SFERES_CONST float mutation_rate = 0.1f; + SFERES_CONST float cross_rate = 0.1f; + SFERES_CONST mutation_t mutation_type = polynomial; + SFERES_CONST cross_over_t cross_over_type = sbx; + SFERES_CONST float eta_m = 15.0f; + SFERES_CONST float eta_c = 15.0f; }; }; }; @@ -105,34 +105,34 @@ struct Params2 { struct dnn { - static const size_t nb_inputs = 4; - static const size_t nb_outputs = 1; - - static const float m_rate_add_conn = 1.0f; - static const float m_rate_del_conn = 0.3f; - static const float m_rate_change_conn = 1.0f; - static const float m_rate_add_neuron = 1.0f; - static const float m_rate_del_neuron = 0.2f; - - static const float weight_sigma = 0.5f; - static const float vect_sigma = 0.5f; - static const float m_rate_weight = 1.0f; - static const float m_rate_fparams = 1.0f; - static const init_t init = ff; + SFERES_CONST size_t nb_inputs = 4; + SFERES_CONST size_t nb_outputs = 1; + + SFERES_CONST float m_rate_add_conn = 1.0f; + SFERES_CONST float m_rate_del_conn = 0.3f; + SFERES_CONST float m_rate_change_conn = 1.0f; + SFERES_CONST float m_rate_add_neuron = 1.0f; + SFERES_CONST float m_rate_del_neuron = 0.2f; + + SFERES_CONST float weight_sigma = 0.5f; + SFERES_CONST float vect_sigma = 0.5f; + SFERES_CONST float m_rate_weight = 1.0f; + SFERES_CONST float m_rate_fparams = 1.0f; + SFERES_CONST init_t init = ff; }; struct evo_float { - static const float mutation_rate = 0.1f; - static const float cross_rate = 0.1f; - static const mutation_t mutation_type = polynomial; - static const cross_over_t cross_over_type = sbx; - static const float eta_m = 15.0f; - static const float eta_c = 15.0f; + SFERES_CONST float mutation_rate = 0.1f; + SFERES_CONST float cross_rate = 0.1f; + SFERES_CONST mutation_t mutation_type = polynomial; + SFERES_CONST cross_over_t cross_over_type = sbx; + SFERES_CONST float eta_m = 15.0f; + SFERES_CONST float eta_c = 15.0f; }; struct parameters { - static const float min = -2.0f; - static const float max = 2.0f; + SFERES_CONST float min = -2.0f; + SFERES_CONST float max = 2.0f; }; struct cppn @@ -141,18 +141,18 @@ struct Params2 struct sampled { SFERES_ARRAY(float, values, 0, 1, 2); - static const float mutation_rate = 0.1f; - static const float cross_rate = 0.25f; - static const bool ordered = false; + SFERES_CONST float mutation_rate = 0.1f; + SFERES_CONST float cross_rate = 0.25f; + SFERES_CONST bool ordered = false; }; struct evo_float { - static const float mutation_rate = 0.1f; - static const float cross_rate = 0.1f; - static const mutation_t mutation_type = polynomial; - static const cross_over_t cross_over_type = sbx; - static const float eta_m = 15.0f; - static const float eta_c = 15.0f; + SFERES_CONST float mutation_rate = 0.1f; + SFERES_CONST float cross_rate = 0.1f; + SFERES_CONST mutation_t mutation_type = polynomial; + SFERES_CONST cross_over_t cross_over_type = sbx; + SFERES_CONST float eta_m = 15.0f; + SFERES_CONST float eta_c = 15.0f; }; }; struct hyper_nn @@ -168,13 +168,13 @@ struct Params2 ); SFERES_ARRAY(float, weights, -1, 0, 1); SFERES_ARRAY(float, bias, -1, 0, 1); - static const size_t nb_inputs = 2; - static const size_t nb_outputs = 1; - static const size_t nb_hidden = 4; - static const size_t nb_pfparams = 0; - static const size_t nb_afparams = 1; - static const float conn_threshold = 0.2f; - static const float max_y = 10.0f; + SFERES_CONST size_t nb_inputs = 2; + SFERES_CONST size_t nb_outputs = 1; + SFERES_CONST size_t nb_hidden = 4; + SFERES_CONST size_t nb_pfparams = 0; + SFERES_CONST size_t nb_afparams = 1; + SFERES_CONST float conn_threshold = 0.2f; + SFERES_CONST float max_y = 10.0f; typedef nn::Neuron, nn::AfTanh > > neuron_t; typedef nn::Connection<> connection_t; diff --git a/trait.hpp b/trait.hpp index 75e1a91..c28d473 100644 --- a/trait.hpp +++ b/trait.hpp @@ -42,9 +42,7 @@ #include #include -#ifdef EIGEN2_ENABLED #include -#endif #include "params.hpp" diff --git a/wscript b/wscript index a118948..60d34ff 100644 --- a/wscript +++ b/wscript @@ -48,7 +48,7 @@ def build(bld): test_nn.source = 'test_nn.cpp' test_nn.includes = '. ../../' test_nn.uselib_local = '' - test_nn.uselib = 'EIGEN2 BOOST BOOST_GRAPH BOOST_UNIT_TEST_FRAMEWORK' + test_nn.uselib = 'EIGEN3 BOOST BOOST_GRAPH BOOST_UNIT_TEST_FRAMEWORK' test_nn.target = 'test_nn' test_nn.unit_test = 1 @@ -56,7 +56,7 @@ def build(bld): test_dnn.source = 'test_dnn.cpp' test_dnn.includes = '. ../../' test_dnn.uselib_local = 'sferes2' - test_dnn.uselib = 'EIGEN2 BOOST BOOST_GRAPH BOOST_UNIT_TEST_FRAMEWORK BOOST_SERIALIZATION' + test_dnn.uselib = 'EIGEN3 BOOST BOOST_GRAPH BOOST_UNIT_TEST_FRAMEWORK BOOST_SERIALIZATION' test_dnn.target = 'test_dnn' test_dnn.unit_test = 1 @@ -64,7 +64,7 @@ def build(bld): test_mlp.source = 'test_mlp.cpp' test_mlp.includes = '. ../../' test_mlp.uselib_local = 'sferes2' - test_mlp.uselib = 'EIGEN2 BOOST BOOST_GRAPH BOOST_UNIT_TEST_FRAMEWORK BOOST_SERIALIZATION' + test_mlp.uselib = 'EIGEN3 BOOST BOOST_GRAPH BOOST_UNIT_TEST_FRAMEWORK BOOST_SERIALIZATION' test_mlp.target = 'test_mlp' test_mlp.unit_test = 1 @@ -72,7 +72,7 @@ def build(bld): test_esn.source = 'test_esn.cpp' test_esn.includes = '. ../../' test_esn.uselib_local = 'sferes2' - test_esn.uselib = 'EIGEN2 BOOST BOOST_GRAPH BOOST_UNIT_TEST_FRAMEWORK BOOST_SERIALIZATION' + test_esn.uselib = 'EIGEN3 BOOST BOOST_GRAPH BOOST_UNIT_TEST_FRAMEWORK BOOST_SERIALIZATION' test_esn.target = 'test_esn' test_esn.unit_test = 1 @@ -80,7 +80,7 @@ def build(bld): test_esn.source = 'test_hyper_nn.cpp' test_esn.includes = '. ../../' test_esn.uselib_local = 'sferes2' - test_esn.uselib = 'EIGEN2 BOOST BOOST_GRAPH BOOST_UNIT_TEST_FRAMEWORK BOOST_SERIALIZATION' + test_esn.uselib = 'EIGEN3 BOOST BOOST_GRAPH BOOST_UNIT_TEST_FRAMEWORK BOOST_SERIALIZATION' test_esn.target = 'test_hyper_nn' test_esn.unit_test = 1 @@ -89,7 +89,7 @@ def build(bld): test_esn.source = 'test_dnn_ff.cpp' test_esn.includes = '. ../../' test_esn.uselib_local = 'sferes2' - test_esn.uselib = 'EIGEN2 BOOST BOOST_GRAPH BOOST_UNIT_TEST_FRAMEWORK BOOST_SERIALIZATION' + test_esn.uselib = 'EIGEN3 BOOST BOOST_GRAPH BOOST_UNIT_TEST_FRAMEWORK BOOST_SERIALIZATION' test_esn.target = 'test_dnn_ff' test_esn.unit_test = 1 @@ -99,7 +99,7 @@ def build(bld): test_osc.source = 'test_osc.cpp' test_osc.includes = '. ../../' test_osc.uselib_local = 'sferes2' - test_osc.uselib = 'EIGEN2 BOOST BOOST_GRAPH BOOST_UNIT_TEST_FRAMEWORK BOOST_SERIALIZATION' + test_osc.uselib = 'EIGEN3 BOOST BOOST_GRAPH BOOST_UNIT_TEST_FRAMEWORK BOOST_SERIALIZATION' test_osc.target = 'test_osc' test_osc.unit_test = 1 @@ -108,6 +108,6 @@ def build(bld): bench_nn.source = 'bench_nn.cpp' bench_nn.includes = '. ../../' bench_nn.uselib_local = 'sferes2' - bench_nn.uselib = 'EIGEN2 BOOST_GRAPH BOOST' + bench_nn.uselib = 'EIGEN3 BOOST_GRAPH BOOST' bench_nn.target = 'bench_nn' From 329bac65f25961d8d994d1e56f6559869c7a18e4 Mon Sep 17 00:00:00 2001 From: Matthieu Zimmer Date: Fri, 23 May 2014 17:25:14 +0200 Subject: [PATCH 2/3] fix merge --- af.hpp | 2 -- esn_2.hpp | 5 ++--- io_trait.hpp | 2 ++ pf.hpp | 1 - trait.hpp | 1 + 5 files changed, 5 insertions(+), 6 deletions(-) diff --git a/af.hpp b/af.hpp index 4966cca..2db9d93 100644 --- a/af.hpp +++ b/af.hpp @@ -36,7 +36,6 @@ #define _NN_AF_HPP_ #include "params.hpp" -#include // classic activation functions namespace nn @@ -73,7 +72,6 @@ namespace nn struct AfTanhNoBias : public Af

{ typedef params::Dummy params_t; - SFERES_CONST float lambda = 5.0f; BOOST_STATIC_CONSTEXPR float lambda = 5.0f; AfTanhNoBias() { } float operator()(float p) const diff --git a/esn_2.hpp b/esn_2.hpp index 3a706a7..cfc98e9 100644 --- a/esn_2.hpp +++ b/esn_2.hpp @@ -42,7 +42,6 @@ #include #include - #include #include @@ -165,8 +164,8 @@ namespace nn if (sferes::misc::rand() < alpha) conn(i, j) = sferes::misc::rand(-1.0f, 1.0f); // spectral radius - float max = (conn.eigenvalues().real().array().square() - + conn.eigenvalues().imag().array().square() + float max = (conn.eigenvalues().real().cwise().square() + + conn.eigenvalues().imag().cwise().square() ).maxCoeff(); assert(max > 0); conn *= alpha / sqrtf(max); diff --git a/io_trait.hpp b/io_trait.hpp index c813a85..58b3d74 100644 --- a/io_trait.hpp +++ b/io_trait.hpp @@ -44,6 +44,7 @@ #ifdef EIGEN3_ENABLED #include +#endif namespace nn { @@ -66,6 +67,7 @@ namespace nn static vector_t zero(size_t k) { return Eigen::VectorXf::Zero(k); } }; +#endif template<> struct io_trait > diff --git a/pf.hpp b/pf.hpp index 9280815..28efce7 100644 --- a/pf.hpp +++ b/pf.hpp @@ -38,7 +38,6 @@ #include #include -#include #include "params.hpp" #include "trait.hpp" diff --git a/trait.hpp b/trait.hpp index 4f3d0cf..e5afa56 100644 --- a/trait.hpp +++ b/trait.hpp @@ -44,6 +44,7 @@ #ifdef EIGEN3_ENABLED #include +#endif #include "params.hpp" From 09ca77d267f094348a80f0932c03eac77cdac1ec Mon Sep 17 00:00:00 2001 From: Matthieu Zimmer Date: Fri, 23 May 2014 19:25:34 +0200 Subject: [PATCH 3/3] io boost test --- test_dnn_ff_io.cpp | 142 +++++++++++++++++++++++++++++++++++++++++++++ wscript | 11 +++- 2 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 test_dnn_ff_io.cpp diff --git a/test_dnn_ff_io.cpp b/test_dnn_ff_io.cpp new file mode 100644 index 0000000..0cc8d47 --- /dev/null +++ b/test_dnn_ff_io.cpp @@ -0,0 +1,142 @@ +//| This file is a part of the sferes2 framework. +//| Copyright 2009, ISIR / Universite Pierre et Marie Curie (UPMC) +//| Main contributor(s): Jean-Baptiste Mouret, mouret@isir.fr +//| +//| This software is a computer program whose purpose is to facilitate +//| experiments in evolutionary computation and evolutionary robotics. +//| +//| This software is governed by the CeCILL license under French law +//| and abiding by the rules of distribution of free software. You +//| can use, modify and/ or redistribute the software under the terms +//| of the CeCILL license as circulated by CEA, CNRS and INRIA at the +//| following URL "http://www.cecill.info". +//| +//| As a counterpart to the access to the source code and rights to +//| copy, modify and redistribute granted by the license, users are +//| provided only with a limited warranty and the software's author, +//| the holder of the economic rights, and the successive licensors +//| have only limited liability. +//| +//| In this respect, the user's attention is drawn to the risks +//| associated with loading, using, modifying and/or developing or +//| reproducing the software by the user in light of its specific +//| status of free software, that may mean that it is complicated to +//| manipulate, and that also therefore means that it is reserved for +//| developers and experienced professionals having in-depth computer +//| knowledge. Users are therefore encouraged to load and test the +//| software's suitability as regards their requirements in conditions +//| enabling the security of their systems and/or data to be ensured +//| and, more generally, to use and operate it in the same conditions +//| as regards security. +//| +//| The fact that you are presently reading this means that you have +//| had knowledge of the CeCILL license and that you accept its terms. + +#define BOOST_TEST_DYN_LINK +#define BOOST_TEST_MODULE dnn_ff + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include "gen_dnn_ff.hpp" +#include "phen_dnn.hpp" + +using namespace sferes; +using namespace sferes::gen::dnn; +using namespace sferes::gen::evo_float; +using boost::archive::xml_oarchive; +using boost::archive::xml_iarchive; +using boost::serialization::make_nvp; + +struct Params +{ + struct evo_float + { + SFERES_CONST float mutation_rate = 0.1f; + SFERES_CONST float cross_rate = 0.1f; + SFERES_CONST mutation_t mutation_type = polynomial; + SFERES_CONST cross_over_t cross_over_type = sbx; + SFERES_CONST float eta_m = 15.0f; + SFERES_CONST float eta_c = 15.0f; + }; + struct parameters + { + // maximum value of parameters + SFERES_CONST float min = -5.0f; + // minimum value + SFERES_CONST float max = 5.0f; + }; + struct dnn + { + SFERES_CONST size_t nb_inputs = 4; + SFERES_CONST size_t nb_outputs = 1; + SFERES_CONST size_t min_nb_neurons = 4; + SFERES_CONST size_t max_nb_neurons = 5; + SFERES_CONST size_t min_nb_conns = 100; + SFERES_CONST size_t max_nb_conns = 101; + + SFERES_CONST float m_rate_add_conn = 1.0f; + SFERES_CONST float m_rate_del_conn = 0.1f; + SFERES_CONST float m_rate_change_conn = 1.0f; + SFERES_CONST float m_rate_add_neuron = 1.0f; + SFERES_CONST float m_rate_del_neuron = 1.0f; + + SFERES_CONST int io_param_evolving = true; + SFERES_CONST init_t init = ff; + }; +}; + +BOOST_AUTO_TEST_CASE(direct_nn_ff_io) +{ + srand(time(0)); + typedef phen::Parameters, fit::FitDummy<>, Params> weight_t; + typedef phen::Parameters, fit::FitDummy<>, Params> bias_t; + typedef nn::PfWSum pf_t; + typedef nn::AfSigmoidBias af_t; + typedef nn::Neuron neuron_t; + typedef nn::Connection connection_t; + typedef gen::DnnFF gen_t; + typedef phen::Dnn, Params> phen_t; + phen_t i; + i.random(); + i.develop(); + std::vector in(Params::dnn::nb_inputs, 0); + for(int i=0;i(0, 1); + i.nn().step(in); + float output1 = i.nn().get_neuron_output(0); + i.show(std::cout); + + std::ofstream ofs("/tmp/nn.xml"); + xml_oarchive xml(ofs); + xml << make_nvp("test", i); + ofs.close(); + + + phen_t* i2 = new phen_t; + std::ifstream inputFile("/tmp/nn.xml"); + xml_iarchive xml2(inputFile); + xml2 >> make_nvp("test", *i2); + inputFile.close(); + + i2->develop(); + i2->nn().step(in); + float output2 = i2->nn().get_neuron_output(0); + i2->show(std::cout); + + std::cout << output1 << " " << output2; + BOOST_CHECK_EQUAL(output1, output2); +} + diff --git a/wscript b/wscript index 60d34ff..5b2db92 100644 --- a/wscript +++ b/wscript @@ -92,7 +92,16 @@ def build(bld): test_esn.uselib = 'EIGEN3 BOOST BOOST_GRAPH BOOST_UNIT_TEST_FRAMEWORK BOOST_SERIALIZATION' test_esn.target = 'test_dnn_ff' test_esn.unit_test = 1 - + + + test_esn = bld.new_task_gen('cxx', 'program') + test_esn.source = 'test_dnn_ff_io.cpp' + test_esn.includes = '. ../../' + test_esn.uselib_local = 'sferes2' + test_esn.uselib = 'EIGEN3 BOOST BOOST_GRAPH BOOST_UNIT_TEST_FRAMEWORK BOOST_SERIALIZATION' + test_esn.target = 'test_dnn_ff_io' + test_esn.unit_test = 1 + test_osc = bld.new_task_gen('cxx', 'program')