OpenNN  2.2
Open Neural Networks Library
gradient_descent.h
1 /****************************************************************************************************************/
2 /* */
3 /* OpenNN: Open Neural Networks Library */
4 /* www.artelnics.com/opennn */
5 /* */
6 /* G R A D I E N T D E S C 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 __GRADIENTDESCENT_H__
15 #define __GRADIENTDESCENT_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 <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 
37 namespace OpenNN
38 {
39 
42 
44 {
45 
46 public:
47 
48  // DEFAULT CONSTRUCTOR
49 
50  explicit GradientDescent(void);
51 
52  // PERFORMANCE FUNCTIONAL CONSTRUCTOR
53 
55 
56  // XML CONSTRUCTOR
57 
58  explicit GradientDescent(const tinyxml2::XMLDocument&);
59 
60 
61  // DESTRUCTOR
62 
63  virtual ~GradientDescent(void);
64 
65  // STRUCTURES
66 
70 
72  {
74 
76  {
78  }
79 
81 
82  GradientDescentResults(GradientDescent* new_gradient_descent_pointer)
83  {
84  gradient_descent_pointer = new_gradient_descent_pointer;
85  }
86 
88 
90  {
91  }
92 
94 
96 
97  // Training history
98 
100 
102 
104 
106 
108 
110 
112 
114 
116 
118 
120 
122 
124 
126 
128 
130 
132 
134 
135  // Final values
136 
138 
140 
142 
144 
146 
148 
150 
152 
154 
156 
158 
160 
162 
164 
166 
168 
170 
171  double elapsed_time;
172 
174 
176 
178 
179  std::string stopping_criterion;
180 
181  void resize_training_history(const size_t&);
182 
183  std::string to_string(void) const;
184 
185  Matrix<std::string> write_final_results(const size_t& precision = 3) const;
186  };
187 
188  // METHODS
189 
192 
193  // Training parameters
194 
195  const double& get_warning_parameters_norm(void) const;
196  const double& get_warning_gradient_norm(void) const;
197  const double& get_warning_training_rate(void) const;
198 
199  const double& get_error_parameters_norm(void) const;
200  const double& get_error_gradient_norm(void) const;
201  const double& get_error_training_rate(void) const;
202 
203  // Stopping criteria
204 
205  const double& get_minimum_parameters_increment_norm(void) const;
206 
207  const double& get_minimum_performance_increase(void) const;
208  const double& get_performance_goal(void) const;
209  const double& get_gradient_norm_goal(void) const;
210  const size_t& get_maximum_generalization_performance_decreases(void) const;
211 
212  const size_t& get_maximum_iterations_number(void) const;
213  const double& get_maximum_time(void) const;
214 
215  // Reserve training history
216 
217  const bool& get_reserve_parameters_history(void) const;
218  const bool& get_reserve_parameters_norm_history(void) const;
219 
220  const bool& get_reserve_performance_history(void) const;
221  const bool& get_reserve_gradient_history(void) const;
222  const bool& get_reserve_gradient_norm_history(void) const;
223  const bool& get_reserve_generalization_performance_history(void) const;
224 
225  const bool& get_reserve_training_direction_history(void) const;
226  const bool& get_reserve_training_rate_history(void) const;
227  const bool& get_reserve_elapsed_time_history(void) const;
228 
229  // Set methods
230 
232 
234 
235 
236  void set_default(void);
237 
238  void set_reserve_all_training_history(const bool&);
239 
240 
241  // Training parameters
242 
243  void set_warning_parameters_norm(const double&);
244  void set_warning_gradient_norm(const double&);
245  void set_warning_training_rate(const double&);
246 
247  void set_error_parameters_norm(const double&);
248  void set_error_gradient_norm(const double&);
249  void set_error_training_rate(const double&);
250 
251  // Stopping criteria
252 
253  void set_minimum_parameters_increment_norm(const double&);
254 
255  void set_minimum_performance_increase(const double&);
256  void set_performance_goal(const double&);
257  void set_gradient_norm_goal(const double&);
259 
260  void set_maximum_iterations_number(const size_t&);
261  void set_maximum_time(const double&);
262 
263  // Reserve training history
264 
265  void set_reserve_parameters_history(const bool&);
266  void set_reserve_parameters_norm_history(const bool&);
267 
268  void set_reserve_performance_history(const bool&);
269  void set_reserve_gradient_history(const bool&);
270  void set_reserve_gradient_norm_history(const bool&);
272 
273  void set_reserve_training_direction_history(const bool&);
274  void set_reserve_training_rate_history(const bool&);
275  void set_reserve_elapsed_time_history(const bool&);
276 
277  // Utilities
278 
279  void set_display_period(const size_t&);
280 
281  // Training methods
282 
284 
286 
287  std::string write_training_algorithm_type(void) const;
288 
289  // Serialization methods
290 
292 
293  tinyxml2::XMLDocument* to_XML(void) const;
294  void from_XML(const tinyxml2::XMLDocument&);
295 
296 private:
297 
298  // TRAINING OPERATORS
299 
301 
303 
304  // TRAINING PARAMETERS
305 
307 
309 
311 
313 
315 
317 
319 
321 
323 
325 
327 
329 
330 
331  // STOPPING CRITERIA
332 
334 
336 
338 
340 
342 
344 
346 
348 
351 
353 
355 
357 
359 
360  double maximum_time;
361 
362  // TRAINING HISTORY
363 
365 
367 
369 
371 
373 
375 
377 
379 
381 
383 
385 
387 
389 
391 
393 
395 
397 
399 };
400 
401 }
402 
403 #endif
const bool & get_reserve_elapsed_time_history(void) const
Returns true if the elapsed time 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. ...
bool reserve_training_rate_history
True if the training rate history vector is to be reserved, false otherwise.
Vector< double > training_rate_history
History of the random search training rate over the training iterations.
void set_reserve_all_training_history(const bool &)
void set_performance_goal(const double &)
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_elapsed_time_history(const bool &)
const double & get_minimum_performance_increase(void) const
Returns the minimum performance improvement during training.
TrainingRateAlgorithm * get_training_rate_algorithm_pointer(void)
Returns a pointer to the training rate algorithm object inside the gradient descent object...
Vector< double > final_training_direction
Final gradient descent training direction.
Matrix< std::string > to_string_matrix(void) const
size_t maximum_iterations_number
Maximum number of iterations to perform_training. It is used as a stopping criterion.
double warning_parameters_norm
Value for the parameters norm at which a warning message is written to the screen.
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.
Matrix< std::string > write_final_results(const size_t &precision=3) const
Returns a default (empty) string matrix with the final results from training.
size_t iterations_number
Maximum number of training iterations.
const bool & get_reserve_training_rate_history(void) const
Returns true if the training rate history vector is to be reserved, and false otherwise.
Vector< Vector< double > > gradient_history
History of the performance function gradient over the training iterations.
void set_minimum_performance_increase(const double &)
void set_maximum_generalization_performance_decreases(const size_t &)
double final_parameters_norm
Final neural network parameters norm.
const double & get_gradient_norm_goal(void) const
void set_display_period(const size_t &)
double maximum_time
Maximum training time. It is used as a stopping criterion.
void set_maximum_iterations_number(const size_t &)
const double & get_warning_gradient_norm(void) const
bool reserve_gradient_history
True if the gradient history matrix is to be reserved, false otherwise.
std::string to_string(void) const
Returns a string representation of the results structure.
const double & get_error_gradient_norm(void) const
void set_warning_parameters_norm(const double &)
const TrainingRateAlgorithm & get_training_rate_algorithm(void) const
Returns a constant reference to the training rate algorithm object inside the gradient descent object...
void set_warning_gradient_norm(const double &)
void set_reserve_training_rate_history(const bool &)
void set_reserve_gradient_norm_history(const bool &)
double error_gradient_norm
Value for the gradient norm at which the training process is assumed to fail.
bool reserve_gradient_norm_history
True if the gradient norm history vector is to be reserved, false otherwise.
void set_warning_training_rate(const double &)
double warning_training_rate
Training rate value at wich a warning message is written to the screen.
void set_minimum_parameters_increment_norm(const double &)
Vector< double > generalization_performance_history
History of the generalization performance over the training iterations.
std::string write_training_algorithm_type(void) const
This method writes a string with the type of training algoritm.
GradientDescentResults * perform_training(void)
const double & get_error_training_rate(void) const
Vector< Vector< double > > training_direction_history
History of the random search training direction over the training iterations.
Vector< Vector< double > > parameters_history
History of the neural network parameters over the training iterations.
void set_default(void)
Sets the members of the training algorithm object to their default values.
const bool & get_reserve_training_direction_history(void) const
Returns true if the training direction history matrix is to be reserved, and false otherwise...
double warning_gradient_norm
Value for the gradient norm at which a warning message is written to the screen.
tinyxml2::XMLDocument * to_XML(void) const
double performance_goal
Goal value for the performance. It is used as a stopping criterion.
bool reserve_parameters_history
True if the parameters history matrix is to be reserved, false otherwise.
double final_generalization_performance
Final generalization performance.
void set_error_training_rate(const double &)
const bool & get_reserve_gradient_history(void) const
Returns true if the gradient history vector of vectors is to be reserved, and false otherwise...
Vector< double > calculate_training_direction(const Vector< double > &) const
bool reserve_elapsed_time_history
True if the elapsed time history vector is to be reserved, false otherwise.
TrainingRateAlgorithm training_rate_algorithm
Training rate algorithm object for one-dimensional minimization.
void set_gradient_norm_goal(const double &)
const size_t & get_maximum_generalization_performance_decreases(void) const
Returns the maximum number of generalization failures during the training process.
double error_parameters_norm
Value for the parameters norm at which the training process is assumed to fail.
void set_reserve_training_direction_history(const bool &)
void set_reserve_performance_history(const bool &)
GradientDescentResults(GradientDescent *new_gradient_descent_pointer)
Gradient descent constructor.
void set_reserve_generalization_performance_history(const bool &)
double gradient_norm_goal
Goal value for the norm of the objective function gradient. It is used as a stopping criterion...
double elapsed_time
Elapsed time of the training process.
Vector< double > performance_history
History of the performance function performance over the training iterations.
void set_maximum_time(const double &)
void set_reserve_parameters_norm_history(const bool &)
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 from_XML(const tinyxml2::XMLDocument &)
void set_reserve_gradient_history(const bool &)
void set_error_gradient_norm(const double &)
Vector< double > final_gradient
Final performance function gradient.
const size_t & get_maximum_iterations_number(void) const
Returns the maximum number of iterations for training.
void set_performance_functional_pointer(PerformanceFunctional *)
const double & get_maximum_time(void) const
Returns the maximum training time.
Vector< double > elapsed_time_history
History of the elapsed time over the training iterations.
bool reserve_generalization_performance_history
True if the Generalization performance history vector is to be reserved, false otherwise.
std::string stopping_criterion
Stopping criterion.
const double & get_performance_goal(void) const
const bool & get_reserve_performance_history(void) const
Returns true if the performance history vector is to be reserved, and false otherwise.
double final_performance
Final performance function evaluation.
const double & get_warning_training_rate(void) const
const double & get_error_parameters_norm(void) const
size_t maximum_generalization_performance_decreases
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 minimum_parameters_increment_norm
Norm of the parameters increment vector at which training stops.
const bool & get_reserve_parameters_history(void) const
Returns true if the parameters history matrix is to be reserved, and false otherwise.
double error_training_rate
Training rate at wich the line minimization algorithm is assumed to be unable to bracket a minimum...
void set_error_parameters_norm(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.
Vector< double > parameters_norm_history
History of the parameters norm over the training iterations.
double final_training_rate
Final gradient descent training rate.
const double & get_warning_parameters_norm(void) const
bool reserve_performance_history
True if the performance history vector is to be reserved, false otherwise.
virtual ~GradientDescent(void)
Destructor.
void set_training_rate_algorithm(const TrainingRateAlgorithm &)
GradientDescent * gradient_descent_pointer
Pointer to the gradient descent object for which the training results are to be stored.
Vector< double > final_parameters
Final neural network parameters vector.
Vector< double > gradient_norm_history
History of the gradient norm over the training iterations.
void set_reserve_parameters_history(const bool &)