OpenNN  2.2
Open Neural Networks Library
training_rate_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 R A T E 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_rate_algorithm.h"
17 
18 namespace OpenNN
19 {
20 
21 // DEFAULT CONSTRUCTOR
22 
26 
28  : performance_functional_pointer(NULL)
29 {
30  set_default();
31 }
32 
33 
34 // GENERAL CONSTRUCTOR
35 
40 
42  : performance_functional_pointer(new_performance_functional_pointer)
43 {
44  set_default();
45 }
46 
47 
48 // XML CONSTRUCTOR
49 
54 
55 TrainingRateAlgorithm::TrainingRateAlgorithm(const tinyxml2::XMLDocument& document)
56  : performance_functional_pointer(NULL)
57 {
58  from_XML(document);
59 }
60 
61 
62 // DESTRUCTOR
63 
65 
67 {
68 }
69 
70 
71 // METHODS
72 
73 // PerformanceFunctional* get_performance_functional_pointer(void) const method
74 
78 
80 {
81  #ifndef NDEBUG
82 
84  {
85  std::ostringstream buffer;
86 
87  buffer << "OpenNN Exception: TrainingRateAlgorithm class.\n"
88  << "PerformanceFunctional* get_performance_functional_pointer(void) const method.\n"
89  << "Performance functional pointer is NULL.\n";
90 
91  throw std::logic_error(buffer.str());
92  }
93 
94  #endif
95 
97 }
98 
99 
100 // bool has_performance_functional(void) const method
101 
104 
106 {
108  {
109  return(true);
110  }
111  else
112  {
113  return(false);
114  }
115 }
116 
117 
118 // const TrainingRateMethod& get_training_rate_method(void) const method
119 
121 
123 {
124  return(training_rate_method);
125 }
126 
127 
128 // std::string write_training_rate_method(void) const method
129 
131 
133 {
134  switch(training_rate_method)
135  {
136  case Fixed:
137  {
138  return("Fixed");
139  }
140  break;
141 
142  case GoldenSection:
143  {
144  return("GoldenSection");
145  }
146  break;
147 
148  case BrentMethod:
149  {
150  return("BrentMethod");
151  }
152  break;
153 
154  default:
155  {
156  std::ostringstream buffer;
157 
158  buffer << "OpenNN Exception: TrainingRateAlgorithm class.\n"
159  << "std::string get_training_rate_method(void) const method.\n"
160  << "Unknown training rate method.\n";
161 
162  throw std::logic_error(buffer.str());
163  }
164  break;
165  }
166 }
167 
168 
169 // const double& get_bracketing_factor(void) const method
170 
172 
174 {
175  return(bracketing_factor);
176 }
177 
178 
179 // const double& get_training_rate_tolerance(void) const method
180 
182 
184 {
185  return(training_rate_tolerance);
186 }
187 
188 
189 // const double& get_warning_training_rate(void) const method
190 
193 
195 {
196  return(warning_training_rate);
197 }
198 
199 
200 // const double& get_error_training_rate(void) const method
201 
204 
206 {
207  return(error_training_rate);
208 }
209 
210 
211 // const bool& get_display(void) const method
212 
215 
216 const bool& TrainingRateAlgorithm::get_display(void) const
217 {
218  return(display);
219 }
220 
221 
222 // void set(void) method
223 
226 
228 {
230  set_default();
231 }
232 
233 
234 // void set(PerformanceFunctional*) method
235 
239 
240 void TrainingRateAlgorithm::set(PerformanceFunctional* new_performance_functional_pointer)
241 {
242  performance_functional_pointer = new_performance_functional_pointer;
243  set_default();
244 }
245 
246 
247 // void set_default(void) method
248 
250 
252 {
253  // TRAINING OPERATORS
254 
255  training_rate_method = BrentMethod;
256 
257  // TRAINING PARAMETERS
258 
259  bracketing_factor = 1.5;
260 
261  training_rate_tolerance = 1.0e-6;
262 
263  warning_training_rate = 1.0e3;
264 
265  error_training_rate = 1.0e6;
266 
267  // UTILITIES
268 
269  display = true;
270 }
271 
272 
273 // void set_performance_functional_pointer(PerformanceFunctional*) method
274 
277 
279 {
280  performance_functional_pointer = new_performance_functional_pointer;
281 }
282 
283 
284 // void set_training_rate_method(const TrainingRateMethod&) method
285 
288 
290 {
291  training_rate_method = new_training_rate_method;
292 }
293 
294 
295 // void set_training_rate_method(const std::string&) method
296 
299 
300 void TrainingRateAlgorithm::set_training_rate_method(const std::string& new_training_rate_method)
301 {
302  if(new_training_rate_method == "Fixed")
303  {
304  training_rate_method = Fixed;
305  }
306  else if(new_training_rate_method == "GoldenSection")
307  {
308  training_rate_method = GoldenSection;
309  }
310  else if(new_training_rate_method == "BrentMethod")
311  {
312  training_rate_method = BrentMethod;
313  }
314  else
315  {
316  std::ostringstream buffer;
317 
318  buffer << "OpenNN Exception: TrainingRateAlgorithm class.\n"
319  << "void set_method(const std::string&) method.\n"
320  << "Unknown training rate method: " << new_training_rate_method << ".\n";
321 
322  throw std::logic_error(buffer.str());
323  }
324 }
325 
326 
327 // void set_bracketing_factor(const double&) method
328 
331 
332 void TrainingRateAlgorithm::set_bracketing_factor(const double& new_bracketing_factor)
333 {
334  // Control sentence (if debug)
335 
336  #ifndef NDEBUG
337 
338  if(new_bracketing_factor < 0.0)
339  {
340  std::ostringstream buffer;
341 
342  buffer << "OpenNN Exception: TrainingRateAlgorithm class.\n"
343  << "void set_bracketing_factor(const double&) method.\n"
344  << "Bracketing factor must be equal or greater than 0.\n";
345 
346  throw std::logic_error(buffer.str());
347  }
348 
349  #endif
350 
351  bracketing_factor = new_bracketing_factor;
352 }
353 
354 
355 // void set_training_rate_tolerance(const double&) method
356 
359 
360 void TrainingRateAlgorithm::set_training_rate_tolerance(const double& new_training_rate_tolerance)
361 {
362  // Control sentence (if debug)
363 
364  #ifndef NDEBUG
365 
366  if(new_training_rate_tolerance < 0.0)
367  {
368  std::ostringstream buffer;
369 
370  buffer << "OpenNN Exception: TrainingRateAlgorithm class.\n"
371  << "void set_training_rate_tolerance(const double&) method.\n"
372  << "Tolerance must be equal or greater than 0.\n";
373 
374  throw std::logic_error(buffer.str());
375  }
376 
377  #endif
378 
379  // Set training rate tolerance
380 
381  training_rate_tolerance = new_training_rate_tolerance;
382 }
383 
384 
385 // void set_warning_training_rate(const double&) method
386 
390 
391 void TrainingRateAlgorithm::set_warning_training_rate(const double& new_warning_training_rate)
392 {
393  // Control sentence (if debug)
394 
395  #ifndef NDEBUG
396 
397  if(new_warning_training_rate < 0.0)
398  {
399  std::ostringstream buffer;
400 
401  buffer << "OpenNN Exception: TrainingRateAlgorithm class.\n"
402  << "void set_warning_training_rate(const double&) method.\n"
403  << "Warning training rate must be equal or greater than 0.\n";
404 
405  throw std::logic_error(buffer.str());
406  }
407 
408  #endif
409 
410  warning_training_rate = new_warning_training_rate;
411 }
412 
413 
414 // void set_error_training_rate(const double&) method
415 
419 
420 void TrainingRateAlgorithm::set_error_training_rate(const double& new_error_training_rate)
421 {
422  // Control sentence (if debug)
423 
424  #ifndef NDEBUG
425 
426  if(new_error_training_rate < 0.0)
427  {
428  std::ostringstream buffer;
429 
430  buffer << "OpenNN Exception: TrainingRateAlgorithm class.\n"
431  << "void set_error_training_rate(const double&) method.\n"
432  << "Error training rate must be equal or greater than 0.\n";
433 
434  throw std::logic_error(buffer.str());
435  }
436 
437  #endif
438 
439  // Set error training rate
440 
441  error_training_rate = new_error_training_rate;
442 }
443 
444 
445 // void set_display(const bool&) method
446 
451 
452 void TrainingRateAlgorithm::set_display(const bool& new_display)
453 {
454  display = new_display;
455 }
456 
457 
458 // Vector<double> calculate_directional_point(const double&, const Vector<double>&, const double&) const method
459 
466 
467 Vector<double> TrainingRateAlgorithm::calculate_directional_point(const double& performance, const Vector<double>& training_direction, const double& initial_training_rate) const
468 {
469  #ifndef NDEBUG
470 
472  {
473  std::ostringstream buffer;
474 
475  buffer << "OpenNN Error: TrainingRateAlgorithm class.\n"
476  << "Vector<double> calculate_directional_point(const double&, const Vector<double>&, const double&) const method.\n"
477  << "Pointer to performance functional is NULL.\n";
478 
479  throw std::logic_error(buffer.str());
480  }
481 
482  #endif
483 
484  #ifndef NDEBUG
485 
487 
488  if(neural_network_pointer == NULL)
489  {
490  std::ostringstream buffer;
491 
492  buffer << "OpenNN Error: TrainingRateAlgorithm class.\n"
493  << "Vector<double> calculate_directional_point(const double&, const Vector<double>&, const double&) const method.\n"
494  << "Pointer to neural network is NULL.\n";
495 
496  throw std::logic_error(buffer.str());
497  }
498 
499  #endif
500 
501  switch(training_rate_method)
502  {
503  case TrainingRateAlgorithm::Fixed:
504  {
505  return(calculate_fixed_directional_point(performance, training_direction, initial_training_rate));
506  }
507  break;
508 
509  case TrainingRateAlgorithm::GoldenSection:
510  {
511  return(calculate_golden_section_directional_point(performance, training_direction, initial_training_rate));
512  }
513  break;
514 
515  case TrainingRateAlgorithm::BrentMethod:
516  {
517  return(calculate_Brent_method_directional_point(performance, training_direction, initial_training_rate));
518  }
519  break;
520 
521  default:
522  {
523  std::ostringstream buffer;
524 
525  buffer << "OpenNN Exception: TrainingRateAlgorithm class\n"
526  << "Vector<double> calculate_directional_point(const double&, const Vector<double>&, const double&) const method.\n"
527  << "Unknown training rate method.\n";
528 
529  throw std::logic_error(buffer.str());
530  }
531  }
532 }
533 
534 
535 // Triplet calculate_bracketing_triplet(const double&, const Vector<double>&, const double&) const method
536 
542 
544  const double& performance,
545  const Vector<double>& training_direction,
546  const double& initial_training_rate) const
547 {
548  Triplet triplet;
549 
550  if(training_direction == 0.0)
551  {
552  triplet.A[0] = 0.0;
553  triplet.A[1] = performance;
554 
555  triplet.U = triplet.A;
556 
557  triplet.B = triplet.A;
558 
559  return(triplet);
560  }
561 
562  if(initial_training_rate == 0.0)
563  {
564  triplet.A[0] = 0.0;
565  triplet.A[1] = performance;
566 
567  triplet.U = triplet.A;
568 
569  triplet.B = triplet.A;
570 
571  return(triplet);
572  }
573 
574  // Left point
575 
576  triplet.A[0] = 0.0;
577  triplet.A[1] = performance;
578 
579  // Right point
580 
581  triplet.B[0] = initial_training_rate;
582  triplet.B[1] = performance_functional_pointer->calculate_performance(training_direction, triplet.B[0]);
583 
584  while(triplet.A[1] > triplet.B[1])
585  {
586  triplet.A = triplet.B;
587 
588  triplet.B[0] *= bracketing_factor;
589  triplet.B[1] = performance_functional_pointer->calculate_performance(training_direction, triplet.B[0]);
590 
591  if(triplet.B[0] > error_training_rate)
592  {
593  std::ostringstream buffer;
594 
595  buffer << "OpenNN Warning: TrainingRateAlgorithm class.\n"
596  << "Vector<double> calculate_bracketing_triplet(double, const Vector<double>&, double) const method\n."
597  << "Right point is " << triplet.B[0] << "." << std::endl;
598 
599  buffer << "Performance: " << performance << "\n"
600  << "Training direction: " << training_direction << "\n"
601  << "Initial training rate: " << initial_training_rate << std::endl;
602 
603  throw std::logic_error(buffer.str());
604 
605  return(triplet);
606  }
607  }
608 
609  // Interior point
610 
611  triplet.U[0] = triplet.A[0] + (triplet.B[0] - triplet.A[0])/2.0;
612  triplet.U[1] = performance_functional_pointer->calculate_performance(training_direction, triplet.U[0]);
613 
614  while(triplet.A[1] < triplet.U[1])
615  {
616  triplet.U[0] = triplet.A[0] + (triplet.U[0]-triplet.A[0])/bracketing_factor;
617  triplet.U[1] = performance_functional_pointer->calculate_performance(training_direction, triplet.U[0]);
618 
619  if(triplet.U[0] - triplet.A[0] <= training_rate_tolerance)
620  {
621  triplet.U = triplet.A;
622  triplet.B = triplet.A;
623 
624  triplet.check();
625 
626  return(triplet);
627  }
628  }
629 
630  triplet.check();
631 
632  return(triplet);
633 }
634 
635 
636 // Vector<double> calculate_fixed_directional_point(const double&, const Vector<double>&, const double&) const method
637 
643 Vector<double> TrainingRateAlgorithm::calculate_fixed_directional_point(const double&, const Vector<double>& training_direction, const double& initial_training_rate) const
644 {
645  Vector<double> directional_point(2);
646 
647  directional_point[0] = initial_training_rate;
648  directional_point[1] = performance_functional_pointer->calculate_performance(training_direction, initial_training_rate);
649 
650  return(directional_point);
651 }
652 
653 
654 // Vector<double> calculate_golden_section_directional_point(double, Vector<double>, double) const method
655 
661 
663 (const double& performance, const Vector<double>& training_direction, const double& initial_training_rate) const
664 {
665  std::ostringstream buffer;
666 
667  // Bracket minimum
668 
669  try
670  {
671  Triplet triplet = calculate_bracketing_triplet(performance, training_direction, initial_training_rate);
672 
673  if(triplet.has_length_zero())
674  {
675  return(triplet.A);
676  }
677 
678  Vector<double> V(2);
679 
680  // Reduce the interval
681 
682  do
683  {
684  V[0] = calculate_golden_section_training_rate(triplet);
685  V[1] = performance_functional_pointer->calculate_performance(training_direction, V[0]);
686 
687  // Update points
688 
689  if(V[0] < triplet.U[0] && V[1] >= triplet.U[1])
690  {
691  triplet.A = V;
692  //U = U;
693  //B = B;
694  }
695  else if(V[0] < triplet.U[0] && V[1] <= triplet.U[1])
696  {
697  //A = A;
698  triplet.U = V;
699  triplet.B = triplet.U;
700  }
701  else if(V[0] > triplet.U[0] && V[1] >= triplet.U[1])
702  {
703  //A = A;
704  triplet.B = V;
705  //U = U;
706  }
707  else if(V[0] > triplet.U[0] && V[1] <= triplet.U[1])
708  {
709  triplet.A = triplet.U;
710  triplet.U = V;
711  //B = B;
712  }
713  else if(V[0] == triplet.U[0])
714  {
715  buffer << "OpenNN Exception: TrainingRateAlgorithm class.\n"
716  << "Vector<double> calculate_golden_section_directional_point(double, const Vector<double>, double) const method.\n"
717  << "Both interior points have the same ordinate.\n";
718 
719  std::cout << buffer.str() << std::endl;
720 
721  break;
722  }
723  else
724  {
725  buffer << "OpenNN Exception: TrainingRateAlgorithm class.\n"
726  << "Vector<double> calculate_golden_section_directional_point(double, const Vector<double>, double) const method.\n"
727  << "Unknown set:\n"
728  << "A = (" << triplet.A[0] << "," << triplet.A[1] << ")\n"
729  << "B = (" << triplet.B[0] << "," << triplet.B[1] << ")\n"
730  << "U = (" << triplet.U[0] << "," << triplet.U[1] << ")\n"
731  << "V = (" << V[0] << "," << V[1] << ")\n";
732 
733  throw std::logic_error(buffer.str());
734  }
735 
736  // Check triplet
737 
738  triplet.check();
739 
740  }while(triplet.B[0] - triplet.A[0] > training_rate_tolerance);
741 
742  return(triplet.U);
743  }
744  catch(const std::logic_error& e)
745  {
746  std::cerr << e.what() << std::endl;
747 
748  Vector<double> X(2);
749  X[0] = initial_training_rate;
750  X[1] = performance_functional_pointer->calculate_performance(training_direction, X[0]);
751 
752  if(X[1] > performance)
753  {
754  X[0] = 0.0;
755  X[1] = 0.0;
756  }
757 
758  return(X);
759  }
760 }
761 
762 
763 // Vector<double> calculate_Brent_method_directional_point(const double&, const Vector<double>, const double&) const method
764 
770 
772 (const double& performance, const Vector<double>& training_direction, const double& initial_training_rate) const
773 {
774  std::ostringstream buffer;
775 
776  // Bracket minimum
777 
778  try
779  {
780  Triplet triplet = calculate_bracketing_triplet(performance, training_direction, initial_training_rate);
781 
782  if(triplet.A == triplet.B)
783  {
784  return(triplet.A);
785  }
786 
787  Vector<double> V(2);
788 
789  // Reduce the interval
790 
791  while(triplet.B[0] - triplet.A[0] > training_rate_tolerance)
792  {
793  try
794  {
795  V[0] = calculate_Brent_method_training_rate(triplet);
796  }
797  catch(const std::logic_error&)
798  {
799  V[0] = calculate_golden_section_training_rate(triplet);
800  }
801 
802  // Calculate performance for V
803 
804  V[1] = performance_functional_pointer->calculate_performance(training_direction, V[0]);
805 
806  // Update points
807 
808  if(V[0] < triplet.U[0] && V[1] >= triplet.U[1])
809  {
810  triplet.A = V;
811  //B = B;
812  //U = U;
813  }
814  else if(V[0] < triplet.U[0] && V[1] <= triplet.U[1])
815  {
816  //A = A;
817  triplet.B = triplet.U;
818  triplet.U = V;
819  }
820  else if(V[0] > triplet.U[0] && V[1] >= triplet.U[1])
821  {
822  //A = A;
823  triplet.B = V;
824  //U = U;
825  }
826  else if(V[0] > triplet.U[0] && V[1] <= triplet.U[1])
827  {
828  triplet.A = triplet.U;
829  //B = B;
830  triplet.U = V;
831  }
832  else if(V[0] == triplet.U[0])
833  {
834  buffer << "OpenNN Exception: TrainingRateAlgorithm class.\n"
835  << "Vector<double> calculate_Brent_method_directional_point(double, const Vector<double>, double) const method.\n"
836  << "Both interior points have the same ordinate.\n";
837 
838  break;
839  }
840  else
841  {
842  buffer << "OpenNN Exception: TrainingRateAlgorithm class.\n"
843  << "Vector<double> calculate_Brent_method_directional_point(double, const Vector<double>, double) const method.\n"
844  << "Unknown set:\n"
845  << "A = (" << triplet.A[0] << "," << triplet.A[1] << ")\n"
846  << "B = (" << triplet.B[0] << "," << triplet.B[1] << ")\n"
847  << "U = (" << triplet.U[0] << "," << triplet.U[1] << ")\n"
848  << "V = (" << V[0] << "," << V[1] << ")\n";
849 
850  throw std::logic_error(buffer.str());
851  }
852 
853  // Check triplet
854 
855  triplet.check();
856  }
857 
858  return(triplet.U);
859  }
860  catch(std::range_error& e) // Interval is of length 0
861  {
862  std::cerr << e.what() << std::endl;
863 
864  Vector<double> A(2);
865  A[0] = 0.0;
866  A[1] = performance;
867 
868  return(A);
869  }
870  catch(const std::logic_error& e)
871  {
872  std::cerr << e.what() << std::endl;
873 
874  Vector<double> X(2);
875  X[0] = initial_training_rate;
876  X[1] = performance_functional_pointer->calculate_performance(training_direction, X[0]);
877 
878  if(X[1] > performance)
879  {
880  X[0] = 0.0;
881  X[1] = 0.0;
882  }
883 
884  return(X);
885  }
886 }
887 
888 
889 // double calculate_golden_section_training_rate(const Triplet&) const method
890 
893 
895 {
896 // const double tau = 0.382; // (3.0-sqrt(5.0))/2.0
897 
898  double training_rate;
899 
900  if(triplet.U[0] < triplet.A[0] + 0.5*(triplet.B[0] - triplet.A[0]))
901  {
902  training_rate = triplet.A[0] + 0.618*(triplet.B[0] - triplet.A[0]);
903  }
904  else
905  {
906  training_rate = triplet.A[0] + 0.382*(triplet.B[0] - triplet.A[0]);
907  }
908 
909  #ifndef NDEBUG
910 
911  if(training_rate < triplet.A[0])
912  {
913  std::ostringstream buffer;
914 
915  buffer << "OpenNN Error: TrainingRateAlgorithm class.\n"
916  << "double calculate_golden_section_training_rate(const Triplet&) const method.\n"
917  << "Training rate (" << training_rate << ") is less than triplet left point (" << triplet.A[0] << ").\n";
918 
919  throw std::logic_error(buffer.str());
920  }
921 
922  if(training_rate > triplet.B[0])
923  {
924  std::ostringstream buffer;
925 
926  buffer << "OpenNN Error: TrainingRateAlgorithm class.\n"
927  << "double calculate_golden_section_training_rate(const Triplet&) const method.\n"
928  << "Training rate (" << training_rate << ") is greater than triplet right point (" << triplet.B[0] << ").\n";
929 
930  throw std::logic_error(buffer.str());
931  }
932 
933  #endif
934 
935  return(training_rate);
936 }
937 
938 
939 // double calculate_Brent_method_training_rate(const Triplet&) const method
940 
943 
945 {
946  const double c = -(triplet.A[1]*(triplet.U[0]-triplet.B[0])
947  + triplet.U[1]*(triplet.B[0]-triplet.A[0])
948  + triplet.B[1]*(triplet.A[0]-triplet.U[0]))/((triplet.A[0]-triplet.U[0])*(triplet.U[0]-triplet.B[0])*(triplet.B[0]-triplet.A[0]));
949 
950  if(c == 0)
951  {
952  std::ostringstream buffer;
953 
954  buffer << "OpenNN Exception: TrainingRateAlgorithm class.\n"
955  << "double calculate_Brent_method_training_rate(Vector<double>&, Vector<double>&, Vector<double>&) const method.\n"
956  << "Parabola cannot be constructed.\n";
957 
958  throw std::logic_error(buffer.str());
959  }
960  else if(c < 0)
961  {
962  std::ostringstream buffer;
963 
964  buffer << "OpenNN Exception: TrainingRateAlgorithm class.\n"
965  << "double calculate_Brent_method_training_rate(Vector<double>&, Vector<double>&, Vector<double>&) const method.\n"
966  << "Parabola does not have a minimum but a maximum.\n";
967 
968  throw std::logic_error(buffer.str());
969  }
970 
971  const double b = (triplet.A[1]*(triplet.U[0]*triplet.U[0]-triplet.B[0]*triplet.B[0])
972  + triplet.U[1]*(triplet.B[0]*triplet.B[0]-triplet.A[0]*triplet.A[0])
973  + triplet.B[1]*(triplet.A[0]*triplet.A[0]-triplet.U[0]*triplet.U[0]))/((triplet.A[0]-triplet.U[0])*(triplet.U[0]-triplet.B[0])*(triplet.B[0]-triplet.A[0]));
974 
975  const double Brent_method_training_rate = -b/(2.0*c);
976 
977  if(Brent_method_training_rate <= triplet.A[0] || Brent_method_training_rate >= triplet.B[0])
978  {
979  std::ostringstream buffer;
980 
981  buffer << "OpenNN Exception: TrainingRateAlgorithm class.\n"
982  << "double calculate_parabola_minimal_training_rate(Vector<double>&, Vector<double>&, Vector<double>&) const method.\n"
983  << "Brent method training rate is not inside interval.\n"
984  << "Interval: (" << triplet.A[0] << "," << triplet.B[0] << ")\n"
985  << "Brent method training rate: " << Brent_method_training_rate << std::endl;
986 
987  throw std::logic_error(buffer.str());
988  }
989 
990  return(Brent_method_training_rate);
991 }
992 
993 
994 // tinyxml2::XMLDocument* to_XML(void) const method
995 
998 
999 tinyxml2::XMLDocument* TrainingRateAlgorithm::to_XML(void) const
1000 {
1001  std::ostringstream buffer;
1002 
1003  tinyxml2::XMLDocument* document = new tinyxml2::XMLDocument;
1004 
1005  // Training algorithm
1006 
1007  tinyxml2::XMLElement* root_element = document->NewElement("TrainingRateAlgorithm");
1008 
1009  document->InsertFirstChild(root_element);
1010 
1011  tinyxml2::XMLElement* element = NULL;
1012  tinyxml2::XMLText* text = NULL;
1013 
1014  // Training rate method
1015  {
1016  element = document->NewElement("TrainingRateMethod");
1017  root_element->LinkEndChild(element);
1018 
1019  text = document->NewText(write_training_rate_method().c_str());
1020  element->LinkEndChild(text);
1021  }
1022 
1023  // Bracketing factor
1024  {
1025  element = document->NewElement("BracketingFactor");
1026  root_element->LinkEndChild(element);
1027 
1028  buffer.str("");
1029  buffer << bracketing_factor;
1030 
1031  text = document->NewText(buffer.str().c_str());
1032  element->LinkEndChild(text);
1033  }
1034 
1035  // Training rate tolerance
1036  {
1037  element = document->NewElement("TrainingRateTolerance");
1038  root_element->LinkEndChild(element);
1039 
1040  buffer.str("");
1041  buffer << training_rate_tolerance;
1042 
1043  text = document->NewText(buffer.str().c_str());
1044  element->LinkEndChild(text);
1045  }
1046 
1047  // Warning training rate
1048  {
1049  element = document->NewElement("WarningTrainingRate");
1050  root_element->LinkEndChild(element);
1051 
1052  buffer.str("");
1053  buffer << warning_training_rate;
1054 
1055  text = document->NewText(buffer.str().c_str());
1056  element->LinkEndChild(text);
1057  }
1058 
1059  // Error training rate
1060  {
1061  element = document->NewElement("ErrorTrainingRate");
1062  root_element->LinkEndChild(element);
1063 
1064  buffer.str("");
1065  buffer << error_training_rate;
1066 
1067  text = document->NewText(buffer.str().c_str());
1068  element->LinkEndChild(text);
1069  }
1070 
1071  // Display warnings
1072  {
1073  element = document->NewElement("Display");
1074  root_element->LinkEndChild(element);
1075 
1076  buffer.str("");
1077  buffer << display;
1078 
1079  text = document->NewText(buffer.str().c_str());
1080  element->LinkEndChild(text);
1081  }
1082 
1083  return(document);
1084 }
1085 
1086 
1087 // virtual void from_XML(const tinyxml2::XMLDocument&) method
1088 
1092 
1093 void TrainingRateAlgorithm::from_XML(const tinyxml2::XMLDocument& document)
1094 {
1095  const tinyxml2::XMLElement* root_element = document.FirstChildElement("TrainingRateAlgorithm");
1096 
1097  if(!root_element)
1098  {
1099  std::ostringstream buffer;
1100 
1101  buffer << "OpenNN Exception: TrainingRateAlgorithm class.\n"
1102  << "void from_XML(const tinyxml2::XMLDocument&) method.\n"
1103  << "Training rate algorithm element is NULL.\n";
1104 
1105  throw std::logic_error(buffer.str());
1106  }
1107 
1108  // Training rate method
1109  {
1110  const tinyxml2::XMLElement* element = root_element->FirstChildElement("TrainingRateMethod");
1111 
1112  if(element)
1113  {
1114  std::string new_training_rate_method = element->GetText();
1115 
1116  try
1117  {
1118  set_training_rate_method(new_training_rate_method);
1119  }
1120  catch(const std::logic_error& e)
1121  {
1122  std::cout << e.what() << std::endl;
1123  }
1124  }
1125  }
1126 
1127  // Bracketing factor
1128  {
1129  const tinyxml2::XMLElement* element = root_element->FirstChildElement("BracketingFactor");
1130 
1131  if(element)
1132  {
1133  const double new_bracketing_factor = atof(element->GetText());
1134 
1135  try
1136  {
1137  set_bracketing_factor(new_bracketing_factor);
1138  }
1139  catch(const std::logic_error& e)
1140  {
1141  std::cout << e.what() << std::endl;
1142  }
1143  }
1144  }
1145 /*
1146  // First training rate
1147  {
1148  const tinyxml2::XMLElement* element = root_element->FirstChildElement("FirstTrainingRate");
1149 
1150  if(element)
1151  {
1152  const double new_first_training_rate = atof(element->GetText());
1153 
1154  try
1155  {
1156  set_first_training_rate(new_first_training_rate);
1157  }
1158  catch(const std::logic_error& e)
1159  {
1160  std::cout << e.what() << std::endl;
1161  }
1162  }
1163  }
1164 */
1165  // Training rate tolerance
1166  {
1167  const tinyxml2::XMLElement* element = root_element->FirstChildElement("TrainingRateTolerance");
1168 
1169  if(element)
1170  {
1171  const double new_training_rate_tolerance = atof(element->GetText());
1172 
1173  try
1174  {
1175  set_training_rate_tolerance(new_training_rate_tolerance);
1176  }
1177  catch(const std::logic_error& e)
1178  {
1179  std::cout << e.what() << std::endl;
1180  }
1181  }
1182  }
1183 
1184  // Warning training rate
1185  {
1186  const tinyxml2::XMLElement* element = root_element->FirstChildElement("WarningTrainingRate");
1187 
1188  if(element)
1189  {
1190  const double new_warning_training_rate = atof(element->GetText());
1191 
1192  try
1193  {
1194  set_warning_training_rate(new_warning_training_rate);
1195  }
1196  catch(const std::logic_error& e)
1197  {
1198  std::cout << e.what() << std::endl;
1199  }
1200  }
1201  }
1202 
1203  // Error training rate
1204  {
1205  const tinyxml2::XMLElement* element = root_element->FirstChildElement("ErrorTrainingRate");
1206 
1207  if(element)
1208  {
1209  const double new_error_training_rate = atof(element->GetText());
1210 
1211  try
1212  {
1213  set_error_training_rate(new_error_training_rate);
1214  }
1215  catch(const std::logic_error& e)
1216  {
1217  std::cout << e.what() << std::endl;
1218  }
1219  }
1220  }
1221 
1222  // Display warnings
1223  {
1224  const tinyxml2::XMLElement* element = root_element->FirstChildElement("Display");
1225 
1226  if(element)
1227  {
1228  const std::string new_display = element->GetText();
1229 
1230  try
1231  {
1232  set_display(new_display != "0");
1233  }
1234  catch(const std::logic_error& e)
1235  {
1236  std::cout << e.what() << std::endl;
1237  }
1238  }
1239  }
1240 }
1241 
1242 }
1243 
1244 
1245 // OpenNN: Open Neural Networks Library.
1246 // Copyright (c) 2005-2015 Roberto Lopez.
1247 //
1248 // This library is free software; you can redistribute it and/or
1249 // modify it under the terms of the GNU Lesser General Public
1250 // License as published by the Free Software Foundation; either
1251 // version 2.1 of the License, or any later version.
1252 //
1253 // This library is distributed in the hope that it will be useful,
1254 // but WITHOUT ANY WARRANTY; without even the implied warranty of
1255 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1256 // Lesser General Public License for more details.
1257 
1258 // You should have received a copy of the GNU Lesser General Public
1259 // License along with this library; if not, write to the Free Software
1260 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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.
const double & get_error_training_rate(void) const
void set_performance_functional_pointer(PerformanceFunctional *)
NeuralNetwork * get_neural_network_pointer(void) const
Returns a pointer to the neural network associated to the performance functional. ...
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
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.
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 &)