diff --git a/src/CSVReader/CSVReader.cpp b/src/CSVReader/CSVReader.cpp index 319c2bf50a0eacd821a6efeefdf93dc3c94d4865..b04e2f75c177bc48f421d3128483dfe75301b6e4 100644 --- a/src/CSVReader/CSVReader.cpp +++ b/src/CSVReader/CSVReader.cpp @@ -26,7 +26,7 @@ namespace lib4neuro { this->delimiter = delimiter; this->ignore_first_line = ignore_first_line; this->header_included = ignore_first_line; - this->data = std::make_unique<std::vector<std::vector<std::string>>>(); +// this->data = std::make_unique<std::vector<std::vector<std::string>>>(); } void CSVReader::read() { @@ -56,18 +56,18 @@ namespace lib4neuro { separated_line.emplace_back(line.substr(last)); /* Store the elements from the line to the vector with data */ - this->data->emplace_back(separated_line); + this->data.emplace_back(separated_line); } ifs.close(); } - std::unique_ptr<std::vector<std::vector<std::string>>>* CSVReader::get_data() { + std::vector<std::vector<std::string>>* CSVReader::get_data() { return &this->data; } void CSVReader::print_data() { - for(auto line : *this->data) { + for(auto line : this->data) { for(auto e : line) { std::cout << e << " "; } @@ -80,12 +80,12 @@ namespace lib4neuro { std::vector<std::pair<std::vector<double>, std::vector<double>>> data_set_contents; - if(this->data->empty()) { + if(this->data.empty()) { THROW_LOGIC_ERROR("DataSet can not be created as there were no data read beforehand! Did you forget to call " "the method 'read()'?"); } - for (auto line : *this->data) { + for (auto line : this->data) { //TODO check empty values in data std::vector<double> input; for (auto ind : *input_col_indices) { diff --git a/src/CSVReader/CSVReader.h b/src/CSVReader/CSVReader.h index 46a9ee4c9a71f278ba0bdfbd847861085363dd09..993ea53bed4814020e521e323037b5961b18a7ef 100644 --- a/src/CSVReader/CSVReader.h +++ b/src/CSVReader/CSVReader.h @@ -43,7 +43,7 @@ namespace lib4neuro { /** * */ - std::unique_ptr<std::vector<std::vector<std::string>>> data; + std::vector<std::vector<std::string>> data; public: @@ -64,7 +64,8 @@ namespace lib4neuro { * * @return */ - LIB4NEURO_API std::unique_ptr<std::vector<std::vector<std::string>>>* get_data(); +// LIB4NEURO_API std::shared_ptr<std::vector<std::vector<std::string>>> get_data(); + LIB4NEURO_API std::vector<std::vector<std::string>>* get_data(); /** * diff --git a/src/LearningMethods/LearningSequence.cpp b/src/LearningMethods/LearningSequence.cpp index 05569b85de4b68e797a1e4db4928f777c017f68f..7109a7797e9bfb01e93629303dd74698e54e3272 100644 --- a/src/LearningMethods/LearningSequence.cpp +++ b/src/LearningMethods/LearningSequence.cpp @@ -31,7 +31,12 @@ namespace lib4neuro { void LearningSequence::optimize(lib4neuro::ErrorFunction &ef, std::ofstream *ofs) { + puts("*********************** 11"); + double error = ef.eval(); + + puts("*********************** 12"); + double the_best_error = error; int mcycles = this->max_number_of_cycles, cycle_idx = 0; @@ -39,11 +44,21 @@ namespace lib4neuro { mcycles--; cycle_idx++; + puts("*********************** 7"); + for( auto m: this->learning_sequence ){ + + puts("*********************** 8"); + m->optimize( ef, ofs ); error = ef.eval(m->get_parameters()); + + puts("*********************** 9"); + ef.get_network_instance()->copy_parameter_space(m->get_parameters()); + puts("*********************** 10"); + if( error < the_best_error ){ the_best_error = error; this->best_parameters = *ef.get_parameters(); diff --git a/src/Network/NeuralNetwork.cpp b/src/Network/NeuralNetwork.cpp index e2adea6dccd99d928bd1a5eb7295603f690667e0..e13646bbf2a1f3dd1353fcad63bb394da67b169b 100644 --- a/src/Network/NeuralNetwork.cpp +++ b/src/Network/NeuralNetwork.cpp @@ -387,7 +387,7 @@ namespace lib4neuro { return output_net; } - size_t NeuralNetwork::add_neuron(Neuron *n, BIAS_TYPE bt, size_t bias_idx) { + size_t NeuralNetwork::add_neuron(std::shared_ptr<Neuron> n, BIAS_TYPE bt, size_t bias_idx) { if (bt == BIAS_TYPE::NO_BIAS) { this->neuron_bias_indices.push_back(-1); @@ -668,7 +668,7 @@ namespace lib4neuro { for (size_t i = 0; i < current_layer->size(); ++i) { neuron_idx = current_layer->at(i); - active_neuron = dynamic_cast<NeuronDifferentiable *> (this->neurons.at(neuron_idx)); + active_neuron = dynamic_cast<NeuronDifferentiable *> (this->neurons.at(neuron_idx).get()); if (active_neuron) { bias_idx = this->neuron_bias_indices.at(neuron_idx); @@ -741,7 +741,7 @@ namespace lib4neuro { for (size_t i = 0; i < current_layer->size(); ++i) { neuron_idx = current_layer->at(i); - active_neuron = dynamic_cast<NeuronDifferentiable *> (this->neurons.at(neuron_idx)); + active_neuron = dynamic_cast<NeuronDifferentiable *> (this->neurons.at(neuron_idx).get()); if (active_neuron) { std::cout << " [backpropagation] active neuron: " << neuron_idx << std::endl; @@ -1275,7 +1275,9 @@ namespace lib4neuro { current_layer_neuron_indices.reserve(inp_dim); input_layer_neuron_indices.reserve(inp_dim); for(unsigned int i = 0; i < inp_dim; i++) { - size_t neuron_id = this->add_neuron(new NeuronLinear, BIAS_TYPE::NO_BIAS); + std::shared_ptr<Neuron> new_neuron; + new_neuron.reset(new NeuronLinear()); + size_t neuron_id = this->add_neuron(new_neuron, BIAS_TYPE::NO_BIAS); current_layer_neuron_indices.emplace_back(neuron_id); } input_layer_neuron_indices = current_layer_neuron_indices; @@ -1294,9 +1296,11 @@ namespace lib4neuro { size_t neuron_id; /* Create new hidden neuron */ - switch (hidden_layer_neuron_types->at(i-1)) { + switch (hidden_layer_neuron_types->at(i-1)) { case NEURON_TYPE::BINARY: { - neuron_id = this->add_neuron(new NeuronBinary, BIAS_TYPE::NEXT_BIAS); + std::shared_ptr<Neuron> new_neuron; + new_neuron.reset(new NeuronBinary()); + neuron_id = this->add_neuron(new_neuron, BIAS_TYPE::NEXT_BIAS); COUT_DEBUG("Added BINARY neuron." << std::endl); WRITE_TO_OFS_DEBUG(ofs, "Added BINARY neuron." << std::endl); break; @@ -1308,14 +1312,18 @@ namespace lib4neuro { } case NEURON_TYPE::LINEAR: { - neuron_id = this->add_neuron(new NeuronLinear, BIAS_TYPE::NEXT_BIAS); + std::shared_ptr<Neuron> new_neuron; + new_neuron.reset(new NeuronLinear()); + neuron_id = this->add_neuron(new_neuron, BIAS_TYPE::NEXT_BIAS); COUT_DEBUG("Added LINEAR neuron." << std::endl); WRITE_TO_OFS_DEBUG(ofs, "Added LINEAR neuron." << std::endl); break; } case NEURON_TYPE::LOGISTIC: { - neuron_id = this->add_neuron(new NeuronLogistic, BIAS_TYPE::NEXT_BIAS); + std::shared_ptr<Neuron> new_neuron; + new_neuron.reset(new NeuronLogistic()); + neuron_id = this->add_neuron(new_neuron, BIAS_TYPE::NEXT_BIAS); COUT_DEBUG("Added LOGISTIC neuron." << std::endl); WRITE_TO_OFS_DEBUG(ofs, "Added LINEAR neuron." << std::endl); break; @@ -1338,7 +1346,9 @@ namespace lib4neuro { /* Creation of OUTPUT layer neurons */ for(unsigned int i = 0; i < out_dim; i++) { - size_t neuron_id = this->add_neuron(new NeuronLinear, BIAS_TYPE::NO_BIAS); + std::shared_ptr<Neuron> new_neuron; + new_neuron.reset(new NeuronLinear()); + size_t neuron_id = this->add_neuron(new_neuron, BIAS_TYPE::NO_BIAS); current_layer_neuron_indices.emplace_back(neuron_id); /* Connect new neuron with all neuron from the previous layer */ diff --git a/src/Network/NeuralNetwork.h b/src/Network/NeuralNetwork.h index f00819b8d1cf811d5a48975e3cb5dded02155a99..cc18cd568603439f5b83a4bba404e2bc3a129ab5 100644 --- a/src/Network/NeuralNetwork.h +++ b/src/Network/NeuralNetwork.h @@ -55,7 +55,7 @@ namespace lib4neuro { /** * */ - std::vector<Neuron *> neurons; // = nullptr; + std::vector<std::shared_ptr<Neuron>> neurons; // = nullptr; /** * @@ -264,7 +264,7 @@ namespace lib4neuro { * @param[in] n * @return */ - LIB4NEURO_API size_t add_neuron(Neuron *n, BIAS_TYPE bt = BIAS_TYPE::NEXT_BIAS, size_t bias_idx = 0); + LIB4NEURO_API size_t add_neuron(std::shared_ptr<Neuron> n, BIAS_TYPE bt = BIAS_TYPE::NEXT_BIAS, size_t bias_idx = 0); /** * diff --git a/src/Solvers/DESolver.cpp b/src/Solvers/DESolver.cpp index e201c565c3ef2186a3ce87da5741e778c8c2e317..d8eebb85f869109c6ee25cc00286464a7cae04e8 100644 --- a/src/Solvers/DESolver.cpp +++ b/src/Solvers/DESolver.cpp @@ -91,8 +91,10 @@ namespace lib4neuro { std::vector<size_t> input_set(this->dim_i); size_t idx; for (size_t i = 0; i < this->dim_i; ++i) { - NeuronLinear *input_i = new NeuronLinear(); //f(x) = x - idx = this->solution->add_neuron(input_i, BIAS_TYPE::NO_BIAS); +// NeuronLinear *input_i = new NeuronLinear(); //f(x) = x + std::shared_ptr<Neuron> new_neuron; + new_neuron.reset(new NeuronLinear()); + idx = this->solution->add_neuron(new_neuron, BIAS_TYPE::NO_BIAS); input_set[i] = idx; } this->solution->specify_input_neurons(input_set); @@ -100,7 +102,9 @@ namespace lib4neuro { /* output neuron */ std::vector<size_t> output_set(1); - idx = this->solution->add_neuron(new NeuronLinear(), BIAS_TYPE::NO_BIAS);//f(x) = x + std::shared_ptr<Neuron> new_neuron; + new_neuron.reset(new NeuronLinear()); + idx = this->solution->add_neuron(new_neuron, BIAS_TYPE::NO_BIAS);//f(x) = x output_set[0] = idx; this->solution->specify_output_neurons(output_set); size_t first_output_neuron = idx; @@ -108,9 +112,11 @@ namespace lib4neuro { /* inner neurons */ size_t first_inner_neuron = 0; for (size_t i = 0; i < this->dim_inn; ++i) { - NeuronLogistic *inner_i = new NeuronLogistic(); //f(x) = 1.0 / (1.0 + e^(-x)) - this->solution_inner_neurons->push_back(inner_i); - idx = this->solution->add_neuron(inner_i, BIAS_TYPE::NEXT_BIAS); +// NeuronLogistic *inner_i = new NeuronLogistic(); //f(x) = 1.0 / (1.0 + e^(-x)) + std::shared_ptr<Neuron> new_neuron2; + new_neuron2.reset(new NeuronLogistic()); + this->solution_inner_neurons->push_back(new_neuron2.get()); + idx = this->solution->add_neuron(new_neuron2, BIAS_TYPE::NEXT_BIAS); if (i == 0) { first_inner_neuron = idx; diff --git a/src/Solvers/DESolver.h b/src/Solvers/DESolver.h index cab736bcc4a8c9d2ba9d8087d04c200f47ff5e4c..886212ff4cf6770b9089f2f7c251288599179e50 100644 --- a/src/Solvers/DESolver.h +++ b/src/Solvers/DESolver.h @@ -93,17 +93,17 @@ namespace lib4neuro { std::map<MultiIndex, NeuralNetwork *> map_multiindices2nn; /* A list of the differential equations */ - std::vector<NeuralNetworkSum *> *differential_equations = nullptr; + std::vector<NeuralNetworkSum *> differential_equations; // = nullptr; /* Error functions for differential equations */ - std::vector<ErrorFunctionType> *errors_functions_types = nullptr; - std::vector<DataSet *> *errors_functions_data_sets = nullptr; + std::vector<ErrorFunctionType> errors_functions_types; // = nullptr; + std::vector<DataSet *> errors_functions_data_sets; // = nullptr; /* NN as the unknown function */ NeuralNetwork *solution = nullptr; /* auxilliary variables */ - std::vector<NeuronLogistic *> *solution_inner_neurons = nullptr; + std::vector<NeuronLogistic *> solution_inner_neurons; // = nullptr; size_t dim_i = 0, dim_inn = 0, n_equations = 0; public: