OpenNN  2.2
Open Neural Networks Library
training_rate_algorithm.h
1 /****************************************************************************************************************/
2 /* */
3 /* OpenNN: Open Neural Networks Library */
4 /* www.artelnics.com/opennn */
5 /* */
6 /* T R A I N I N G R A T E 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 __TRAININGRATEALGORITHM_H__
15 #define __TRAININGRATEALGORITHM_H__
16 
17 // System includes
18 
19 #include <iostream>
20 #include <fstream>
21 #include <algorithm>
22 #include <functional>
23 #include <limits>
24 #include <cmath>
25 #include <ctime>
26 
27 // OpenNN includes
28 
29 #include "neural_network.h"
30 #include "performance_functional.h"
31 
32 // TinyXml includes
33 
34 #include "../tinyxml2/tinyxml2.h"
35 
36 namespace OpenNN
37 {
38 
41 
43 {
44 
45 public:
46 
47  // ENUMERATIONS
48 
50 
51  enum TrainingRateMethod{Fixed, GoldenSection, BrentMethod};
52 
53  // DEFAULT CONSTRUCTOR
54 
55  explicit TrainingRateAlgorithm(void);
56 
57  // GENERAL CONSTRUCTOR
58 
60 
61  // XML CONSTRUCTOR
62 
63  explicit TrainingRateAlgorithm(const tinyxml2::XMLDocument&);
64 
65  // DESTRUCTOR
66 
67  virtual ~TrainingRateAlgorithm(void);
68 
72 
73  struct Triplet
74  {
76 
77  Triplet(void)
78  {
79  A.set(2, 0.0);
80  U.set(2, 0.0);
81  B.set(2, 0.0);
82  }
83 
85 
86  virtual ~Triplet(void)
87  {
88  }
89 
94 
95  inline bool operator == (const Triplet& other_triplet) const
96  {
97  if(A == other_triplet.A
98  && U == other_triplet.U
99  && B == other_triplet.B)
100  {
101  return(true);
102  }
103  else
104  {
105  return(false);
106  }
107  }
108 
111 
112  inline bool has_length_zero(void) const
113  {
114  if(A[0] == B[0])
115  {
116  return(true);
117  }
118  else
119  {
120  return(false);
121  }
122  }
123 
125 
126  inline std::string to_string(void) const
127  {
128  std::ostringstream buffer;
129 
130  buffer << "A = (" << A[0] << "," << A[1] << ")\n"
131  << "U = (" << U[0] << "," << U[1] << ")\n"
132  << "B = (" << B[0] << "," << B[1] << ")" << std::endl;
133 
134  return(buffer.str());
135  }
136 
138 
139  inline void print(void) const
140  {
141  std::cout << to_string();
142  }
143 
147 
148  inline void check(void) const
149  {
150  std::ostringstream buffer;
151 
152  if(A[0] > U[0] || U[0] > B[0])
153  {
154  buffer << "OpenNN Exception: TrainingRateAlgorithm class.\n"
155  << "void check(void) const method.\n"
156  << "Uncorrect triplet:\n"
157  << to_string();
158 
159  throw std::logic_error(buffer.str());
160  }
161 
162  if(A[1] < U[1] || U[1] > B[1])
163  {
164  buffer << "OpenNN Exception: TrainingRateAlgorithm class.\n"
165  << "void check(void) const method.\n"
166  << "Triplet does not satisfy minimum condition:\n"
167  << to_string();
168 
169  throw std::logic_error(buffer.str());
170  }
171  }
172 
174 
176 
178 
180 
182 
184  };
185 
186 
187  // METHODS
188 
189  // Get methods
190 
192 
193  bool has_performance_functional(void) const;
194 
195  // Training operators
196 
197  const TrainingRateMethod& get_training_rate_method(void) const;
198  std::string write_training_rate_method(void) const;
199 
200  // Training parameters
201 
202  const double& get_bracketing_factor(void) const;
203  const double& get_training_rate_tolerance(void) const;
204 
205  const double& get_warning_training_rate(void) const;
206 
207  const double& get_error_training_rate(void) const;
208 
209  // Utilities
210 
211  const bool& get_display(void) const;
212 
213  // Set methods
214 
215  void set(void);
216  void set(PerformanceFunctional*);
217 
219 
220  // Training operators
221 
223  void set_training_rate_method(const std::string&);
224 
225  // Training parameters
226 
227  void set_bracketing_factor(const double&);
228  void set_training_rate_tolerance(const double&);
229 
230  void set_warning_training_rate(const double&);
231 
232  void set_error_training_rate(const double&);
233 
234  // Utilities
235 
236  void set_display(const bool&);
237 
238  virtual void set_default(void);
239 
240  // Training rate method
241 
242  double calculate_golden_section_training_rate(const Triplet&) const;
243  double calculate_Brent_method_training_rate(const Triplet&) const;
244 
245  Triplet calculate_bracketing_triplet(const double&, const Vector<double>&, const double&) const;
246 
247  Vector<double> calculate_fixed_directional_point(const double&, const Vector<double>&, const double&) const;
248  Vector<double> calculate_golden_section_directional_point(const double&, const Vector<double>&, const double&) const;
249  Vector<double> calculate_Brent_method_directional_point(const double&, const Vector<double>&, const double&) const;
250 
251  Vector<double> calculate_directional_point(const double&, const Vector<double>&, const double&) const;
252 
253  // Serialization methods
254 
255  tinyxml2::XMLDocument* to_XML(void) const;
256  void from_XML(const tinyxml2::XMLDocument&);
257 
258 
259 protected:
260 
261  // FIELDS
262 
264 
266 
267 
268  // TRAINING OPERATORS
269 
271 
273 
275 
277 
279 
281 
283 
285 
287 
289 
290  // UTILITIES
291 
293 
294  bool display;
295 };
296 
297 }
298 
299 #endif
300 
301 
302 // OpenNN: Open Neural Networks Library.
303 // Copyright (c) 2005-2015 Roberto Lopez.
304 //
305 // This library is free software; you can redistribute it and/or
306 // modify it under the terms of the GNU Lesser General Public
307 // License as published by the Free Software Foundation; either
308 // version 2.1 of the License, or any later version.
309 //
310 // This library is distributed in the hope that it will be useful,
311 // but WITHOUT ANY WARRANTY; without even the implied warranty of
312 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
313 // Lesser General Public License for more details.
314 
315 // You should have received a copy of the GNU Lesser General Public
316 // License along with this library; if not, write to the Free Software
317 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
318 
TrainingRateMethod training_rate_method
Variable containing the actual method used to obtain a suitable perform_training rate.
double calculate_Brent_method_training_rate(const Triplet &) const
PerformanceFunctional * get_performance_functional_pointer(void) const
Vector< double > A
Left point of the triplet.
void print(void) const
Prints the triplet points to the standard output.
const double & get_error_training_rate(void) const
void set_performance_functional_pointer(PerformanceFunctional *)
void set(void)
Sets the size of a vector to zero.
Definition: vector.h:656
bool display
Display messages to screen.
std::string write_training_rate_method(void) const
Returns a string with the name of the training rate method to be used.
tinyxml2::XMLDocument * to_XML(void) const
Vector< double > U
Interior point of the triplet.
Vector< double > calculate_fixed_directional_point(const double &, const Vector< double > &, const double &) const
Vector< double > calculate_Brent_method_directional_point(const double &, const Vector< double > &, const double &) const
bool operator==(const Triplet &other_triplet) const
Vector< double > calculate_directional_point(const double &, const Vector< double > &, const double &) const
double calculate_golden_section_training_rate(const Triplet &) const
TrainingRateMethod
Available training operators for obtaining the perform_training rate.
void set_warning_training_rate(const double &)
void set_training_rate_tolerance(const double &)
double bracketing_factor
Increase factor when bracketing a minimum.
virtual ~TrainingRateAlgorithm(void)
Destructor.
virtual void set_default(void)
Sets the members of the training rate algorithm to their default values.
void from_XML(const tinyxml2::XMLDocument &)
const double & get_warning_training_rate(void) const
double warning_training_rate
Big training rate value at which the algorithm displays a warning.
const bool & get_display(void) const
Triplet calculate_bracketing_triplet(const double &, const Vector< double > &, const double &) const
const double & get_training_rate_tolerance(void) const
Returns the tolerance value in line minimization.
const double & get_bracketing_factor(void) const
Returns the increase factor when bracketing a minimum in line minimization.
double training_rate_tolerance
Maximum interval length for the training rate.
Vector< double > calculate_golden_section_directional_point(const double &, const Vector< double > &, const double &) const
Vector< double > B
Right point of the triplet.
PerformanceFunctional * performance_functional_pointer
Pointer to an external performance functional object.
std::string to_string(void) const
Writes a string with the values of A, U and B.
double error_training_rate
Big training rate value at which the algorithm throws an exception.
const TrainingRateMethod & get_training_rate_method(void) const
Returns the training rate method used for training.
void set_training_rate_method(const TrainingRateMethod &)