OpenNN  2.2
Open Neural Networks Library
conjugate_gradient.h
1 /****************************************************************************************************************/
2 /* */
3 /* OpenNN: Open Neural Networks Library */
4 /* www.artelnics.com/opennn */
5 /* */
6 /* C O N J U G A T E G R A D I E N T 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 __CONJUGATEGRADIENT_H__
15 #define __CONJUGATEGRADIENT_H__
16 
17 // System inlcludes
18 
19 #include <string>
20 #include <sstream>
21 #include <iostream>
22 #include <fstream>
23 #include <algorithm>
24 #include <functional>
25 #include <climits>
26 #include <cmath>
27 #include <ctime>
28 
29 // OpenNN includes
30 
31 #include "performance_functional.h"
32 
33 #include "training_algorithm.h"
34 #include "training_rate_algorithm.h"
35 
36 // TinyXml includes
37 
38 #include "../tinyxml2/tinyxml2.h"
39 
40 namespace OpenNN
41 {
42 
46 
48 {
49 
50 public:
51 
52  // ENUMERATIONS
53 
55 
57 
58  // DEFAULT CONSTRUCTOR
59 
60  explicit ConjugateGradient(void);
61 
62 
63  // GENERAL CONSTRUCTOR
64 
66 
67 
68  // XML CONSTRUCTOR
69 
70  explicit ConjugateGradient(const tinyxml2::XMLDocument&);
71 
72 
73  // DESTRUCTOR
74 
75  virtual ~ConjugateGradient(void);
76 
77 
78  // STRUCTURES
79 
83 
85  {
87 
89  {
91  }
92 
94 
95  ConjugateGradientResults(ConjugateGradient* new_conjugate_gradient_pointer)
96  {
97  conjugate_gradient_pointer = new_conjugate_gradient_pointer;
98  }
99 
101 
103  {
104  }
105 
107 
109 
110  // TRAINING HISTORY
111 
113 
115 
117 
119 
121 
123 
125 
127 
129 
131 
133 
135 
137 
139 
141 
143 
145 
147 
148  // FINAL VALUES
149 
151 
153 
155 
157 
159 
161 
163 
165 
167 
169 
171 
173 
175 
177 
179 
181 
183 
184  double elapsed_time;
185 
187 
189 
190  void resize_training_history(const size_t&);
191  std::string to_string(void) const;
192 
193  Matrix<std::string> write_final_results(const size_t& precision = 3) const;
194  };
195 
196 
197  // METHODS
198 
199  // Get methods
200 
203 
204  // Training operators
205 
207  std::string write_training_direction_method(void) const;
208 
209  // Training parameters
210 
211  const double& get_warning_parameters_norm(void) const;
212  const double& get_warning_gradient_norm(void) const;
213  const double& get_warning_training_rate(void) const;
214 
215  const double& get_error_parameters_norm(void) const;
216  const double& get_error_gradient_norm(void) const;
217  const double& get_error_training_rate(void) const;
218 
219  // Stopping criteria
220 
221  const double& get_minimum_parameters_increment_norm(void) const;
222 
223  const double& get_minimum_performance_increase(void) const;
224  const double& get_performance_goal(void) const;
225  const size_t& get_maximum_generalization_performance_decreases(void) const;
226  const double& get_gradient_norm_goal(void) const;
227 
228  const size_t& get_maximum_iterations_number(void) const;
229  const double& get_maximum_time(void) const;
230 
231  // Reserve training history
232 
233  const bool& get_reserve_parameters_history(void) const;
234  const bool& get_reserve_parameters_norm_history(void) const;
235 
236  const bool& get_reserve_performance_history(void) const;
237  const bool& get_reserve_generalization_performance_history(void) const;
238  const bool& get_reserve_gradient_history(void) const;
239  const bool& get_reserve_gradient_norm_history(void) const;
240 
241  const bool& get_reserve_training_direction_history(void) const;
242  const bool& get_reserve_training_rate_history(void) const;
243  const bool& get_reserve_elapsed_time_history(void) const;
244 
245  // Set methods
246 
247  void set_default(void);
248 
250 
251  // Training operators
252 
254  void set_training_direction_method(const std::string&);
255 
256  // Training parameters
257 
258  void set_warning_parameters_norm(const double&);
259  void set_warning_gradient_norm(const double&);
260  void set_warning_training_rate(const double&);
261 
262  void set_error_parameters_norm(const double&);
263  void set_error_gradient_norm(const double&);
264  void set_error_training_rate(const double&);
265 
266  // Stopping criteria
267 
268  void set_minimum_parameters_increment_norm(const double&);
269 
270  void set_performance_goal(const double&);
271  void set_minimum_performance_increase(const double&);
273  void set_gradient_norm_goal(const double&);
274 
275  void set_maximum_iterations_number(const size_t&);
276  void set_maximum_time(const double&);
277 
278  // Reserve training history
279 
280  void set_reserve_parameters_history(const bool&);
281  void set_reserve_parameters_norm_history(const bool&);
282 
283  void set_reserve_performance_history(const bool&);
285  void set_reserve_gradient_history(const bool&);
286  void set_reserve_gradient_norm_history(const bool&);
287 
288  void set_reserve_training_direction_history(const bool&);
289  void set_reserve_training_rate_history(const bool&);
290  void set_reserve_elapsed_time_history(const bool&);
291 
292  void set_reserve_all_training_history(const bool&);
293 
294  // Utilities
295 
296  void set_display_period(const size_t&);
297  void set_save_period(const size_t&);
298 
299  // Training direction methods
300 
301  double calculate_PR_parameter(const Vector<double>&, const Vector<double>&) const;
302  double calculate_FR_parameter(const Vector<double>&, const Vector<double>&) const;
303 
306 
308 
310 
311  // Training methods
312 
314 
315  std::string write_training_algorithm_type(void) const;
316 
317  // Serialization methods
318 
320 
321  tinyxml2::XMLDocument* to_XML(void) const;
322  void from_XML(const tinyxml2::XMLDocument&);
323 
324 private:
325 
327 
329 
331 
333 
335 
337 
339 
341 
343 
345 
347 
349 
351 
353 
355 
357 
358 
359  // STOPPING CRITERIA
360 
362 
364 
366 
368 
370 
372 
374 
376 
379 
381 
383 
385 
387 
388  double maximum_time;
389 
390  // TRAINING HISTORY
391 
393 
395 
397 
399 
401 
403 
405 
407 
409 
411 
413 
415 
417 
419 
421 
423 
425 
427 
428 
429 };
430 
431 }
432 
433 #endif
434 
435 
436 // OpenNN: Open Neural Networks Library.
437 // Copyright (c) 2005-2015 Roberto Lopez.
438 //
439 // This library is free software; you can redistribute it and/or
440 // modify it under the terms of the GNU Lesser General Public
441 // License as published by the Free Software Foundation; either
442 // version 2.1 of the License, or any later version.
443 //
444 // This library is distributed in the hope that it will be useful,
445 // but WITHOUT ANY WARRANTY; without even the implied warranty of
446 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
447 // Lesser General Public License for more details.
448 
449 // You should have received a copy of the GNU Lesser General Public
450 // License along with this library; if not, write to the Free Software
451 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
452 
Vector< double > calculate_FR_training_direction(const Vector< double > &, const Vector< double > &, const Vector< double > &) const
TrainingDirectionMethod training_direction_method
Applied method for calculating the conjugate gradient direction.
Vector< double > calculate_training_direction(const Vector< double > &, const Vector< double > &, const Vector< double > &) const
void set_gradient_norm_goal(const double &)
void set_performance_goal(const double &)
const double & get_warning_parameters_norm(void) const
Returns the minimum value for the norm of the parameters vector at wich a warning message is written ...
double warning_training_rate
Training rate value at wich a warning message is written to the screen.
Vector< Vector< double > > gradient_history
History of the performance function gradient over the training iterations.
double elapsed_time
Elapsed time of the training process.
const bool & get_reserve_performance_history(void) const
Returns true if the performance history vector is to be reserved, and false otherwise.
Matrix< std::string > write_final_results(const size_t &precision=3) const
Returns a default (empty) string matrix with the final results from training.
double maximum_time
Maximum training time. It is used as a stopping criterion.
Vector< double > gradient_norm_history
History of the gradient norm over the training iterations.
const TrainingDirectionMethod & get_training_direction_method(void) const
Returns the conjugate gradient training direction method used for training.
void from_XML(const tinyxml2::XMLDocument &)
const bool & get_reserve_training_rate_history(void) const
Returns true if the training rate history vector is to be reserved, and false otherwise.
void set_reserve_training_rate_history(const bool &)
const bool & get_reserve_parameters_history(void) const
Returns true if the parameters history matrix 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.
double error_training_rate
Training rate at wich the line minimization algorithm is assumed to be unable to bracket a minimum...
double warning_parameters_norm
Value for the parameters 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.
double calculate_FR_parameter(const Vector< double > &, const Vector< double > &) const
void set_reserve_gradient_history(const bool &)
void set_reserve_parameters_norm_history(const bool &)
bool reserve_parameters_history
True if the parameters history matrix is to be reserved, false otherwise.
double final_parameters_norm
Final neural network parameters norm.
const double & get_warning_gradient_norm(void) const
Returns the minimum value for the norm of the gradient vector at wich a warning message is written to...
const double & get_gradient_norm_goal(void) const
std::string to_string(void) const
Returns a string representation of the results structure.
const bool & get_reserve_generalization_performance_history(void) const
Returns true if the Generalization performance history vector is to be reserved, and false otherwise...
void set_warning_parameters_norm(const double &)
double minimum_parameters_increment_norm
Norm of the parameters increment vector at which training stops.
Vector< double > calculate_gradient_descent_training_direction(const Vector< double > &) const
void set_error_training_rate(const double &)
ConjugateGradient * conjugate_gradient_pointer
Pointer to the conjugate gradient object for which the training results are to be stored...
double performance_goal
Goal value for the performance. 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.
Vector< double > performance_history
History of the performance function performance over the training iterations.
const double & get_warning_training_rate(void) const
Returns the training rate value at wich a warning message is written to the screen during line minimi...
void set_reserve_generalization_performance_history(const bool &)
virtual ~ConjugateGradient(void)
Destructor.
void set_maximum_iterations_number(const size_t &)
Vector< Vector< double > > parameters_history
History of the neural network parameters over the training iterations.
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_performance_functional_pointer(PerformanceFunctional *)
bool reserve_parameters_norm_history
True if the parameters norm history vector is to be reserved, false otherwise.
bool reserve_gradient_norm_history
True if the gradient norm history vector is to be reserved, false otherwise.
double warning_gradient_norm
Value for the gradient norm at which a warning message is written to the screen.
ConjugateGradientResults * perform_training(void)
Vector< double > final_parameters
Final neural network parameters vector.
void set_error_parameters_norm(const double &)
ConjugateGradientResults(ConjugateGradient *new_conjugate_gradient_pointer)
Conjugate gradient constructor.
void set_maximum_generalization_performance_decreases(const size_t &)
void set_minimum_parameters_increment_norm(const double &)
void set_reserve_elapsed_time_history(const bool &)
Vector< double > calculate_PR_training_direction(const Vector< double > &, const Vector< double > &, const Vector< double > &) const
void set_minimum_performance_increase(const double &)
bool reserve_performance_history
True if the performance history vector is to be reserved, false otherwise.
std::string write_training_algorithm_type(void) const
This method writes a string with the type of training algoritm.
Vector< double > training_rate_history
History of the training rate over the training iterations.
size_t maximum_iterations_number
Maximum number of iterations to perform_training. It is used as a stopping criterion.
TrainingRateAlgorithm training_rate_algorithm
Training rate algorithm object for one-dimensional minimization.
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 calculate_PR_parameter(const Vector< double > &, const Vector< double > &) const
Vector< double > final_training_direction
Final conjugate gradient training direction.
const double & get_performance_goal(void) const
void set_training_direction_method(const TrainingDirectionMethod &)
const bool & get_reserve_gradient_norm_history(void) const
Returns true if the gradient norm history vector is to be reserved, and false otherwise.
Vector< double > final_gradient
Final performance function gradient.
size_t iterations_number
Maximum number of training iterations.
bool reserve_elapsed_time_history
True if the elapsed time history vector is to be reserved, false otherwise.
double final_training_rate
Final conjugate gradient training rate.
double minimum_performance_increase
Minimum performance improvement between two successive iterations. It is used as a stopping criterion...
Vector< double > generalization_performance_history
History of the generalization performance over the training iterations.
void set_warning_gradient_norm(const double &)
const size_t & get_maximum_generalization_performance_decreases(void) const
Returns the maximum number of generalization failures during the training process.
const bool & get_reserve_elapsed_time_history(void) const
Returns true if the elapsed time history vector is to be reserved, and false otherwise.
tinyxml2::XMLDocument * to_XML(void) const
Matrix< std::string > to_string_matrix(void) const
const bool & get_reserve_training_direction_history(void) const
Returns true if the training direction history matrix is to be reserved, and false otherwise...
std::string write_training_direction_method(void) const
Returns a string with the name of the training direction.
Vector< Vector< double > > training_direction_history
History of the conjugate gradient training direction over the training iterations.
double gradient_norm_goal
Goal value for the norm of the objective function gradient. It is used as a stopping criterion...
const TrainingRateAlgorithm & get_training_rate_algorithm(void) const
Returns a constant reference to the training rate algorithm object inside the conjugate gradient meth...
Vector< double > parameters_norm_history
History of the parameters norm over the training iterations.
void set_error_gradient_norm(const double &)
bool reserve_training_rate_history
True if the training rate history vector is to be reserved, false otherwise.
bool reserve_generalization_performance_history
True if the Generalization performance history vector is to be reserved, false otherwise.
double final_performance
Final performance function evaluation.
void set_maximum_time(const double &)
const bool & get_reserve_parameters_norm_history(void) const
Returns true if the parameters norm history vector is to be reserved, and false otherwise.
bool reserve_training_direction_history
True if the training direction history matrix is to be reserved, false otherwise. ...
TrainingDirectionMethod
Enumeration of the available training operators for obtaining the training direction.
TrainingRateAlgorithm * get_training_rate_algorithm_pointer(void)
Returns a pointer to the training rate algorithm object inside the conjugate gradient method object...
void set_save_period(const size_t &)
void set_warning_training_rate(const double &)
const size_t & get_maximum_iterations_number(void) const
Returns the maximum number of iterations for training.
void set_reserve_gradient_norm_history(const bool &)
void set_reserve_all_training_history(const bool &)
bool reserve_gradient_history
True if the gradient history matrix is to be reserved, false otherwise.
void set_display_period(const size_t &)
const double & get_error_training_rate(void) const
const double & get_maximum_time(void) const
Returns the maximum training time.
double final_generalization_performance
Final generalization performance.
void set_reserve_parameters_history(const bool &)
void set_reserve_training_direction_history(const bool &)
void set_reserve_performance_history(const bool &)
const double & get_error_gradient_norm(void) const
const double & get_error_parameters_norm(void) const
Returns the value for the norm of the parameters vector at wich an error message is written to the sc...
Vector< double > elapsed_time_history
History of the elapsed time over the training iterations.