OpenNN  2.2
Open Neural Networks Library
levenberg_marquardt_algorithm.h
1 /****************************************************************************************************************/
2 /* */
3 /* OpenNN: Open Neural Networks Library */
4 /* www.artelnics.com/opennn */
5 /* */
6 /* L E V E N B E R G - M A R Q U A R D T A L G O R I T H M C L A S S H E A D E R */
7 /* */
8 /* Roberto Lopez */
9 /* Artelnics - Making intelligent use of data */
11 /* */
12 /****************************************************************************************************************/
13 
14 #ifndef __LEVENBERGMARQUARDTALGORITHM_H__
15 #define __LEVENBERGMARQUARDTALGORITHM_H__
16 
17 // System includes
18 
19 #include <string>
20 #include <sstream>
21 #include <iostream>
22 #include <fstream>
23 #include <algorithm>
24 #include <functional>
25 #include <limits>
26 #include <math.h>
27 #include <time.h>
28 
29 // OpenNN includes
30 
31 #include "training_algorithm.h"
32 
33 // TinyXml includes
34 
35 #include "../tinyxml2/tinyxml2.h"
36 
37 namespace OpenNN
38 {
39 
42 
44 {
45 
46 public:
47 
48  // DEFAULT CONSTRUCTOR
49 
50  explicit LevenbergMarquardtAlgorithm(void);
51 
52  // PERFORMANCE FUNCTIONAL CONSTRUCTOR
53 
55 
56  // XML CONSTRUCTOR
57 
58  explicit LevenbergMarquardtAlgorithm(const tinyxml2::XMLDocument&);
59 
60  // DESTRUCTOR
61 
62  virtual ~LevenbergMarquardtAlgorithm(void);
63 
64  // STRUCTURES
65 
69 
71  {
73 
75  {
77  }
78 
80 
81  LevenbergMarquardtAlgorithmResults(LevenbergMarquardtAlgorithm* new_Levenberg_Marquardt_algorithm_pointer)
82  {
83  Levenberg_Marquardt_algorithm_pointer = new_Levenberg_Marquardt_algorithm_pointer;
84  }
85 
87 
89  {
90  }
91 
93 
95 
96  // Training history
97 
99 
101 
103 
105 
107 
109 
111 
113 
115 
117 
119 
121 
123 
125 
127 
129 
131 
133 
134  // Final values
135 
137 
139 
141 
143 
145 
147 
149 
151 
153 
155 
157 
159 
161 
162  double elapsed_time;
163 
165 
167 
168  void resize_training_history(const size_t&);
169  std::string to_string(void) const;
170 
171  Matrix<std::string> write_final_results(const size_t& precision = 3) const;
172 
173  };
174 
175  // METHODS
176 
177  // Get methods
178 
179  // Training parameters
180 
181  const double& get_warning_parameters_norm(void) const;
182  const double& get_warning_gradient_norm(void) const;
183 
184  const double& get_error_parameters_norm(void) const;
185  const double& get_error_gradient_norm(void) const;
186 
187  // Stopping criteria
188 
189  const double& get_minimum_parameters_increment_norm(void) const;
190 
191  const double& get_minimum_performance_increase(void) const;
192  const double& get_performance_goal(void) const;
193  const double& get_gradient_norm_goal(void) const;
194  const size_t& get_maximum_generalization_performance_decreases(void) const;
195 
196  const size_t& get_maximum_iterations_number(void) const;
197  const double& get_maximum_time(void) const;
198 
199  // Reserve training history
200 
201  const bool& get_reserve_parameters_history(void) const;
202  const bool& get_reserve_parameters_norm_history(void) const;
203 
204  const bool& get_reserve_performance_history(void) const;
205  const bool& get_reserve_gradient_history(void) const;
206  const bool& get_reserve_gradient_norm_history(void) const;
207  const bool& get_reserve_Hessian_approximation_history(void) const;
208  const bool& get_reserve_generalization_performance_history(void) const;
209 
210  const bool& get_reserve_elapsed_time_history(void) const;
211 
212  // Utilities
213 
214  const double& get_damping_parameter(void) const;
215 
216  const double& get_damping_parameter_factor(void) const;
217 
218  const double& get_minimum_damping_parameter(void) const;
219  const double& get_maximum_damping_parameter(void) const;
220 
221  const bool& get_reserve_damping_parameter_history(void) const;
222 
224 
225  // Set methods
226 
227  void set_default(void);
228 
229  void set_damping_parameter(const double&);
230 
231  void set_damping_parameter_factor(const double&);
232 
233  void set_minimum_damping_parameter(const double&);
234  void set_maximum_damping_parameter(const double&);
235 
236  void set_reserve_damping_parameter_history(const bool&);
237 
238  // Training parameters
239 
240  void set_warning_parameters_norm(const double&);
241  void set_warning_gradient_norm(const double&);
242 
243  void set_error_parameters_norm(const double&);
244  void set_error_gradient_norm(const double&);
245 
246  // Stopping criteria
247 
248  void set_minimum_parameters_increment_norm(const double&);
249 
250  void set_minimum_performance_increase(const double&);
251  void set_performance_goal(const double&);
252  void set_gradient_norm_goal(const double&);
254 
255  void set_maximum_iterations_number(const size_t&);
256  void set_maximum_time(const double&);
257 
258  // Reserve training history
259 
260  void set_reserve_parameters_history(const bool&);
261  void set_reserve_parameters_norm_history(const bool&);
262 
263  void set_reserve_performance_history(const bool&);
264  void set_reserve_gradient_history(const bool&);
265  void set_reserve_gradient_norm_history(const bool&);
268 
269  void set_reserve_elapsed_time_history(const bool&);
270 
272 
273  virtual void set_reserve_all_training_history(const bool&);
274 
275  // Utilities
276 
277  void set_display_period(const size_t&);
278 
279  // Training methods
280 
281  void check(void) const;
282 
283  double calculate_performance(const Vector<double>&) const;
286 
288 
289  std::string write_training_algorithm_type(void) const;
290 
291  // Serialization methods
292 
294 
295  tinyxml2::XMLDocument* to_XML(void) const;
296 
297  void from_XML(const tinyxml2::XMLDocument&);
298 
300 
301 
302 private:
303 
304  // MEMBERS
305 
307 
309 
311 
313 
315 
317 
319 
321 
323 
325 
327 
329 
330 
332 
334 
336 
338 
340 
342 
344 
346 
347 
348  // STOPPING CRITERIA
349 
351 
353 
355 
357 
359 
361 
363 
365 
368 
370 
372 
374 
376 
377  double maximum_time;
378 
379  // TRAINING HISTORY
380 
382 
384 
386 
388 
390 
392 
394 
396 
398 
400 
402 
404 
406 
408 
410 
412 
413 };
414 
415 }
416 
417 #endif
418 
419 
420 // OpenNN: Open Neural Networks Library.
421 // Copyright (c) 2005-2015 Roberto Lopez.
422 //
423 // This library is free software; you can redistribute it and/or
424 // modify it under the terms of the GNU Lesser General Public
425 // License as published by the Free Software Foundation; either
426 // version 2.1 of the License, or any later version.
427 //
428 // This library is distributed in the hope that it will be useful,
429 // but WITHOUT ANY WARRANTY; without even the implied warranty of
430 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
431 // Lesser General Public License for more details.
432 
433 // You should have received a copy of the GNU Lesser General Public
434 // License along with this library; if not, write to the Free Software
435 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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...
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.
LevenbergMarquardtAlgorithm * Levenberg_Marquardt_algorithm_pointer
Pointer to the Levenberg-Marquardt algorithm object for which the training results are to be stored...
bool reserve_generalization_performance_history
True if the Generalization performance history vector is to be reserved, false otherwise.
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.
double calculate_performance(const Vector< double > &) const
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.
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.
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.
double performance_goal
Goal value for the performance. It is used as a stopping criterion.
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 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.
Vector< double > gradient_norm_history
History of the gradient norm over the training iterations.
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.
const bool & get_reserve_Hessian_approximation_history(void) const
double maximum_damping_parameter
Maximum Levenberg-Marquardt parameter.
tinyxml2::XMLDocument * to_XML(void) const
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.
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.
Vector< Vector< double > > parameters_history
History of the neural network parameters over the training iterations.
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...
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
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
const bool & get_reserve_performance_history(void) const
Returns true if the performance history vector is to be reserved, and false otherwise.
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.
const bool & get_reserve_parameters_history(void) const
Returns true if the parameters history matrix is to be reserved, and false otherwise.
LevenbergMarquardtAlgorithmResults(LevenbergMarquardtAlgorithm *new_Levenberg_Marquardt_algorithm_pointer)
Random search constructor.
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...
Matrix< std::string > to_string_matrix(void) const
const bool & get_reserve_parameters_norm_history(void) const
Returns true if the parameters norm history vector is to be reserved, and false otherwise.
Vector< Matrix< double > > Hessian_approximation_history
History of the Hessian approximation over the training iterations.
const double & get_damping_parameter_factor(void) const
Returns the damping parameter factor (beta in the User's Guide) for the Hessian approximation.
bool reserve_gradient_history
True if the gradient history matrix is to be reserved, false otherwise.
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.