From ce4277873a5547cf25baf800be5ec4e4940433ac Mon Sep 17 00:00:00 2001 From: Martin Beseda <martin.beseda@vsb.cz> Date: Sat, 14 Sep 2019 22:36:12 +0200 Subject: [PATCH] [ENH] Added parameters to ACSF optimizer to make the parameters' fitting more flexible. --- .../ACSFParametersOptimizer.cpp | 42 +++++++++++-------- .../ACSFParametersOptimizer.h | 9 ++++ 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/SymmetryFunction/ACSFParametersOptimizer.cpp b/src/SymmetryFunction/ACSFParametersOptimizer.cpp index 4db08bae..4d92bedf 100644 --- a/src/SymmetryFunction/ACSFParametersOptimizer.cpp +++ b/src/SymmetryFunction/ACSFParametersOptimizer.cpp @@ -30,19 +30,30 @@ lib4neuro::ACSFParametersOptimizer::ACSFParametersOptimizer(lib4neuro::ErrorFunc void lib4neuro::ACSFParametersOptimizer::fit_ACSF_parameters(std::vector<SYMMETRY_FUNCTION_PARAMETER>& fitted_params, bool random_init_params) { + fit_ACSF_parameters(fitted_params, + random_init_params, + 30, + 10, + 1e-5, + 0.98, + 0.085, + 1e-6); +} +void lib4neuro::ACSFParametersOptimizer::fit_ACSF_parameters(std::vector<SYMMETRY_FUNCTION_PARAMETER>& fitted_params, + bool random_init_params, + unsigned int main_iterations_num, + unsigned int steady_temperature_iters_num, + double init_temperature, + double temperature_change, + double distribution_stddev, + double termination_threshold) { COUT_INFO("Starting optimizing of ACSF parameters..."); /* Seed the random numbers' generator */ std::random_device rd{}; std::mt19937 gen{rd()}; -// GradientDescentBB gd(1e-6, -// 1000, -// 10000); -// -// LevenbergMarquardt lm(1000); - /* Construct and train a new linear neural network to evaluate the chosen symmetry functions' parameters */ std::vector<unsigned int> neuron_numbers = {static_cast<unsigned int>(this->net->get_input_neurons_number()), 1}; std::vector<NEURON_TYPE> type_hidden_neurons = {}; @@ -81,16 +92,16 @@ void lib4neuro::ACSFParametersOptimizer::fit_ACSF_parameters(std::vector<SYMMETR double current_error; double delta_error; - double temperature = 3e-4; //TODO make flexible by adding function parameters + double temperature = init_temperature; //TODO make flexible by adding function parameters MSE mse_init(&nn, this->data_set); previous_error = mse_init.eval(); /* Iteratively improve the parameters */ unsigned int iter_ind = 0; - for(unsigned int i = 0; i < 10; i++) { + for(unsigned int i = 0; i < main_iterations_num; i++) { /* Temperature iterations */ - for(unsigned int j = 0; j < 10; j++) { + for(unsigned int j = 0; j < steady_temperature_iters_num; j++) { /* Change the selected parameters in ACSFs */ for (auto& el : *this->net->get_elements()) { for (auto& func : *el.second->getSymmetryFunctions()) { @@ -103,7 +114,7 @@ void lib4neuro::ACSFParametersOptimizer::fit_ACSF_parameters(std::vector<SYMMETR func->set_parameter(param.first, shift_max_vals.at((int) (d_uni(gen)))); } else { - std::normal_distribution<> dd{param.second, 0.085}; + std::normal_distribution<> dd{param.second, distribution_stddev}; func->set_parameter(param.first, dd(gen)); } @@ -124,18 +135,16 @@ void lib4neuro::ACSFParametersOptimizer::fit_ACSF_parameters(std::vector<SYMMETR MSE mse(&nn, this->data_set); -// gd.optimize(mse); - std::vector<double> new_weights = arma::conv_to<std::vector<double>>::from( arma::solve(*this->data_set->get_inputs_matrix(), - *this->data_set->get_outputs_matrix(), - arma::solve_opts::fast)); + *this->data_set->get_outputs_matrix(), + arma::solve_opts::fast)); nn.copy_parameter_space(&new_weights); current_error = mse.eval(); /* Termination condition */ - if(current_error < 10e-6) { + if(current_error < termination_threshold) { COUT_INFO("Error when optimizing ACSF parameters: " + std::to_string(current_error)); return; } @@ -162,10 +171,9 @@ void lib4neuro::ACSFParametersOptimizer::fit_ACSF_parameters(std::vector<SYMMETR previous_error = current_error; - std::cout << "it: " << iter_ind << " Delta error: " << delta_error << std::endl; iter_ind++; } - temperature *= 0.98; + temperature *= temperature_change; } } diff --git a/src/SymmetryFunction/ACSFParametersOptimizer.h b/src/SymmetryFunction/ACSFParametersOptimizer.h index c0e8bd42..0434a567 100644 --- a/src/SymmetryFunction/ACSFParametersOptimizer.h +++ b/src/SymmetryFunction/ACSFParametersOptimizer.h @@ -26,6 +26,15 @@ namespace lib4neuro { LIB4NEURO_API void fit_ACSF_parameters(std::vector<SYMMETRY_FUNCTION_PARAMETER>& fitted_params, bool random_init_params); + + LIB4NEURO_API void fit_ACSF_parameters(std::vector<SYMMETRY_FUNCTION_PARAMETER>& fitted_params, + bool random_init_params, + unsigned int main_iterations_num, + unsigned int steady_temperature_iters_num, + double init_temperature, + double temperature_change, + double distribution_stddev, + double termination_threshold); }; } -- GitLab