16 #include "levenberg_marquardt_algorithm.h"
46 std::ostringstream buffer;
48 buffer <<
"OpenNN Exception: LevenbergMarquardtAlgorithm class." << std::endl
49 <<
"explicit LevenbergMarquardtAlgorithm(PerformanceFunctional*) constructor." << std::endl
50 <<
"Performance functional cannot be expressed as a series of sum squared terms." << std::endl;
52 throw std::logic_error(buffer.str());
446 if(new_damping_parameter_factor <= 0.0)
448 std::ostringstream buffer;
450 buffer <<
"OpenNN Exception: LevenbergMarquardtAlgorithm class." << std::endl
451 <<
"void set_damping_parameter_factor(const double&) method." << std::endl
452 <<
"Damping parameter factor must be greater than zero." << std::endl;
454 throw std::logic_error(buffer.str());
472 if(new_minimum_damping_parameter <= 0.0)
474 std::ostringstream buffer;
476 buffer <<
"OpenNN Exception: LevenbergMarquardtAlgorithm class." << std::endl
477 <<
"void set_minimum_damping_parameter(const double&) method." << std::endl
478 <<
"Minimum damping parameter must be greater than zero." << std::endl;
480 throw std::logic_error(buffer.str());
498 if(new_maximum_damping_parameter <= 0.0)
500 std::ostringstream buffer;
502 buffer <<
"OpenNN Exception: LevenbergMarquardtAlgorithm class." << std::endl
503 <<
"void set_maximum_damping_parameter(const double&) method." << std::endl
504 <<
"Maximum damping parameter must be greater than zero." << std::endl;
506 throw std::logic_error(buffer.str());
538 if(new_warning_parameters_norm < 0.0)
540 std::ostringstream buffer;
542 buffer <<
"OpenNN Exception: TrainingAlgorithm class.\n"
543 <<
"void set_warning_parameters_norm(const double&) method.\n"
544 <<
"Warning parameters norm must be equal or greater than 0.\n";
546 throw std::logic_error(buffer.str());
569 if(new_warning_gradient_norm < 0.0)
571 std::ostringstream buffer;
573 buffer <<
"OpenNN Exception: TrainingAlgorithm class.\n"
574 <<
"void set_warning_gradient_norm(const double&) method.\n"
575 <<
"Warning gradient norm must be equal or greater than 0.\n";
577 throw std::logic_error(buffer.str());
600 if(new_error_parameters_norm < 0.0)
602 std::ostringstream buffer;
604 buffer <<
"OpenNN Exception: TrainingAlgorithm class.\n"
605 <<
"void set_error_parameters_norm(const double&) method.\n"
606 <<
"Error parameters norm must be equal or greater than 0.\n";
608 throw std::logic_error(buffer.str());
631 if(new_error_gradient_norm < 0.0)
633 std::ostringstream buffer;
635 buffer <<
"OpenNN Exception: TrainingAlgorithm class.\n"
636 <<
"void set_error_gradient_norm(const double&) method.\n"
637 <<
"Error gradient norm must be equal or greater than 0.\n";
639 throw std::logic_error(buffer.str());
661 if(new_minimum_parameters_increment_norm < 0.0)
663 std::ostringstream buffer;
665 buffer <<
"OpenNN Exception: TrainingAlgorithm class.\n"
666 <<
"void new_minimum_parameters_increment_norm(const double&) method.\n"
667 <<
"Minimum parameters increment norm must be equal or greater than 0.\n";
669 throw std::logic_error(buffer.str());
691 if(new_minimum_performance_increase < 0.0)
693 std::ostringstream buffer;
695 buffer <<
"OpenNN Exception: TrainingAlgorithm class.\n"
696 <<
"void set_minimum_performance_increase(const double&) method.\n"
697 <<
"Minimum performance improvement must be equal or greater than 0.\n";
699 throw std::logic_error(buffer.str());
734 if(new_gradient_norm_goal < 0.0)
736 std::ostringstream buffer;
738 buffer <<
"OpenNN Exception: TrainingAlgorithm class.\n"
739 <<
"void set_gradient_norm_goal(const double&) method.\n"
740 <<
"Gradient norm goal must be equal or greater than 0.\n";
742 throw std::logic_error(buffer.str());
786 if(new_maximum_time < 0.0)
788 std::ostringstream buffer;
790 buffer <<
"OpenNN Exception: TrainingAlgorithm class.\n"
791 <<
"void set_maximum_time(const double&) method.\n"
792 <<
"Maximum time must be equal or greater than 0.\n";
794 throw std::logic_error(buffer.str());
909 if(new_display_period <= 0)
911 std::ostringstream buffer;
913 buffer <<
"OpenNN Exception: TrainingAlgorithm class.\n"
914 <<
"void set_display_period(const double&) method.\n"
915 <<
"First training rate must be greater than 0.\n";
917 throw std::logic_error(buffer.str());
939 std::ostringstream buffer;
943 buffer <<
"OpenNN Exception: LevenbergMarquardtAlgorithm class.\n"
944 <<
"void check(void) const method.\n"
945 <<
"Pointer to performance functional is NULL.\n";
947 throw std::logic_error(buffer.str());
952 if(!data_set_pointer)
954 buffer <<
"OpenNN Exception: LevenbergMarquardtAlgorithm class." << std::endl
955 <<
"void check(void) const method.\n"
956 <<
"The performance funcional has no data set." << std::endl;
958 throw std::logic_error(buffer.str());
963 if(!neural_network_pointer)
965 buffer <<
"OpenNN Exception: LevenbergMarquardtAlgorithm class." << std::endl
966 <<
"void check(void) const method.\n"
967 <<
"Pointer to neural network is NULL." << std::endl;
969 throw std::logic_error(buffer.str());
982 return((terms*terms).calculate_sum());
999 std::ostringstream buffer;
1001 const NeuralNetwork* neural_network_pointer = performance_functional_pointer->get_neural_network_pointer();
1007 if(columns_number != parameters_number)
1009 buffer <<
"OpenNN Exception: LevenbergMarquardtAlgorithm class." << std::endl
1010 <<
"Vector<double> calculate_gradient(const Vector<double>&, const Matrix<double>&) const method." << std::endl
1011 <<
"Number of columns in terms Jacobian must be equal to number of parameters." << std::endl;
1013 throw std::logic_error(buffer.str());
1038 std::ostringstream buffer;
1042 if(columns_number != parameters_number)
1044 buffer <<
"OpenNN Exception: LevenbergMarquardtAlgorithm class." << std::endl
1045 <<
"Matrix<double> calculate_Hessian_approximation(const Matrix<double>&) const method." << std::endl
1046 <<
"Number of columns in terms Jacobian must be equal to number of parameters." << std::endl;
1048 throw std::logic_error(buffer.str());
1070 std::ostringstream buffer;
1072 buffer <<
"OpenNN Exception: LevenbergMarquardtAlgorithmResults structure.\n"
1073 <<
"void resize_training_history(const size_t&) method.\n"
1074 <<
"Levenberg-Marquardt algorithm pointer is NULL.\n";
1076 throw std::logic_error(buffer.str());
1134 std::ostringstream buffer;
1138 if(!parameters_history.empty())
1140 if(!parameters_history[0].empty())
1142 buffer <<
"% Parameters history:\n"
1143 << parameters_history <<
"\n";
1149 if(!parameters_norm_history.empty())
1151 buffer <<
"% Parameters norm history:\n"
1152 << parameters_norm_history <<
"\n";
1157 if(!performance_history.empty())
1159 buffer <<
"% Performance history:\n"
1160 << performance_history <<
"\n";
1165 if(!generalization_performance_history.empty())
1167 buffer <<
"% Generalization performance history:\n"
1168 << generalization_performance_history <<
"\n";
1173 if(!gradient_history.empty())
1175 if(!gradient_history[0].empty())
1177 buffer <<
"% Gradient history:\n"
1178 << gradient_history <<
"\n";
1184 if(!gradient_norm_history.empty())
1186 buffer <<
"% Gradient norm history:\n"
1187 << gradient_norm_history <<
"\n";
1192 if(!Hessian_approximation_history.empty())
1194 if(!Hessian_approximation_history[0].empty())
1196 buffer <<
"% Hessian approximation history:\n"
1197 << Hessian_approximation_history <<
"\n";
1205 buffer <<
"% Damping parameter history:\n"
1211 if(!elapsed_time_history.empty())
1213 buffer <<
"% Elapsed time history:\n"
1214 << elapsed_time_history <<
"\n";
1217 return(buffer.str());
1225 std::ostringstream buffer;
1232 names.push_back(
"Final parameters norm");
1235 buffer << std::setprecision(precision) << final_parameters_norm;
1237 values.push_back(buffer.str());
1241 names.push_back(
"Final performance");
1244 buffer << std::setprecision(precision) << final_performance;
1246 values.push_back(buffer.str());
1254 names.push_back(
"Final generalization performance");
1257 buffer << std::setprecision(precision) << final_generalization_performance;
1259 values.push_back(buffer.str());
1264 names.push_back(
"Final gradient norm");
1267 buffer << std::setprecision(precision) << final_gradient_norm;
1269 values.push_back(buffer.str());
1273 names.push_back(
"Iterations number");
1276 buffer << iterations_number;
1278 values.push_back(buffer.str());
1282 names.push_back(
"Elapsed time");
1285 buffer << elapsed_time;
1287 values.push_back(buffer.str());
1289 const size_t rows_number = names.size();
1290 const size_t columns_number = 2;
1297 return(final_results);
1308 std::ostringstream buffer;
1322 std::cout <<
"Training with Levenberg-Marquardt algorithm...\n";
1337 double parameters_norm;
1349 double performance = 0.0;
1350 double old_performance = 0.0;
1351 double performance_increase = 0.0;
1353 double generalization_performance = 0.0;
1354 double old_generalization_performance = 0.0;
1356 size_t generalization_failures = 0;
1359 Matrix<double> terms_Jacobian(training_instances_number, parameters_number);
1363 double gradient_norm;
1365 Matrix<double> JacobianT_dot_Jacobian(parameters_number, parameters_number);
1367 Matrix<double> Hessian_approximation(parameters_number, parameters_number);
1372 double parameters_increment_norm;
1374 bool stop_training =
false;
1376 time_t beginning_time, current_time;
1377 time(&beginning_time);
1378 double elapsed_time;
1390 std::cout <<
"OpenNN Warning: Parameters norm is " << parameters_norm <<
"." << std::endl;
1407 std::cout <<
"OpenNN Warning: Gradient norm is " << gradient_norm <<
"." << std::endl;
1420 if(new_performance <= performance)
1424 parameters += parameters_increment;
1426 performance = new_performance;
1436 parameters_increment_norm = parameters_increment.
calculate_norm();
1440 performance_increase = 0.0;
1444 performance_increase = old_performance - performance;
1449 if(iteration != 0 && generalization_performance > old_generalization_performance)
1451 generalization_failures++;
1456 time(¤t_time);
1457 elapsed_time = difftime(current_time, beginning_time);
1516 std::cout <<
"Iteration " << iteration <<
": Minimum parameters increment norm reached.\n"
1517 <<
"Parameters increment norm: " << parameters_increment_norm << std::endl;
1520 stop_training =
true;
1527 std::cout <<
"Iteration " << iteration <<
": Performance goal reached.\n";
1530 stop_training =
true;
1538 <<
"Performance increase: " << performance_increase << std::endl;
1541 stop_training =
true;
1548 std::cout <<
"Iteration " << iteration <<
": Gradient norm goal reached." << std::endl;
1551 stop_training =
true;
1558 std::cout <<
"Iteration " << iteration <<
": Maximum generalization performance decreases reached.\n"
1559 <<
"Generalization performance decreases: "<< generalization_failures << std::endl;
1562 stop_training =
true;
1569 std::cout <<
"Iteration " << iteration <<
": Maximum number of iterations reached." << std::endl;
1572 stop_training =
true;
1579 std::cout <<
"Iteration " << iteration <<
": Maximum training time reached." << std::endl;
1582 stop_training =
true;
1585 if(iteration != 0 && iteration %
save_period == 0)
1594 std::cout <<
"Parameters norm: " << parameters_norm <<
"\n"
1595 <<
"performance: " << performance <<
"\n"
1596 <<
"Gradient norm: " << gradient_norm <<
"\n"
1599 <<
"Elapsed time: " << elapsed_time << std::endl;
1601 if(generalization_performance != 0)
1603 std::cout <<
"Generalization performance: " << generalization_performance << std::endl;
1628 std::cout <<
"Iteration " << iteration <<
";\n"
1629 <<
"Parameters norm: " << parameters_norm <<
"\n"
1630 <<
"performance: " << performance <<
"\n"
1631 <<
"Gradient norm: " << gradient_norm <<
"\n"
1634 <<
"Elapsed time: " << elapsed_time << std::endl;
1636 if(generalization_performance != 0)
1638 std::cout <<
"Generalization performance: " << generalization_performance << std::endl;
1645 old_performance = performance;
1646 old_generalization_performance = generalization_performance;
1653 return(results_pointer);
1680 return(
"LEVENBERG_MARQUARDT_ALGORITHM");
1690 std::ostringstream buffer;
1697 labels.push_back(
"Minimum parameters increment norm");
1702 values.push_back(buffer.str());
1706 labels.push_back(
"Minimum performance increase");
1711 values.push_back(buffer.str());
1715 labels.push_back(
"Performance goal");
1720 values.push_back(buffer.str());
1724 labels.push_back(
"Gradient norm goal");
1729 values.push_back(buffer.str());
1733 labels.push_back(
"Maximum generalization failures");
1738 values.push_back(buffer.str());
1742 labels.push_back(
"Maximum iterations number");
1747 values.push_back(buffer.str());
1751 labels.push_back(
"Maximum time");
1756 values.push_back(buffer.str());
1760 labels.push_back(
"Reserve parameters norm history");
1765 values.push_back(buffer.str());
1769 labels.push_back(
"Reserve performance history");
1774 values.push_back(buffer.str());
1778 labels.push_back(
"Reserve gradient norm history");
1783 values.push_back(buffer.str());
1787 labels.push_back(
"Reserve generalization performance history");
1792 values.push_back(buffer.str());
1812 labels.push_back(
"Reserve elapsed time history");
1817 values.push_back(buffer.str());
1819 const size_t rows_number = labels.size();
1820 const size_t columns_number = 2;
1827 return(string_matrix);
1835 std::ostringstream buffer;
1837 tinyxml2::XMLDocument* document =
new tinyxml2::XMLDocument;
1841 tinyxml2::XMLElement* root_element = document->NewElement(
"LevenbergMarquardtAlgorithm");
1842 document->InsertFirstChild(root_element);
1844 tinyxml2::XMLElement* element = NULL;
1845 tinyxml2::XMLText* text = NULL;
1849 element = document->NewElement(
"DampingParameter");
1850 root_element->LinkEndChild(element);
1855 text = document->NewText(buffer.str().c_str());
1856 element->LinkEndChild(text);
1860 element = document->NewElement(
"MinimumDampingParameter");
1861 root_element->LinkEndChild(element);
1866 text = document->NewText(buffer.str().c_str());
1867 element->LinkEndChild(text);
1871 element = document->NewElement(
"MaximumDampingParameter");
1872 root_element->LinkEndChild(element);
1877 text = document->NewText(buffer.str().c_str());
1878 element->LinkEndChild(text);
1882 element = document->NewElement(
"DampingParameterFactor");
1883 root_element->LinkEndChild(element);
1888 text = document->NewText(buffer.str().c_str());
1889 element->LinkEndChild(text);
1893 element = document->NewElement(
"WarningParametersNorm");
1894 root_element->LinkEndChild(element);
1899 text = document->NewText(buffer.str().c_str());
1900 element->LinkEndChild(text);
1904 element = document->NewElement(
"WarningGradientNorm");
1905 root_element->LinkEndChild(element);
1910 text = document->NewText(buffer.str().c_str());
1911 element->LinkEndChild(text);
1915 element = document->NewElement(
"ErrorParametersNorm");
1916 root_element->LinkEndChild(element);
1921 text = document->NewText(buffer.str().c_str());
1922 element->LinkEndChild(text);
1926 element = document->NewElement(
"ErrorGradientNorm");
1927 root_element->LinkEndChild(element);
1932 text = document->NewText(buffer.str().c_str());
1933 element->LinkEndChild(text);
1937 element = document->NewElement(
"MinimumParametersIncrementNorm");
1938 root_element->LinkEndChild(element);
1943 text = document->NewText(buffer.str().c_str());
1944 element->LinkEndChild(text);
1948 element = document->NewElement(
"MinimumPerformanceIncrease");
1949 root_element->LinkEndChild(element);
1954 text = document->NewText(buffer.str().c_str());
1955 element->LinkEndChild(text);
1959 element = document->NewElement(
"PerformanceGoal");
1960 root_element->LinkEndChild(element);
1965 text = document->NewText(buffer.str().c_str());
1966 element->LinkEndChild(text);
1970 element = document->NewElement(
"GradientNormGoal");
1971 root_element->LinkEndChild(element);
1976 text = document->NewText(buffer.str().c_str());
1977 element->LinkEndChild(text);
1981 element = document->NewElement(
"MaximumGeneralizationPerformanceDecreases");
1982 root_element->LinkEndChild(element);
1987 text = document->NewText(buffer.str().c_str());
1988 element->LinkEndChild(text);
1992 element = document->NewElement(
"MaximumIterationsNumber");
1993 root_element->LinkEndChild(element);
1998 text = document->NewText(buffer.str().c_str());
1999 element->LinkEndChild(text);
2003 element = document->NewElement(
"MaximumTime");
2004 root_element->LinkEndChild(element);
2009 text = document->NewText(buffer.str().c_str());
2010 element->LinkEndChild(text);
2014 element = document->NewElement(
"ReserveParametersHistory");
2015 root_element->LinkEndChild(element);
2020 text = document->NewText(buffer.str().c_str());
2021 element->LinkEndChild(text);
2025 element = document->NewElement(
"ReserveParametersNormHistory");
2026 root_element->LinkEndChild(element);
2031 text = document->NewText(buffer.str().c_str());
2032 element->LinkEndChild(text);
2036 element = document->NewElement(
"ReservePerformanceHistory");
2037 root_element->LinkEndChild(element);
2042 text = document->NewText(buffer.str().c_str());
2043 element->LinkEndChild(text);
2047 element = document->NewElement(
"ReserveGeneralizationPerformanceHistory");
2048 root_element->LinkEndChild(element);
2053 text = document->NewText(buffer.str().c_str());
2054 element->LinkEndChild(text);
2058 element = document->NewElement(
"ReserveGradientHistory");
2059 root_element->LinkEndChild(element);
2064 text = document->NewText(buffer.str().c_str());
2065 element->LinkEndChild(text);
2069 element = document->NewElement(
"ReserveGradientNormHistory");
2070 root_element->LinkEndChild(element);
2075 text = document->NewText(buffer.str().c_str());
2076 element->LinkEndChild(text);
2080 element = document->NewElement(
"ReserveHessianApproximationHistory");
2081 root_element->LinkEndChild(element);
2086 text = document->NewText(buffer.str().c_str());
2087 element->LinkEndChild(text);
2091 element = document->NewElement(
"ReserveElapsedTimeHistory");
2092 root_element->LinkEndChild(element);
2097 text = document->NewText(buffer.str().c_str());
2098 element->LinkEndChild(text);
2102 element = document->NewElement(
"ReserveGeneralizationPerformanceHistory");
2103 root_element->LinkEndChild(element);
2108 text = document->NewText(buffer.str().c_str());
2109 element->LinkEndChild(text);
2113 element = document->NewElement(
"DisplayPeriod");
2114 root_element->LinkEndChild(element);
2119 text = document->NewText(buffer.str().c_str());
2120 element->LinkEndChild(text);
2124 element = document->NewElement(
"SavePeriod");
2125 root_element->LinkEndChild(element);
2130 text = document->NewText(buffer.str().c_str());
2131 element->LinkEndChild(text);
2136 element = document->NewElement(
"NeuralNetworkFileName");
2137 root_element->LinkEndChild(element);
2140 element->LinkEndChild(text);
2145 element = document->NewElement(
"Display");
2146 root_element->LinkEndChild(element);
2151 text = document->NewText(buffer.str().c_str());
2152 element->LinkEndChild(text);
2166 const tinyxml2::XMLElement* root_element = document.FirstChildElement(
"LevenbergMarquardtAlgorithm");
2170 std::ostringstream buffer;
2172 buffer <<
"OpenNN Exception: LevenbergMarquardtAlgorithm class.\n"
2173 <<
"void from_XML(const tinyxml2::XMLDocument&) method.\n"
2174 <<
"Levenberg-Marquardt algorithm element is NULL.\n";
2176 throw std::logic_error(buffer.str());
2182 const tinyxml2::XMLElement* damping_parameter_element = root_element->FirstChildElement(
"DampingParameter");
2184 if(damping_parameter_element)
2186 const double new_damping_parameter = atof(damping_parameter_element->GetText());
2192 catch(
const std::logic_error& e)
2194 std::cout << e.what() << std::endl;
2200 const tinyxml2::XMLElement* minimum_damping_parameter_element = root_element->FirstChildElement(
"MinimumDampingParameter");
2202 if(minimum_damping_parameter_element)
2204 const double new_minimum_damping_parameter = atof(minimum_damping_parameter_element->GetText());
2210 catch(
const std::logic_error& e)
2212 std::cout << e.what() << std::endl;
2218 const tinyxml2::XMLElement* maximum_damping_parameter_element = root_element->FirstChildElement(
"MaximumDampingParameter");
2220 if(maximum_damping_parameter_element)
2222 const double new_maximum_damping_parameter = atof(maximum_damping_parameter_element->GetText());
2228 catch(
const std::logic_error& e)
2230 std::cout << e.what() << std::endl;
2236 const tinyxml2::XMLElement* damping_parameter_factor_element = root_element->FirstChildElement(
"DampingParameterFactor");
2238 if(damping_parameter_factor_element)
2240 const double new_damping_parameter_factor = atof(damping_parameter_factor_element->GetText());
2246 catch(
const std::logic_error& e)
2248 std::cout << e.what() << std::endl;
2254 const tinyxml2::XMLElement* warning_parameters_norm_element = root_element->FirstChildElement(
"WarningParametersNorm");
2256 if(warning_parameters_norm_element)
2258 const double new_warning_parameters_norm = atof(warning_parameters_norm_element->GetText());
2264 catch(
const std::logic_error& e)
2266 std::cout << e.what() << std::endl;
2272 const tinyxml2::XMLElement* warning_gradient_norm_element = root_element->FirstChildElement(
"WarningGradientNorm");
2274 if(warning_gradient_norm_element)
2276 const double new_warning_gradient_norm = atof(warning_gradient_norm_element->GetText());
2282 catch(
const std::logic_error& e)
2284 std::cout << e.what() << std::endl;
2290 const tinyxml2::XMLElement* error_parameters_norm_element = root_element->FirstChildElement(
"ErrorParametersNorm");
2292 if(error_parameters_norm_element)
2294 const double new_error_parameters_norm = atof(error_parameters_norm_element->GetText());
2300 catch(
const std::logic_error& e)
2302 std::cout << e.what() << std::endl;
2308 const tinyxml2::XMLElement* error_gradient_norm_element = root_element->FirstChildElement(
"ErrorGradientNorm");
2310 if(error_gradient_norm_element)
2312 const double new_error_gradient_norm = atof(error_gradient_norm_element->GetText());
2318 catch(
const std::logic_error& e)
2320 std::cout << e.what() << std::endl;
2326 const tinyxml2::XMLElement* minimum_parameters_increment_norm_element = root_element->FirstChildElement(
"MinimumParametersIncrementNorm");
2328 if(minimum_parameters_increment_norm_element)
2330 const double new_minimum_parameters_increment_norm = atof(minimum_parameters_increment_norm_element->GetText());
2336 catch(
const std::logic_error& e)
2338 std::cout << e.what() << std::endl;
2344 const tinyxml2::XMLElement* minimum_performance_increase_element = root_element->FirstChildElement(
"MinimumPerformanceIncrease");
2346 if(minimum_performance_increase_element)
2348 const double new_minimum_performance_increase = atof(minimum_performance_increase_element->GetText());
2354 catch(
const std::logic_error& e)
2356 std::cout << e.what() << std::endl;
2362 const tinyxml2::XMLElement* performance_goal_element = root_element->FirstChildElement(
"PerformanceGoal");
2364 if(performance_goal_element)
2366 const double new_performance_goal = atof(performance_goal_element->GetText());
2372 catch(
const std::logic_error& e)
2374 std::cout << e.what() << std::endl;
2380 const tinyxml2::XMLElement* gradient_norm_goal_element = root_element->FirstChildElement(
"GradientNormGoal");
2382 if(gradient_norm_goal_element)
2384 const double new_gradient_norm_goal = atof(gradient_norm_goal_element->GetText());
2390 catch(
const std::logic_error& e)
2392 std::cout << e.what() << std::endl;
2398 const tinyxml2::XMLElement* maximum_generalization_performance_decreases_element = root_element->FirstChildElement(
"MaximumGeneralizationPerformanceDecreases");
2400 if(maximum_generalization_performance_decreases_element)
2402 const size_t new_maximum_generalization_performance_decreases = atoi(maximum_generalization_performance_decreases_element->GetText());
2408 catch(
const std::logic_error& e)
2410 std::cout << e.what() << std::endl;
2416 const tinyxml2::XMLElement* maximum_iterations_number_element = root_element->FirstChildElement(
"MaximumIterationsNumber");
2418 if(maximum_iterations_number_element)
2420 const size_t new_maximum_iterations_number = atoi(maximum_iterations_number_element->GetText());
2426 catch(
const std::logic_error& e)
2428 std::cout << e.what() << std::endl;
2434 const tinyxml2::XMLElement* maximum_time_element = root_element->FirstChildElement(
"MaximumTime");
2436 if(maximum_time_element)
2438 const double new_maximum_time = atof(maximum_time_element->GetText());
2444 catch(
const std::logic_error& e)
2446 std::cout << e.what() << std::endl;
2452 const tinyxml2::XMLElement* reserve_parameters_history_element = root_element->FirstChildElement(
"ReserveParametersHistory");
2454 if(reserve_parameters_history_element)
2456 std::string new_reserve_parameters_history = reserve_parameters_history_element->GetText();
2462 catch(
const std::logic_error& e)
2464 std::cout << e.what() << std::endl;
2470 const tinyxml2::XMLElement* reserve_parameters_norm_history_element = root_element->FirstChildElement(
"ReserveParametersNormHistory");
2472 if(reserve_parameters_norm_history_element)
2474 const std::string new_reserve_parameters_norm_history = reserve_parameters_norm_history_element->GetText();
2480 catch(
const std::logic_error& e)
2482 std::cout << e.what() << std::endl;
2488 const tinyxml2::XMLElement* reserve_performance_history_element = root_element->FirstChildElement(
"ReservePerformanceHistory");
2490 if(reserve_performance_history_element)
2492 const std::string new_reserve_performance_history = reserve_performance_history_element->GetText();
2498 catch(
const std::logic_error& e)
2500 std::cout << e.what() << std::endl;
2506 const tinyxml2::XMLElement* reserve_generalization_performance_history_element = root_element->FirstChildElement(
"ReserveGeneralizationPerformanceHistory");
2508 if(reserve_generalization_performance_history_element)
2510 const std::string new_reserve_generalization_performance_history = reserve_generalization_performance_history_element->GetText();
2516 catch(
const std::logic_error& e)
2518 std::cout << e.what() << std::endl;
2524 const tinyxml2::XMLElement* reserve_gradient_history_element = root_element->FirstChildElement(
"ReserveGradientHistory");
2526 if(reserve_gradient_history_element)
2528 std::string new_reserve_gradient_history = reserve_gradient_history_element->GetText();
2534 catch(
const std::logic_error& e)
2536 std::cout << e.what() << std::endl;
2542 const tinyxml2::XMLElement* reserve_gradient_norm_history_element = root_element->FirstChildElement(
"ReserveGradientNormHistory");
2544 if(reserve_gradient_norm_history_element)
2546 const std::string new_reserve_gradient_norm_history = reserve_gradient_norm_history_element->GetText();
2552 catch(
const std::logic_error& e)
2554 std::cout << e.what() << std::endl;
2560 const tinyxml2::XMLElement* reserve_elapsed_time_history_element = root_element->FirstChildElement(
"ReserveElapsedTimeHistory");
2562 if(reserve_elapsed_time_history_element)
2564 const std::string new_reserve_elapsed_time_history = reserve_elapsed_time_history_element->GetText();
2570 catch(
const std::logic_error& e)
2572 std::cout << e.what() << std::endl;
2578 const tinyxml2::XMLElement* display_period_element = root_element->FirstChildElement(
"DisplayPeriod");
2580 if(display_period_element)
2582 const size_t new_display_period = atoi(display_period_element->GetText());
2588 catch(
const std::logic_error& e)
2590 std::cout << e.what() << std::endl;
2596 const tinyxml2::XMLElement* element = root_element->FirstChildElement(
"SavePeriod");
2600 const size_t new_save_period = atoi(element->GetText());
2606 catch(
const std::logic_error& e)
2608 std::cout << e.what() << std::endl;
2615 const tinyxml2::XMLElement* element = root_element->FirstChildElement(
"NeuralNetworkFileName");
2619 const std::string new_neural_network_file_name = element->GetText();
2625 catch(
const std::logic_error& e)
2627 std::cout << e.what() << std::endl;
2634 const tinyxml2::XMLElement* display_element = root_element->FirstChildElement(
"Display");
2638 const std::string new_display = display_element->GetText();
2644 catch(
const std::logic_error& e)
2646 std::cout << e.what() << std::endl;
2662 const Eigen::Map<Eigen::MatrixXd> A_eigen((
double*)A.data(), n, n);
2663 const Eigen::Map<Eigen::VectorXd> b_eigen((
double*)b.data(), n);
2664 Eigen::Map<Eigen::VectorXd> x_eigen(x.data(), n);
2666 x_eigen = A_eigen.colPivHouseholderQr().solve(b_eigen);
const double & get_minimum_parameters_increment_norm(void) const
Returns the minimum norm of the parameter increment vector used as a stopping criteria when training...
void set_warning_parameters_norm(const double &)
size_t count_parameters_number(void) const
void set_reserve_generalization_performance_history(const bool &)
const bool & get_reserve_generalization_performance_history(void) const
Returns true if the Generalization performance history vector is to be reserved, and false otherwise...
Vector< double > calculate_gradient(const Vector< double > &, const Matrix< double > &) const
double minimum_damping_parameter
Minimum Levenberg-Marquardt parameter.
bool reserve_gradient_norm_history
True if the gradient norm history vector is to be reserved, false otherwise.
void set_error_parameters_norm(const double &)
Vector< double > arrange_parameters(void) const
LevenbergMarquardtAlgorithm * Levenberg_Marquardt_algorithm_pointer
Pointer to the Levenberg-Marquardt algorithm object for which the training results are to be stored...
void set_reserve_parameters_history(const bool &)
void resize_training_history(const size_t &)
size_t count_training_instances_number(void) const
Returns the number of instances in the data set which will be used for training.
void set_display_period(const size_t &)
bool reserve_generalization_performance_history
True if the Generalization performance history vector is to be reserved, false otherwise.
const double & get_performance_goal(void) const
double minimum_parameters_increment_norm
Norm of the parameters increment vector at which training stops.
bool reserve_Hessian_approximation_history
True if the Hessian history vector of matrices is to be reserved, false otherwise.
bool display
Display messages to screen.
const double & get_error_parameters_norm(void) const
double calculate_performance(const Vector< double > &) const
double final_performance
Final performance function evaluation.
virtual void set_reserve_all_training_history(const bool &)
Makes the training history of all variables to be reseved or not in memory.
const size_t & get_maximum_iterations_number(void) const
Returns the maximum number of iterations for training.
double final_generalization_performance
Final generalization performance.
bool reserve_damping_parameter_history
True if the damping parameter history vector is to be reserved, false otherwise.
size_t maximum_iterations_number
Maximum number of iterations to perform_training. It is used as a stopping criterion.
size_t iterations_number
Maximum number of training iterations.
void set_reserve_parameters_norm_history(const bool &)
double minimum_performance_increase
Minimum performance improvement between two successive iterations. It is used as a stopping criterion...
bool reserve_parameters_norm_history
True if the parameters norm history vector is to be reserved, false otherwise.
const bool & get_reserve_elapsed_time_history(void) const
Returns true if the elapsed time history vector is to be reserved, and false otherwise.
void set_reserve_Hessian_approximation_history(const bool &)
double performance_goal
Goal value for the performance. It is used as a stopping criterion.
const double & get_warning_gradient_norm(void) const
void set_save_period(const size_t &)
void set_error_gradient_norm(const double &)
Vector< double > damping_parameter_history
Vector containing the damping parameter history over the training iterations.
const size_t & get_maximum_generalization_performance_decreases(void) const
Returns the maximum number of generalization failures during the training process.
Vector< double > damping_parameter_history
History of the damping parameter over the training iterations.
const Vector< double > & get_damping_parameter_history(void) const
Returns a vector containing the damping parameter history over the training iterations.
double final_parameters_norm
Final neural network parameters norm.
size_t save_period
Number of iterations between the training saving progress.
double warning_parameters_norm
Value for the parameters norm at which a warning message is written to the screen.
Matrix< std::string > write_final_results(const size_t &precision=3) const
Returns a default (empty) string matrix with the final results from training.
void set_minimum_performance_increase(const double &)
Vector< double > gradient_norm_history
History of the gradient norm over the training iterations.
const size_t & get_columns_number(void) const
Returns the number of columns in the matrix.
const bool & get_reserve_gradient_norm_history(void) const
Returns true if the gradient norm history vector is to be reserved, and false otherwise.
std::string write_training_algorithm_type(void) const
This method writes a string with the type of training algoritm.
double calculate_norm(void) const
Returns the vector norm.
void set_reserve_gradient_history(const bool &)
const bool & get_reserve_Hessian_approximation_history(void) const
void set_display(const bool &)
std::string neural_network_file_name
Path where the neural network is saved.
double maximum_damping_parameter
Maximum Levenberg-Marquardt parameter.
const double & get_error_gradient_norm(void) const
void set_neural_network_file_name(const std::string &)
void set_maximum_time(const double &)
tinyxml2::XMLDocument * to_XML(void) const
size_t maximum_generalization_performance_decreases
void set_column(const size_t &, const Vector< T > &)
void set_minimum_damping_parameter(const double &)
double damping_parameter_factor
Damping parameter increase/decrease factor.
Vector< double > generalization_performance_history
History of the generalization performance over the training iterations.
Vector< Vector< double > > gradient_history
History of the performance function gradient over the training iterations.
void from_XML(const tinyxml2::XMLDocument &)
const double & get_maximum_time(void) const
Returns the maximum training time.
double final_gradient_norm
Final gradient norm.
virtual ~LevenbergMarquardtAlgorithm(void)
Vector< double > performance_history
History of the performance function performance over the training iterations.
const double & get_damping_parameter(void) const
Returns the damping parameter for the Hessian approximation.
void set_gradient_norm_goal(const double &)
Vector< Vector< double > > parameters_history
History of the neural network parameters over the training iterations.
const size_t & get_rows_number(void) const
Returns the number of rows in the matrix.
Vector< double > elapsed_time_history
History of the elapsed time over the training iterations.
const bool & get_reserve_gradient_history(void) const
Returns true if the gradient history vector of vectors is to be reserved, and false otherwise...
Matrix< T > calculate_transpose(void) const
Returns the transpose of the matrix.
void set_reserve_damping_parameter_history(const bool &)
void set_minimum_parameters_increment_norm(const double &)
double error_parameters_norm
Value for the parameters norm at which the training process is assumed to fail.
bool reserve_performance_history
True if the performance history vector is to be reserved, false otherwise.
const double & get_warning_parameters_norm(void) const
void set_reserve_elapsed_time_history(const bool &)
void save(const std::string &) const
void set_damping_parameter_factor(const double &)
LevenbergMarquardtAlgorithmResults * perform_training(void)
double maximum_time
Maximum training time. It is used as a stopping criterion.
double error_gradient_norm
Value for the gradient norm at which the training process is assumed to fail.
Matrix< double > calculate_Hessian_approximation(const Matrix< double > &) const
Vector< double > dot(const Vector< double > &) const
LevenbergMarquardtAlgorithm(void)
void set_maximum_iterations_number(const size_t &)
const bool & get_reserve_performance_history(void) const
Returns true if the performance history vector is to be reserved, and false otherwise.
void set_reserve_gradient_norm_history(const bool &)
void set_maximum_damping_parameter(const double &)
const double & get_gradient_norm_goal(void) const
std::string to_string(void) const
Returns a string representation of the current Levenberg-Marquardt algorithm results structure...
bool reserve_parameters_history
True if the parameters history matrix is to be reserved, false otherwise.
void set_damping_parameter(const double &)
const bool & get_reserve_parameters_history(void) const
Returns true if the parameters history matrix is to be reserved, and false otherwise.
void set_warning_gradient_norm(const double &)
Vector< double > final_parameters
Final neural network parameters vector.
double gradient_norm_goal
Goal value for the norm of the objective function gradient. It is used as a stopping criterion...
void set_maximum_generalization_performance_decreases(const size_t &)
Matrix< std::string > to_string_matrix(void) const
PerformanceFunctional * performance_functional_pointer
Pointer to a performance functional for a multilayer perceptron object.
const bool & get_reserve_parameters_norm_history(void) const
Returns true if the parameters norm history vector is to be reserved, and false otherwise.
Matrix< T > sum_diagonal(const T &) const
void set_performance_goal(const double &)
Vector< Matrix< double > > Hessian_approximation_history
History of the Hessian approximation over the training iterations.
double elapsed_time
Elapsed time of the training process.
const double & get_damping_parameter_factor(void) const
Returns the damping parameter factor (beta in the User's Guide) for the Hessian approximation.
size_t display_period
Number of iterations between the training showing progress.
bool reserve_gradient_history
True if the gradient history matrix is to be reserved, false otherwise.
Vector< double > final_gradient
Final performance function gradient.
double damping_parameter
Initial Levenberg-Marquardt parameter.
const double & get_maximum_damping_parameter(void) const
Returns the maximum damping parameter allowed in the algorithm.
Vector< double > perform_Householder_QR_decomposition(const Matrix< double > &, const Vector< double > &) const
Uses Eigen to solve the system of equations by means of the Householder QR decomposition.
const bool & get_reserve_damping_parameter_history(void) const
Returns true if the damping parameter history vector is to be reserved, and false otherwise...
Vector< double > parameters_norm_history
History of the parameters norm over the training iterations.
double warning_gradient_norm
Value for the gradient norm at which a warning message is written to the screen.
const double & get_minimum_performance_increase(void) const
Returns the minimum performance improvement during training.
const double & get_minimum_damping_parameter(void) const
Returns the minimum damping parameter allowed in the algorithm.
bool reserve_elapsed_time_history
True if the elapsed time history vector is to be reserved, false otherwise.
void set_parameters(const Vector< double > &)
void set_reserve_performance_history(const bool &)
const Instances & get_instances(void) const
Returns a constant reference to the instances object composing this data set object.