OpenNN  2.2
Open Neural Networks Library
newton_method.h
1 /****************************************************************************************************************/
2 /* */
3 /* OpenNN: Open Neural Networks Library */
4 /* www.artelnics.com/opennn */
5 /* */
6 /* N E W T O N M E T H O D 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 __NEWTONMETHOD_H__
15 #define __NEWTONMETHOD_H__
16 
17 // System includes
18 
19 #include <string>
20 #include <sstream>
21 #include <iostream>
22 #include <fstream>
23 #include <stdlib.h>
24 #include <math.h>
25 #include <time.h>
26 
27 // OpenNN includes
28 
29 #include "performance_functional.h"
30 
31 #include "training_algorithm.h"
32 #include "training_rate_algorithm.h"
33 
34 // TinyXml includes
35 
36 #include "../tinyxml2/tinyxml2.h"
37 
38 namespace OpenNN
39 {
40 
44 
46 {
47 
48 public:
49 
50  // DEFAULT CONSTRUCTOR
51 
52  explicit NewtonMethod(void);
53 
54  // PERFORMANCE FUNCTIONAL CONSTRUCTOR
55 
57 
58  // XML CONSTRUCTOR
59 
60  explicit NewtonMethod(const tinyxml2::XMLDocument&);
61 
62 
63  // DESTRUCTOR
64 
65  virtual ~NewtonMethod(void);
66 
67 
68  // STRUCTURES
69 
73 
75  {
77 
79  {
80  Newton_method_pointer = NULL;
81  }
82 
84 
85  NewtonMethodResults(NewtonMethod* new_Newton_method_pointer)
86  {
87  Newton_method_pointer = new_Newton_method_pointer;
88  }
89 
91 
92  virtual ~NewtonMethodResults(void)
93  {
94  }
95 
97 
99 
100  // Training history
101 
103 
105 
107 
109 
111 
113 
115 
117 
119 
121 
123 
125 
127 
129 
131 
133 
135 
137 
139 
141 
142  // Final values
143 
145 
147 
149 
151 
153 
155 
157 
159 
161 
163 
165 
167 
169 
171 
173 
175 
177 
178  double elapsed_time;
179 
181 
183 
184  void resize_training_history(const size_t&);
185  std::string to_string(void) const;
186 
187  Matrix<std::string> write_final_results(const size_t& precision = 3) const;
188  };
189 
190 
191  // METHODS
192 
195 
196  // Training parameters
197 
198  const double& get_warning_parameters_norm(void) const;
199  const double& get_warning_gradient_norm(void) const;
200  const double& get_warning_training_rate(void) const;
201 
202  const double& get_error_parameters_norm(void) const;
203  const double& get_error_gradient_norm(void) const;
204  const double& get_error_training_rate(void) const;
205 
206  // Stopping criteria
207 
208  const double& get_minimum_parameters_increment_norm(void) const;
209 
210  const double& get_minimum_performance_increase(void) const;
211  const double& get_performance_goal(void) const;
212  const double& get_gradient_norm_goal(void) const;
213  const size_t& get_maximum_generalization_performance_decreases(void) const;
214 
215  const size_t& get_maximum_iterations_number(void) const;
216  const double& get_maximum_time(void) const;
217 
218  // Reserve training history
219 
220  const bool& get_reserve_parameters_history(void) const;
221  const bool& get_reserve_parameters_norm_history(void) const;
222 
223  const bool& get_reserve_performance_history(void) const;
224  const bool& get_reserve_gradient_history(void) const;
225  const bool& get_reserve_gradient_norm_history(void) const;
226  const bool& get_reserve_inverse_Hessian_history(void) const;
227  const bool& get_reserve_generalization_performance_history(void) const;
228 
229  const bool& get_reserve_training_direction_history(void) const;
230  const bool& get_reserve_training_rate_history(void) const;
231  const bool& get_reserve_elapsed_time_history(void) const;
232 
233  // Utilities
234 
236 
237  void set_default(void);
238 
239  // Training parameters
240 
241  void set_warning_parameters_norm(const double&);
242  void set_warning_gradient_norm(const double&);
243  void set_warning_training_rate(const double&);
244 
245  void set_error_parameters_norm(const double&);
246  void set_error_gradient_norm(const double&);
247  void set_error_training_rate(const double&);
248 
249  // Stopping criteria
250 
251  void set_minimum_parameters_increment_norm(const double&);
252 
253  void set_minimum_performance_increase(const double&);
254  void set_performance_goal(const double&);
255  void set_gradient_norm_goal(const double&);
257 
258  void set_maximum_iterations_number(const size_t&);
259  void set_maximum_time(const double&);
260 
261  // Reserve training history
262 
263  void set_reserve_parameters_history(const bool&);
264  void set_reserve_parameters_norm_history(const bool&);
265 
266  void set_reserve_performance_history(const bool&);
267  void set_reserve_gradient_history(const bool&);
268  void set_reserve_gradient_norm_history(const bool&);
269  void set_reserve_inverse_Hessian_history(const bool&);
271 
272  void set_reserve_training_direction_history(const bool&);
273  void set_reserve_training_rate_history(const bool&);
274  void set_reserve_elapsed_time_history(const bool&);
275 
277 
278  void set_reserve_all_training_history(const bool&);
279 
280  // Utilities
281 
282  void set_display_period(const size_t&);
283 
284  // Training methods
285 
288 
290 
291  std::string write_training_algorithm_type(void) const;
292 
293  // Serialization methods
294 
296 
297  tinyxml2::XMLDocument* to_XML(void) const;
298  void from_XML(const tinyxml2::XMLDocument&);
299 
300 private:
301 
304 
306 
308 
310 
312 
314 
316 
318 
320 
322 
324 
326 
328 
330 
331 
332  // STOPPING CRITERIA
333 
335 
337 
339 
341 
343 
345 
347 
349 
352 
354 
356 
358 
360 
361  double maximum_time;
362 
363  // TRAINING HISTORY
364 
366 
368 
370 
372 
374 
376 
378 
380 
382 
384 
386 
388 
390 
392 
394 
396 
398 
400 
402 
404 };
405 
406 }
407 
408 #endif
409 
410 
411 // OpenNN: Open Neural Networks Library.
412 // Copyright (c) 2005-2015 Roberto Lopez.
413 //
414 // This library is free software; you can redistribute it and/or
415 // modify it under the terms of the GNU Lesser General Public
416 // License as published by the Free Software Foundation; either
417 // version 2.1 of the License, or any later version.
418 //
419 // This library is distributed in the hope that it will be useful,
420 // but WITHOUT ANY WARRANTY; without even the implied warranty of
421 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
422 // Lesser General Public License for more details.
423 
424 // You should have received a copy of the GNU Lesser General Public
425 // License along with this library; if not, write to the Free Software
426 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
427 
void set_error_gradient_norm(const double &)
bool reserve_parameters_history
True if the parameters history matrix is to be reserved, false otherwise.
void set_reserve_parameters_norm_history(const bool &)
const bool & get_reserve_inverse_Hessian_history(void) const
Returns true if the inverse Hessian history vector of matrices is to be reserved, and false otherwise...
NewtonMethodResults * perform_training(void)
void set_minimum_parameters_increment_norm(const double &)
tinyxml2::XMLDocument * to_XML(void) const
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 final_training_rate
Final Newton method training rate.
void set_error_parameters_norm(const double &)
double final_generalization_performance
Final generalization performance.
double warning_parameters_norm
Value for the parameters norm at which a warning message is written to the screen.
void set_reserve_generalization_performance_history(const bool &)
double final_parameters_norm
Final neural network parameters norm.
void set_error_training_rate(const double &)
std::string to_string(void) const
Returns a string representation of the current Newton method results structure.
size_t iterations_number
Maximum number of 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...
size_t maximum_generalization_performance_decreases
const bool & get_reserve_training_rate_history(void) const
Returns true if the training rate history vector is to be reserved, and false otherwise.
const double & get_error_training_rate(void) const
const double & get_error_parameters_norm(void) const
size_t maximum_iterations_number
Maximum number of iterations to perform_training. It is used as a stopping criterion.
void set_display_period(const size_t &)
void set_maximum_iterations_number(const size_t &)
const bool & get_reserve_gradient_norm_history(void) const
Returns true if the gradient norm history vector is to be reserved, and false otherwise.
void set_reserve_training_direction_history(const bool &)
void set_reserve_inverse_Hessian_history(const bool &)
Matrix< std::string > to_string_matrix(void) const
const double & get_gradient_norm_goal(void) const
void set_warning_gradient_norm(const double &)
void set_reserve_elapsed_time_history(const bool &)
virtual ~NewtonMethodResults(void)
Destructor.
Definition: newton_method.h:92
const bool & get_reserve_performance_history(void) const
Returns true if the performance history vector is to be reserved, and false otherwise.
Vector< Vector< double > > gradient_history
History of the performance function gradient over the training iterations.
Vector< double > calculate_gradient_descent_training_direction(const Vector< double > &) const
void set_reserve_performance_history(const bool &)
void from_XML(const tinyxml2::XMLDocument &)
NewtonMethodResults(NewtonMethod *new_Newton_method_pointer)
Newton method constructor.
Definition: newton_method.h:85
const bool & get_reserve_generalization_performance_history(void) const
Returns true if the Generalization performance history vector is to be reserved, and false otherwise...
double elapsed_time
Elapsed time of the training process.
double final_performance
Final performance function evaluation.
bool reserve_performance_history
True if the performance history vector is to be reserved, false otherwise.
const double & get_warning_gradient_norm(void) const
TrainingRateAlgorithm training_rate_algorithm
const double & get_performance_goal(void) const
Vector< Vector< double > > training_direction_history
History of the random search training direction over the training iterations.
Vector< double > performance_history
History of the performance function performance 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...
Vector< double > elapsed_time_history
History of the elapsed time over the training iterations.
Vector< double > final_gradient
Final performance function gradient.
void set_reserve_gradient_norm_history(const bool &)
void set_maximum_time(const double &)
const double & get_error_gradient_norm(void) const
void set_performance_functional_pointer(PerformanceFunctional *)
const bool & get_reserve_parameters_norm_history(void) const
Returns true if the parameters norm history vector is to be reserved, and false otherwise.
NewtonMethod * Newton_method_pointer
Pointer to the Newton method object for which the training results are to be stored.
Definition: newton_method.h:98
bool reserve_elapsed_time_history
True if the elapsed time 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.
double performance_goal
Goal value for the performance. It is used as a stopping criterion.
double error_training_rate
Training rate at wich the line minimization algorithm is assumed to be unable to bracket a minimum...
void set_default(void)
Sets the members of the training algorithm object to their default values.
Vector< double > gradient_norm_history
History of the gradient norm over the training iterations.
double error_parameters_norm
Value for the parameters norm at which the training process is assumed to fail.
bool reserve_inverse_Hessian_history
True if the inverse Hessian history vector of matrices is to be reserved, false otherwise.
Vector< double > calculate_training_direction(const Vector< double > &, const Matrix< double > &) const
void set_reserve_parameters_history(const bool &)
bool reserve_gradient_norm_history
True if the gradient norm history vector is to be reserved, false otherwise.
const double & get_maximum_time(void) const
Returns the maximum training time.
void set_reserve_gradient_history(const bool &)
void set_performance_goal(const double &)
Vector< double > final_training_direction
Final Newton method training direction.
bool reserve_training_direction_history
True if the training direction history matrix is to be reserved, false otherwise. ...
Vector< double > training_rate_history
History of the random search training rate over the training iterations.
bool reserve_training_rate_history
True if the training rate history vector is to be reserved, false otherwise.
TrainingRateAlgorithm * get_training_rate_algorithm_pointer(void)
Returns a pointer to the training rate algorithm object inside the Newton method object.
void set_reserve_all_training_history(const bool &)
Makes the training history of all variables to be reseved or not in memory.
const double & get_warning_training_rate(void) const
double final_gradient_norm
Final gradient norm.
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.
bool reserve_gradient_history
True if the gradient history matrix is to be reserved, false otherwise.
const size_t & get_maximum_generalization_performance_decreases(void) const
Returns the maximum number of generalization failures during the training process.
void set_gradient_norm_goal(const double &)
double warning_gradient_norm
Value for the gradient norm at which a warning message is written to the screen.
Vector< Vector< double > > parameters_history
History of the neural network parameters over the training iterations.
const size_t & get_maximum_iterations_number(void) const
Returns the maximum number of iterations for training.
void set_warning_training_rate(const double &)
virtual ~NewtonMethod(void)
Destructor.
void set_minimum_performance_increase(const double &)
void set_maximum_generalization_performance_decreases(const size_t &)
const TrainingRateAlgorithm & get_training_rate_algorithm(void) const
Returns a constant reference to the training rate algorithm object inside the Newton method object...
Matrix< std::string > write_final_results(const size_t &precision=3) const
Returns a default (empty) string matrix with the final results from training.
NewtonMethodResults(void)
Default constructor.
Definition: newton_method.h:78
void set_warning_parameters_norm(const double &)
Vector< double > final_parameters
Final neural network parameters vector.
double error_gradient_norm
Value for the gradient norm at which the training process is assumed to fail.
const bool & get_reserve_training_direction_history(void) const
Returns true if the training direction history matrix is to be reserved, and false otherwise...
Vector< double > parameters_norm_history
History of the parameters norm 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...
bool reserve_generalization_performance_history
True if the Generalization performance history vector is to be reserved, false otherwise.
Vector< Matrix< double > > inverse_Hessian_history
History of the inverse Hessian over the training iterations.
Vector< double > generalization_performance_history
History of the generalization performance over the training iterations.
double minimum_parameters_increment_norm
Norm of the parameters increment vector at which training stops.
double maximum_time
Maximum training time. It is used as a stopping criterion.
double warning_training_rate
Training rate value at wich a warning message is written to the screen.
const double & get_warning_parameters_norm(void) const
double minimum_performance_increase
Minimum performance improvement between two successive iterations. It is used as a stopping criterion...
const double & get_minimum_performance_increase(void) const
Returns the minimum performance improvement during training.
bool reserve_parameters_norm_history
True if the parameters norm history vector is to be reserved, false otherwise.