diff --git a/data_files/data_pro_SP_MK.xlsx b/data_files/data_pro_SP_MK.xlsx deleted file mode 100644 index 132ed559dcbacddab2ab592b0a81e3c5cb96e2e2..0000000000000000000000000000000000000000 Binary files a/data_files/data_pro_SP_MK.xlsx and /dev/null differ diff --git a/src/Simulator/Simulator.cpp b/src/Simulator/Simulator.cpp deleted file mode 100644 index d738da50e9f3687f092c0e2af43d99b93a02c7e5..0000000000000000000000000000000000000000 --- a/src/Simulator/Simulator.cpp +++ /dev/null @@ -1,338 +0,0 @@ -/** - * DESCRIPTION OF THE FILE - * - * @author Michal KravÄŤenko - * @date 11.3.19 - - */ - -#include "../Neuron/NeuronBiased.h" -#include "../message.h" -#include "../exceptions.h" -#include "../settings.h" - -#include "../General/ExprtkWrapper.h" -#include "../Network/NeuralNetwork.h" - -#include "Simulator.h" - -namespace lib4neuro { - - Simulator::Simulator( - size_t n_outputs, - std::vector<size_t>& hidden_net_structure, - std::vector<std::vector<double>>& t, - std::vector<std::vector<double>>& xi - ) { - - - this->delete_weights = true; - this->delete_biases = true; - this->layers_analyzed = false; - - size_t inp_dim = 1; //!< Network input dimension - size_t out_dim = n_outputs; //!< Network output dimension - size_t transfer_dim = xi.size(); - this->n_valves = transfer_dim; - - COUT_DEBUG("Simulator is being constructed:" << std::endl); - COUT_DEBUG("# of inputs: " << inp_dim << std::endl); - COUT_DEBUG("# of outputs: " << out_dim << std::endl); - COUT_DEBUG("# transfer functions: " << transfer_dim << std::endl); - - std::vector<size_t> input_layer_neuron_indices; - std::vector<size_t> output_layer_neuron_indices; - std::vector<size_t> previous_layer_neuron_indices; - std::vector<size_t> current_layer_neuron_indices; - - /* Creation of INPUT layer neurons */ - current_layer_neuron_indices.reserve(inp_dim); - input_layer_neuron_indices.reserve(inp_dim); - output_layer_neuron_indices.reserve(out_dim); - - std::shared_ptr<Neuron> new_neuron2; - new_neuron2.reset(new NeuronLinear()); - size_t neuron_id = this->add_neuron(new_neuron2, - BIAS_TYPE::NO_BIAS); - input_layer_neuron_indices.emplace_back(neuron_id); - - /* Creation of OUTPUT layer neurons */ - for (unsigned int i = 0; i < out_dim; i++) { - std::shared_ptr<Neuron> new_neuron; - new_neuron.reset(new NeuronLinear()); - size_t neuron_id = this->add_neuron(new_neuron, - BIAS_TYPE::NO_BIAS); - output_layer_neuron_indices.emplace_back(neuron_id); - } - - - std::vector<size_t> bias_layer_neuron_indices; - for (size_t hi = 0; hi < transfer_dim; ++hi) { - COUT_DEBUG(" CONSTRUCTING TRANSFER FUNCTION BETWEEN VALVE #" << (hi + 1) - << " AND THE OUTPUTS, # OF COPIES PER OUTPUT: " - << xi[hi].size() << std::endl); - - this->index_start_output_connections_inclusive.push_back(this->connection_list.size()); - - //connection towards the bias neurons - bias_layer_neuron_indices.resize(0); - for (auto e : xi[hi]) { - std::shared_ptr<Neuron> new_neuron; - new_neuron.reset(new NeuronBiased(e)); - size_t new_n_idx = this->add_neuron(new_neuron, - BIAS_TYPE::NO_BIAS); - bias_layer_neuron_indices.emplace_back(new_n_idx); - - this->add_connection_constant(input_layer_neuron_indices[0], - new_n_idx, - 1.0); - } - - for (size_t oi = 0; oi < out_dim; ++oi) { - COUT_DEBUG(" CONSTRUCTING TRANSFER FUNCTION TOWARDS OUTPUT #" << (oi + 1) << std::endl); - size_t first_connection_idx = this->connection_weights.size(); - size_t first_bias_idx = this->neuron_biases.size(); - - COUT_DEBUG(" CONSTRUCTING TRANSFER FUNCTION #" << (1) << std::endl); - //for each timestep, we add a 'copy' of one neural network - std::vector<size_t> current_layer_neuron_indices_local, input_layer_neuron_indices_local, previous_layer_neuron_indices_local; - std::vector<size_t> bias_indices, connection_weights_indices; - /* Creation of INPUT layer neurons */ - current_layer_neuron_indices_local.reserve(inp_dim); - input_layer_neuron_indices_local.reserve(inp_dim); - size_t first_neuron_id = bias_layer_neuron_indices[0]; - input_layer_neuron_indices_local.emplace_back(first_neuron_id); - current_layer_neuron_indices_local = input_layer_neuron_indices_local; - - /* Creation of HIDDEN layers */ - for (unsigned int i = 1; i <= hidden_net_structure.size() - 2; i++) { - previous_layer_neuron_indices_local.reserve(hidden_net_structure.at(i - 1)); - previous_layer_neuron_indices_local = current_layer_neuron_indices_local; - current_layer_neuron_indices_local.clear(); - current_layer_neuron_indices_local.reserve(hidden_net_structure.at(i)); - - /* Creation of one single hidden layer */ - for (unsigned int j = 0; j < hidden_net_structure.at(i); j++) { - - /* Create a new hidden neuron */ - std::shared_ptr<Neuron> new_neuron; - new_neuron.reset(new NeuronLogistic()); - size_t neuron_id = this->add_neuron(new_neuron, - BIAS_TYPE::NEXT_BIAS); - - current_layer_neuron_indices_local.emplace_back(neuron_id); - - /* Connect the new neuron with all neurons from the previous layer */ - for (auto ind : previous_layer_neuron_indices_local) { - this->add_connection_simple(ind, - neuron_id, - l4n::SIMPLE_CONNECTION_TYPE::NEXT_WEIGHT); - } - } - } - - previous_layer_neuron_indices_local.reserve(hidden_net_structure.back() - 1); - previous_layer_neuron_indices_local = current_layer_neuron_indices_local; - current_layer_neuron_indices_local.clear(); - current_layer_neuron_indices_local.reserve(1); - - /* Creation of OUTPUT layer neurons */ - std::shared_ptr<Neuron> new_neuron; - new_neuron.reset(new NeuronLinear()); - size_t last_neuron_id = this->add_neuron(new_neuron, - BIAS_TYPE::NO_BIAS); - current_layer_neuron_indices_local.emplace_back(last_neuron_id); - /* Connect new neuron with all neurons from the previous layer */ - for (auto ind : previous_layer_neuron_indices_local) { - this->add_connection_simple(ind, - last_neuron_id, - l4n::SIMPLE_CONNECTION_TYPE::NEXT_WEIGHT); - } - /* connection towards the output */ - this->add_connection_constant(last_neuron_id, - output_layer_neuron_indices[oi], - xi[hi][0]); - - for (size_t k = 1; k < xi[hi].size(); ++k) { - COUT_DEBUG(" CONSTRUCTING TRANSFER FUNCTION #" << (k + 1) << std::endl); - size_t first_bias_idx_local = first_bias_idx; - size_t first_connection_idx_local = first_connection_idx; - - //for each timestep, we add a 'copy' of one neural network - current_layer_neuron_indices_local.clear(); - input_layer_neuron_indices_local.clear(); - previous_layer_neuron_indices_local.clear(); - - /* Creation of INPUT layer neurons */ - current_layer_neuron_indices_local.reserve(inp_dim); - input_layer_neuron_indices_local.reserve(inp_dim); - first_neuron_id = bias_layer_neuron_indices[k]; - input_layer_neuron_indices_local.emplace_back(first_neuron_id); - current_layer_neuron_indices_local = input_layer_neuron_indices_local; - - /* Creation of HIDDEN layers */ - for (unsigned int i = 1; i <= hidden_net_structure.size() - 2; i++) { - previous_layer_neuron_indices_local.reserve(hidden_net_structure.at(i - 1)); - previous_layer_neuron_indices_local = current_layer_neuron_indices_local; - current_layer_neuron_indices_local.clear(); - current_layer_neuron_indices_local.reserve(hidden_net_structure.at(i)); - - /* Creation of one single hidden layer */ - for (unsigned int j = 0; j < hidden_net_structure.at(i); j++) { - - /* Create a new hidden neuron */ - std::shared_ptr<Neuron> new_neuron; - new_neuron.reset(new NeuronLogistic()); - size_t neuron_id_local = this->add_neuron(new_neuron, - BIAS_TYPE::EXISTING_BIAS, - first_bias_idx_local); - first_bias_idx_local++; - current_layer_neuron_indices_local.emplace_back(neuron_id_local); - - /* Connect the new neuron with all neurons from the previous layer */ - for (auto ind : previous_layer_neuron_indices_local) { - this->add_existing_connection(ind, - neuron_id_local, - first_connection_idx_local, - *this); - first_connection_idx_local++; - } - } - } - - previous_layer_neuron_indices_local.reserve(hidden_net_structure.back() - 1); - previous_layer_neuron_indices_local = current_layer_neuron_indices_local; - current_layer_neuron_indices_local.clear(); - current_layer_neuron_indices_local.reserve(1); - - /* Creation of OUTPUT layer neurons */ - std::shared_ptr<Neuron> new_neuron; - new_neuron.reset(new NeuronLinear()); - size_t last_neuron_id_local = this->add_neuron(new_neuron, - BIAS_TYPE::NO_BIAS); - current_layer_neuron_indices_local.emplace_back(last_neuron_id_local); - /* Connect new neuron with all neurons from the previous layer */ - for (auto ind : previous_layer_neuron_indices_local) { - this->add_existing_connection(ind, - last_neuron_id_local, - first_connection_idx_local, - *this); - first_connection_idx_local++; - } - /* connection towards the output */ - this->add_connection_constant(last_neuron_id_local, - output_layer_neuron_indices[oi], - xi[hi][k]); - } - } - this->index_end_output_connections_inclusive.push_back(this->connection_list.size() - 1); - } - - /* Init variables containing indices of INPUT nad OUTPUT neurons */ - - this->input_neuron_indices = input_layer_neuron_indices; - this->output_neuron_indices = output_layer_neuron_indices; - - this->analyze_layer_structure(); - } - - Simulator::~Simulator() {} - - size_t Simulator::get_n_valves() { - return this->n_valves; - } - - void Simulator::eval_model(double t, - std::vector<double>& result) { - std::vector<double> input = {t}; - std::fill(result.begin(), - result.end(), - 0.0); - - this->eval_single(input, - result); - } - - void Simulator::eval_model(double t, - size_t valve_idx) { - THROW_NOT_IMPLEMENTED_ERROR(); - } - - void Simulator::eval_model(double t, - size_t valve_idx, - std::vector<double>& result) { - if ((this->input_neuron_indices.size() * this->output_neuron_indices.size()) <= 0) { - THROW_INVALID_ARGUMENT_ERROR("Input and output neurons have not been specified!"); - } - - if (this->input_neuron_indices.size() != 1) { - THROW_INVALID_ARGUMENT_ERROR("Data input size != Network input size"); - } - - if (this->output_neuron_indices.size() != result.size()) { - THROW_INVALID_ARGUMENT_ERROR("Data output size != Network output size"); - } - this->analyze_layer_structure(); - - std::vector<double> input = {t}; - std::fill(result.begin(), - result.end(), - 0.0); - - //TODO optimize... right now it just ignores the connections to be disregarded towards all the output - - - double potential, bias; - int bias_idx; - - - /* reset of the output and the neuron potentials */ - ::std::fill(this->neuron_potentials.begin(), - this->neuron_potentials.end(), - 0.0); - - /* set the potentials of the input neurons */ - for (size_t i = 0; i < this->input_neuron_indices.size(); ++i) { - this->neuron_potentials.at(this->input_neuron_indices.at(i)) = input[i]; - } - - /* we iterate through SOME of the connections and transfer the signals */ - for (auto layer: this->neuron_layers_feedforward) { - /* we iterate through all neurons in this layer and propagate the signal to the neighboring neurons */ - - for (auto si: *layer) { - bias = 0.0; - bias_idx = this->neuron_bias_indices.at(si); - if (bias_idx >= 0) { - bias = this->neuron_biases.at(bias_idx); - } - potential = this->neurons.at(si)->activate(this->neuron_potentials.at(si), - bias); - - for (auto c: *this->outward_adjacency.at(si)) { - size_t ti = c.first; - size_t ci = c.second; - - if (ci >= this->index_start_output_connections_inclusive[valve_idx] && - ci <= this->index_end_output_connections_inclusive[valve_idx]) { - this->neuron_potentials.at(ti) += - this->connection_list.at(ci)->eval(this->connection_weights) * potential; - } - - } - } - } - - unsigned int i = 0; - for (auto oi: this->output_neuron_indices) { - bias = 0.0; - bias_idx = this->neuron_bias_indices.at(oi); - if (bias_idx >= 0) { - bias = this->neuron_biases.at(bias_idx); - } - result[i] = this->neurons.at(oi)->activate(this->neuron_potentials.at(oi), - bias); - ++i; - } - } - -}//end of namespace lib4neuro diff --git a/src/Simulator/Simulator.h b/src/Simulator/Simulator.h deleted file mode 100644 index 54660df0314f54b73f3f693ea61caeb244970411..0000000000000000000000000000000000000000 --- a/src/Simulator/Simulator.h +++ /dev/null @@ -1,53 +0,0 @@ -/** - * DESCRIPTION OF THE FILE - * - * @author Michal KravÄŤenko - * @date 11.3.19 - - */ - -#ifndef LIB4NEURO_SIMULATOR_H -#define LIB4NEURO_SIMULATOR_H - -#include <iostream> - -#include "4neuro.h" - - -namespace lib4neuro { - class Simulator : public NeuralNetwork { - private: - size_t n_valves; - - std::vector<size_t> index_start_output_connections_inclusive; - std::vector<size_t> index_end_output_connections_inclusive; - - public: - - LIB4NEURO_API explicit Simulator( - size_t n_outputs, - std::vector<size_t>& hidden_net_structure, - std::vector<std::vector<double>>& t, - std::vector<std::vector<double>>& xi - ); - - ~Simulator(); - - LIB4NEURO_API size_t get_n_valves(); - - LIB4NEURO_API void eval_model(double t, - std::vector<double>& result); - - LIB4NEURO_API void eval_model(double t, - size_t valve_idx, - std::vector<double>& result); - - //TODO - LIB4NEURO_API void eval_model(double t, - size_t valve_idx); - - }; - -};//end of namespace lib4neuro - - -#endif //LIB4NEURO_SIMULATOR_H diff --git a/src/examples/simulator.cpp b/src/examples/simulator.cpp deleted file mode 100644 index eda272018df6ca6298c20cbf7c49b91549f07527..0000000000000000000000000000000000000000 --- a/src/examples/simulator.cpp +++ /dev/null @@ -1,172 +0,0 @@ - - -#include <iostream> -#include <cstdio> -#include <fstream> -#include <vector> -#include <utility> -#include <algorithm> -#include <assert.h> - -#include "4neuro.h" -#include "../LearningMethods/RandomSolution.h" - -int main(int argc, - char** argv) { - - bool normalize_data = true; - double prec = 1e-9; - double prec_lm = 1e-9; - int restart_interval = 500; - int max_n_iters_gradient = 10000; - int max_n_iters_gradient_lm = 10000; - - int max_n_iters_swarm = 20; - int n_particles_swarm = 200; - unsigned long batch_size = 0; - int max_number_of_cycles = 1; - try { - /* PHASE 1 - TRAINING DATA LOADING, NETWORK ASSEMBLY AND PARTICLE SWARM OPTIMIZATION */ - l4n::CSVReader reader1("/home/fluffymoo/Dropbox/data_BACK_RH_1.csv", - ";", - true); // File, separator, skip 1st line - reader1.read(); // Read from the file - - /* PHASE 1 - NEURAL NETWORK SPECIFICATION */ - /* Create data set for both the first training of the neural network */ - /* Specify which columns are inputs or outputs */ - std::vector<unsigned int> inputs = {0}; // Possible multiple inputs, e.g. {0,3}, column indices starting from 0 - std::vector<unsigned int> outputs = {2}; // Possible multiple outputs, e.g. {1,2} - std::shared_ptr<l4n::DataSet> ds1 = reader1.get_data_set(&inputs, - &outputs); // Creation of data-set for NN - if (normalize_data) { - ds1.operator->()->set_normalization_strategy(new DoubleUnitStrategy()); - ds1.get()->normalize(); // Normalization of data to prevent numerical problems - } - - /* Numbers of neurons in layers (including input and output layers) */ - std::vector<unsigned int> neuron_numbers_in_layers = {1, 3, 1}; - - /* Fully connected feed-forward network with linear activation functions for input and output */ - /* layers and the specified activation fns for the hidden ones (each entry = layer)*/ - std::vector<l4n::NEURON_TYPE> hidden_type_v = {l4n::NEURON_TYPE::LOGISTIC, l4n::NEURON_TYPE::LOGISTIC, - l4n::NEURON_TYPE::LOGISTIC, l4n::NEURON_TYPE::LOGISTIC, - l4n::NEURON_TYPE::LOGISTIC}; // hidden_type_v = {l4n::NEURON_TYPE::LOGISTIC, l4n::NEURON_TYPE::LINEAR} - l4n::FullyConnectedFFN nn1(&neuron_numbers_in_layers, - &hidden_type_v); - - /* Error function */ - l4n::MSE mse1(&nn1, - ds1.get()); // First parameter - neural network, second parameter - data-set - - /* Particle Swarm method domain*/ - std::vector<double> domain_bounds(2 * (nn1.get_n_weights() + nn1.get_n_biases())); - for (size_t i = 0; i < domain_bounds.size() / 2; ++i) { - domain_bounds[2 * i] = -0.1; - domain_bounds[2 * i + 1] = 0.1; - } - - // Parameters of the Particle Swarm - // 1) domain_bounds Bounds for every optimized parameter (p1_lower, p1_upper, p2_lower, p2_upper...) - // 2) c1 Cognitive parameter - // 3) c2 Social parameter - // 4) w Inertia weight - // 5) gamma Threshold value for particle velocity - all particles must posses the same or slower velocity for the algorithm to end - // 6) epsilon Radius of the cluster area (Euclidean distance) - // 7) delta Amount of particles, which has to be in the cluster for the algorithm to stop (0-1) - // 8) n_particles Number of particles in the swarm - // 9) iter_max Maximal number of iterations - optimization will stop after that, even if not converged - l4n::ParticleSwarm ps(&domain_bounds, - 1.711897, - 1.711897, - 0.711897, - 0.5, - 0.3, - 0.7, - n_particles_swarm, - max_n_iters_swarm); - - // Parameters of the gradient descent - // 1) Threshold for the successful ending of the optimization - deviation from minima - // 2) Number of iterations to reset step size to tolerance/10.0 - // 3) Maximal number of iterations - optimization will stop after that, even if not converged - l4n::RandomSolution rnd; - l4n::GradientDescent gs_(prec, - restart_interval, - max_n_iters_gradient, - batch_size); - l4n::GradientDescentBB gs(prec, - restart_interval, - max_n_iters_gradient, - batch_size); - l4n::GradientDescentSingleItem gs_si(prec, - 0, - 5000);//TODO needs improvement - l4n::LevenbergMarquardt leven(max_n_iters_gradient_lm, - batch_size, - prec_lm); - l4n::LearningSequence learning_sequence(1e-6, - max_number_of_cycles); - - std::shared_ptr<l4n::LearningMethod> new_learning_method; - new_learning_method.reset(&rnd); - learning_sequence.add_learning_method(new_learning_method); - std::shared_ptr<l4n::LearningMethod> new_learning_method2; - new_learning_method2.reset(&leven); - learning_sequence.add_learning_method(new_learning_method2); - - /* Weight and bias randomization in the network accordingly to the uniform distribution */ - nn1.randomize_parameters(); - - /* Complex Optimization */ - learning_sequence.optimize(mse1); // Network training - - /* Save Neural network parameters to file */ - nn1.save_text("test_net_Gradient_Descent.4n"); - - /* PHASE 4 - TESTING DATA */ - - std::string filename = "simulator_output.txt"; - std::ofstream output_file(filename); - if (!output_file.is_open()) { - throw std::runtime_error("File '" + filename + "' can't be opened!"); - } - l4n::NeuralNetwork nn3("test_net_Gradient_Descent.4n"); - - /* Check of the saved network - write to the file */ - output_file << std::endl << "The loaded network info:" << std::endl; - nn3.write_stats(&output_file); - nn3.write_weights(&output_file); - nn3.write_biases(&output_file); - l4n::CSVReader reader3("/home/fluffymoo/Dropbox/data_BACK_RH_1.csv", - ";", - true); // File, separator, skip 1st line - reader3.read(); // Read from the file - std::shared_ptr<l4n::DataSet> ds3 = reader3.get_data_set(&inputs, - &outputs); // Creation of data-set for NN - if (normalize_data) { - ds3.operator->()->set_normalization_strategy(new DoubleUnitStrategy()); - ds3.get()->normalize(); // Normalization of data to prevent numerical problems - } - output_file << "Output and the error:" << std::endl; - l4n::MSE mse3(&nn3, - ds3.get()); // First parameter - neural network, second parameter - data-set - - mse3.eval_on_data_set(ds3.get(), - &output_file, - nullptr, - normalize_data, - true); - - /* Close the output file for writing */ - output_file.close(); - - return 0; - - } - catch (const std::exception& e) { - std::cerr << e.what() << std::endl; - exit(EXIT_FAILURE); - } - -} diff --git a/src/examples/simulator_1_2.cpp b/src/examples/simulator_1_2.cpp deleted file mode 100644 index e30e086815820f98163fb2325f5508aeefa0b6f1..0000000000000000000000000000000000000000 --- a/src/examples/simulator_1_2.cpp +++ /dev/null @@ -1,170 +0,0 @@ -/** - * DESCRIPTION OF THE FILE - * - * @author Michal KravÄŤenko - * @date 15.3.19 - - */ - - -#include <iostream> -#include <cstdio> -#include <fstream> -#include <vector> -#include <utility> -#include <algorithm> -#include <assert.h> - -#include "4neuro.h" -#include "../LearningMethods/RandomSolution.h" - -int main(int argc, - char** argv) { - - //POZOR, NORMALIZACE JE NUTNA A JE TREBA MIT NA PAMETI, ZE MA VLIV NA CASOVE POSUNY - bool normalize_data = true; - double prec = 1e-9; - double prec_lm = 1e-9; - int restart_interval = 500; - int max_n_iters_gradient = 10000; - int max_n_iters_gradient_lm = 100000; - int max_n_iters_swarm = 5; - int n_particles_swarm = 200; - unsigned long batch_size = 0; - int max_number_of_cycles = 1; - try { - /* PHASE 2 - TRAINING DATA LOADING, SIMPLE SIMULATION */ - l4n::CSVReader reader1("/home/fluffymoo/Dropbox/data_BACK_RH_1.csv", - ";", - true); // File, separator, skip 1st line - reader1.read(); // Read from the file - - /* PHASE 2 - NEURAL NETWORK SPECIFICATION */ - /* Create data set for both the first training of the neural network */ - /* Specify which columns are inputs or outputs */ - std::vector<unsigned int> inputs = {0}; // Possible multiple inputs, e.g. {0,3}, column indices starting from 0 - std::vector<unsigned int> outputs = {2}; // Possible multiple outputs, e.g. {1,2} - std::shared_ptr<l4n::DataSet> ds1 = reader1.get_data_set(&inputs, - &outputs); // Creation of data-set for NN - - if (normalize_data) { - ds1.operator->()->set_normalization_strategy(new DoubleUnitStrategy()); - ds1->normalize(); // Normalization of data to prevent numerical problems - } - - /* Numbers of neurons in layers (including input and output layers) */ - std::vector<size_t> neuron_numbers_in_layers = {1, 5, 5, 1}; - - /* V CASE 0 NASTAVIME TEPLOTU NA 1135*/ - /* for each valve (1 in this example) setup the times of change */ - std::vector<std::vector<double>> t; - t.push_back({ds1->get_normalized_value(0)});//DULEZITY RADEK - - /* for each valve (1 in this example) setup the magnitudes of change */ - std::vector<std::vector<double>> xi; - xi.push_back({ds1->get_normalized_value(1135)});//DULEZITY RADEK - - /* The simulator2 object */ - l4n::Simulator sim(outputs.size(), - neuron_numbers_in_layers, - t, - xi); - - /* Error function */ - l4n::MSE mse1(&sim, - ds1.get()); // First parameter - neural network, second parameter - data-set - - /* Particle Swarm method domain*/ - std::vector<double> domain_bounds(2 * (sim.get_n_weights() + sim.get_n_biases())); - for (size_t i = 0; i < domain_bounds.size() / 2; ++i) { - domain_bounds[2 * i] = -0.1; - domain_bounds[2 * i + 1] = 0.1; - } - - l4n::LearningSequence learning_sequence(1e-6, - max_number_of_cycles); - - auto new_learning_method = std::make_shared<l4n::RandomSolution>(); - learning_sequence.add_learning_method(new_learning_method); - - auto new_learning_method2 = std::make_shared<l4n::ParticleSwarm>(l4n::ParticleSwarm(&domain_bounds, - 1.711897, - 1.711897, - 0.711897, - 0.5, - 0.3, - 0.7, - n_particles_swarm, - max_n_iters_swarm)); - - auto new_learning_method3 = std::make_shared<l4n::LevenbergMarquardt>(l4n::LevenbergMarquardt(max_n_iters_gradient_lm, - batch_size, - prec_lm)); - learning_sequence.add_learning_method(new_learning_method3); - - /* Complex Optimization */ - learning_sequence.optimize(mse1); // Network training - - /* Save Neural network parameters to file */ - sim.save_text("test_net_Gradient_Descent.4n"); - - /* PHASE 4 - TESTING DATA */ - - /* Output file specification */ - std::string filename = "simulator_output.txt"; - std::ofstream output_file(filename); - if (!output_file.is_open()) { - throw std::runtime_error("File '" + filename + "' can't be opened!"); - } - - /* Neural network loading */ - l4n::NeuralNetwork nn3("test_net_Gradient_Descent.4n"); - - /* Check of the saved network - write to the file */ - output_file << std::endl << "The loaded network info:" << std::endl; - nn3.write_stats(&output_file); - nn3.write_weights(&output_file); - nn3.write_biases(&output_file); - - /* Evaluate network on an arbitrary data-set and save results into the file */ - l4n::CSVReader reader3("/home/fluffymoo/Dropbox/data_BACK_RH_1.csv", - ";", - true); // File, separator, skip 1st line - reader3.read(); // Read from the file - - /* Create data set for both the testing of the neural network */ - /* Specify which columns are inputs or outputs */ - - std::shared_ptr<l4n::DataSet> ds3 = reader3.get_data_set(&inputs, - &outputs); // Creation of data-set for NN - if (normalize_data) { - ds3.operator->()->set_normalization_strategy(new DoubleUnitStrategy()); - ds3.get()->normalize(); // Normalization of data to prevent numerical problems - } - - output_file << std::endl << "Evaluating network on the dataset: " << std::endl; - ds3->store_data_text(&output_file); - - output_file << "Output and the error:" << std::endl; - - /* Error function */ - l4n::MSE mse3(&nn3, - ds3.get()); // First parameter - neural network, second parameter - data-set - - mse3.eval_on_data_set(ds3.get(), - &output_file, - nullptr, - normalize_data, - true); - - /* Close the output file for writing */ - output_file.close(); - - return 0; - - } - catch (const std::exception& e) { - std::cerr << e.what() << std::endl; - exit(EXIT_FAILURE); - } - -}