OpenNN  2.2
Open Neural Networks Library
training_algorithm.cpp
1 /****************************************************************************************************************/
2 /* */
3 /* OpenNN: Open Neural Networks Library */
4 /* www.artelnics.com/opennn */
5 /* */
6 /* T R A I N I N G A L G O R I T H M C L A S S */
7 /* */
8 /* Roberto Lopez */
9 /* Artelnics - Making intelligent use of data */
11 /* */
12 /****************************************************************************************************************/
13 
14 // OpenNN includes
15 
16 #include "training_algorithm.h"
17 
18 namespace OpenNN
19 {
20 
21 
22 // DEFAULT CONSTRUCTOR
23 
26 
28  : performance_functional_pointer(NULL)
29 {
30  set_default();
31 }
32 
33 
34 // GENERAL CONSTRUCTOR
35 
39 
40 TrainingAlgorithm::TrainingAlgorithm(PerformanceFunctional* new_performance_functional_pointer)
41  : performance_functional_pointer(new_performance_functional_pointer)
42 {
43  set_default();
44 }
45 
46 
47 // XML CONSTRUCTOR
48 
52 
53 TrainingAlgorithm::TrainingAlgorithm(const tinyxml2::XMLDocument& document)
54  : performance_functional_pointer(NULL)
55 {
56  from_XML(document);
57 }
58 
59 
60 // DESTRUCTOR
61 
63 
65 {
66 }
67 
68 
69 // ASSIGNMENT OPERATOR
70 
74 
76 {
77  if(this != &other_training_algorithm)
78  {
80 
81  display = other_training_algorithm.display;
82  }
83 
84  return(*this);
85 }
86 
87 
88 // EQUAL TO OPERATOR
89 
92 
93 bool TrainingAlgorithm::operator == (const TrainingAlgorithm& other_training_algorithm) const
94 {
96  && display == other_training_algorithm.display)
97  {
98  return(true);
99  }
100  else
101  {
102  return(false);
103  }
104 }
105 
106 
107 // METHODS
108 
109 // PerformanceFunctional* get_performance_functional_pointer(void) const method
110 
113 
115 {
116  #ifndef NDEBUG
117 
119  {
120  std::ostringstream buffer;
121 
122  buffer << "OpenNN Exception: TrainingAlgorithm class.\n"
123  << "PerformanceFunctional* get_performance_functional_pointer(void) const method.\n"
124  << "Performance functional pointer is NULL.\n";
125 
126  throw std::logic_error(buffer.str());
127  }
128 
129  #endif
130 
132 }
133 
134 
135 // bool has_performance_functional(void) const method
136 
139 
141 {
143  {
144  return(true);
145  }
146  else
147  {
148  return(false);
149  }
150 }
151 
152 
153 // const bool& get_display(void) const method
154 
157 
158 const bool& TrainingAlgorithm::get_display(void) const
159 {
160  return(display);
161 }
162 
163 
164 // const size_t& get_display_period(void) const method
165 
167 
168 const size_t& TrainingAlgorithm::get_display_period(void) const
169 {
170  return(display_period);
171 }
172 
173 
174 // const size_t& get_save_period(void) const method
175 
177 
178 const size_t& TrainingAlgorithm::get_save_period(void) const
179 {
180  return(save_period);
181 }
182 
183 
184 // const size_t& get_neural_network_filename(void) const method
185 
187 
189 {
190  return(neural_network_file_name);
191 }
192 
193 
194 // void set(void) method
195 
198 
200 {
202 
203  set_default();
204 }
205 
206 
207 // void set(PerformanceFunctional*) method
208 
212 
213 void TrainingAlgorithm::set(PerformanceFunctional* new_performance_functional_pointer)
214 {
215  performance_functional_pointer = new_performance_functional_pointer;
216 
217  set_default();
218 }
219 
220 
221 // void set_performance_functional_pointer(PerformanceFunctional*) method
222 
225 
227 {
228  performance_functional_pointer = new_performance_functional_pointer;
229 }
230 
231 
232 // void set_display(const bool&) method
233 
238 
239 void TrainingAlgorithm::set_display(const bool& new_display)
240 {
241  display = new_display;
242 }
243 
244 
245 // void set_display_period(size_t) method
246 
250 
251 void TrainingAlgorithm::set_display_period(const size_t& new_display_period)
252 {
253  // Control sentence (if debug)
254 
255  #ifndef NDEBUG
256 
257  if(new_display_period <= 0)
258  {
259  std::ostringstream buffer;
260 
261  buffer << "OpenNN Exception: ConjugateGradient class.\n"
262  << "void set_display_period(const size_t&) method.\n"
263  << "Display period must be greater than 0.\n";
264 
265  throw std::logic_error(buffer.str());
266  }
267 
268  #endif
269 
270  display_period = new_display_period;
271 }
272 
273 
274 // void set_save_period(const size_t&) method
275 
279 
280 void TrainingAlgorithm::set_save_period(const size_t& new_save_period)
281 {
282  // Control sentence (if debug)
283 
284  #ifndef NDEBUG
285 
286  if(new_save_period <= 0)
287  {
288  std::ostringstream buffer;
289 
290  buffer << "OpenNN Exception: ConjugateGradient class.\n"
291  << "void set_save_period(const size_t&) method.\n"
292  << "Save period must be greater than 0.\n";
293 
294  throw std::logic_error(buffer.str());
295  }
296 
297  #endif
298 
299  save_period = new_save_period;
300 }
301 
302 
303 // void set_neural_network_file_name(const std::string&) method
304 
308 
309 void TrainingAlgorithm::set_neural_network_file_name(const std::string& new_neural_network_file_name)
310 {
311  neural_network_file_name = new_neural_network_file_name;
312 }
313 
314 
315 // void set_default(void) method
316 
318 
320 {
321  display = true;
322 
323  display_period = 10;
324 
325  save_period = UINT_MAX;
326 
327  neural_network_file_name = "neural_network.xml";
328 }
329 
330 
331 // std::string write_training_algorithm_type(void) const method
332 
334 
336 {
337  return("USER_TRAINING_ALGORITHM");
338 }
339 
340 
341 // void check(void) const method
342 
347 
348 void TrainingAlgorithm::check(void) const
349 {
350  std::ostringstream buffer;
351 
353  {
354  buffer << "OpenNN Exception: TrainingAlgorithm class.\n"
355  << "void check(void) const method.\n"
356  << "Pointer to performance functional is NULL.\n";
357 
358  throw std::logic_error(buffer.str());
359  }
360 
362 
363  if(neural_network_pointer == NULL)
364  {
365  buffer << "OpenNN Exception: TrainingAlgorithm class.\n"
366  << "void check(void) const method.\n"
367  << "Pointer to neural network is NULL.\n";
368 
369  throw std::logic_error(buffer.str());
370  }
371 }
372 
373 
374 // tinyxml2::XMLDocument* to_XML(void) const method
375 
378 
379 tinyxml2::XMLDocument* TrainingAlgorithm::to_XML(void) const
380 {
381  std::ostringstream buffer;
382 
383  tinyxml2::XMLDocument* document = new tinyxml2::XMLDocument;
384 
385  // Nueral network outputs integrals
386 
387  tinyxml2::XMLElement* training_algorithm_element = document->NewElement("TrainingAlgorithm");
388 
389  document->InsertFirstChild(training_algorithm_element);
390 
391  // Display
392  {
393  tinyxml2::XMLElement* element = document->NewElement("Display");
394  training_algorithm_element->LinkEndChild(element);
395 
396  buffer.str("");
397  buffer << display;
398 
399  tinyxml2::XMLText* text = document->NewText(buffer.str().c_str());
400  element->LinkEndChild(text);
401  }
402 
403  return(document);
404 }
405 
406 
407 // void from_XML(const tinyxml2::XMLDocument&) method
408 
411 
412 void TrainingAlgorithm::from_XML(const tinyxml2::XMLDocument& document)
413 {
414  const tinyxml2::XMLElement* root_element = document.FirstChildElement("TrainingAlgorithm");
415 
416  if(!root_element)
417  {
418  std::ostringstream buffer;
419 
420  buffer << "OpenNN Exception: TrainingAlgorithm class.\n"
421  << "void from_XML(const tinyxml2::XMLDocument&) method.\n"
422  << "Training algorithm element is NULL.\n";
423 
424  throw std::logic_error(buffer.str());
425  }
426 
427  // Display
428  {
429  const tinyxml2::XMLElement* display_element = root_element->FirstChildElement("Display");
430 
431  if(display_element)
432  {
433  const std::string new_display_string = display_element->GetText();
434 
435  try
436  {
437  set_display(new_display_string != "0");
438  }
439  catch(const std::logic_error& e)
440  {
441  std::cout << e.what() << std::endl;
442  }
443  }
444  }
445 
446 }
447 
448 
449 // std::string to_string(void) const method
450 
452 
453 std::string TrainingAlgorithm::to_string(void) const
454 {
455  std::ostringstream buffer;
456 
457  buffer << "Training strategy\n"
458  << "Display: " << display << "\n";
459 
460  return(buffer.str());
461 }
462 
463 
464 // Matrix<std::string> to_string_matrix(void) const method
465 
468 
470 {
471  Matrix<std::string> string_matrix;
472 
473  return(string_matrix);
474 }
475 
476 
477 // void print(void) const method
478 
480 
481 void TrainingAlgorithm::print(void) const
482 {
483  std::cout << to_string();
484 }
485 
486 
487 // void save(const std::string&) const method
488 
491 
492 void TrainingAlgorithm::save(const std::string& file_name) const
493 {
494  tinyxml2::XMLDocument* document = to_XML();
495 
496  document->SaveFile(file_name.c_str());
497 
498  delete document;
499 }
500 
501 
502 // void load(const std::string&) method
503 
507 
508 void TrainingAlgorithm::load(const std::string& file_name)
509 {
510  set_default();
511 
512  tinyxml2::XMLDocument document;
513 
514  if (document.LoadFile(file_name.c_str()))
515  {
516  std::ostringstream buffer;
517 
518  buffer << "OpenNN Exception: TrainingAlgorithm class.\n"
519  << "void load(const std::string&) method.\n"
520  << "Cannot load XML file " << file_name << ".\n";
521 
522  throw std::logic_error(buffer.str());
523  }
524 
525  from_XML(document);
526 }
527 
528 
529 // void initialize_random(void) method
530 
533 
535 {
536  switch(rand()%2)
537  {
538  case 0:
539  {
540  display = false;
541  }
542  break;
543 
544  case 1:
545  {
546  display = true;
547  }
548  break;
549 
550  default:
551  {
552  std::ostringstream buffer;
553 
554  buffer << "OpenNN Exception: TrainingAlgorithm class.\n"
555  << "void initialize_random(void) method.\n"
556  << "Unknown display value.\n";
557 
558  throw std::logic_error(buffer.str());
559  }
560  break;
561  }
562 }
563 
564 
565 }
566 
567 
568 // OpenNN: Open Neural Networks Library.
569 // Copyright (c) 2005-2015 Roberto Lopez.
570 //
571 // This library is free software; you can redistribute it and/or
572 // modify it under the terms of the GNU Lesser General Public
573 // License as published by the Free Software Foundation; either
574 // version 2.1 of the License, or any later version.
575 //
576 // This library is distributed in the hope that it will be useful,
577 // but WITHOUT ANY WARRANTY; without even the implied warranty of
578 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
579 // Lesser General Public License for more details.
580 
581 // You should have received a copy of the GNU Lesser General Public
582 // License along with this library; if not, write to the Free Software
583 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
void set_display_period(const size_t &)
virtual void set_default(void)
Sets the members of the training algorithm object to their default values.
void save(const std::string &) const
const size_t & get_save_period(void) const
Returns the number of iterations between the training saving progress.
bool display
Display messages to screen.
NeuralNetwork * get_neural_network_pointer(void) const
Returns a pointer to the neural network associated to the performance functional. ...
PerformanceFunctional * get_performance_functional_pointer(void) const
bool has_performance_functional(void) const
void print(void) const
Prints to the screen the XML-type representation of the training algorithm object.
void set_save_period(const size_t &)
size_t save_period
Number of iterations between the training saving progress.
virtual tinyxml2::XMLDocument * to_XML(void) const
std::string neural_network_file_name
Path where the neural network is saved.
void set_neural_network_file_name(const std::string &)
virtual std::string write_training_algorithm_type(void) const
This method writes a string with the type of training algoritm.
virtual Matrix< std::string > to_string_matrix(void) const
virtual TrainingAlgorithm & operator=(const TrainingAlgorithm &)
virtual bool operator==(const TrainingAlgorithm &) const
virtual void check(void) const
virtual std::string to_string(void) const
Returns a default string representation of a training algorithm.
const bool & get_display(void) const
virtual void initialize_random(void)
const size_t & get_display_period(void) const
Returns the number of iterations between the training showing progress.
virtual void from_XML(const tinyxml2::XMLDocument &)
PerformanceFunctional * performance_functional_pointer
Pointer to a performance functional for a multilayer perceptron object.
virtual void set_performance_functional_pointer(PerformanceFunctional *)
virtual ~TrainingAlgorithm(void)
Destructor.
size_t display_period
Number of iterations between the training showing progress.
void load(const std::string &)
const std::string & get_neural_network_file_name(void) const
Returns the file name where the neural network will be saved.