OpenNN  2.2
Open Neural Networks Library
unscaling_layer.cpp
1 /****************************************************************************************************************/
2 /* */
3 /* OpenNN: Open Neural Networks Library */
4 /* www.artelnics.com/opennn */
5 /* */
6 /* U N S C A L I N G L A Y E R 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 "unscaling_layer.h"
17 
18 namespace OpenNN
19 {
20 
21 // DEFAULT CONSTRUCTOR
22 
24 
26 {
27  set();
28 }
29 
30 
32 
33 UnscalingLayer::UnscalingLayer(const size_t& new_unscaling_neurons_number)
34 {
35  set(new_unscaling_neurons_number);
36 }
37 
38 
40 
42 {
43  set(new_statistics);
44 }
45 
46 
48 
49 UnscalingLayer::UnscalingLayer(const tinyxml2::XMLDocument& unscaling_layer_document)
50 {
51  set(unscaling_layer_document);
52 }
53 
54 
56 
57 UnscalingLayer::UnscalingLayer(const UnscalingLayer& other_unscaling_layer)
58 {
59  set(other_unscaling_layer);
60 }
61 
62 
63 // DESTRUCTOR
64 
66 
68 {
69 }
70 
71 
72 // ASSIGNMENT OPERATOR
73 
74 // UnscalingLayer& operator = (const UnscalingLayer&) method
75 
78 
80 {
81  if(this != &other_unscaling_layer)
82  {
83  //statistics = other_unscaling_layer.statistics;
84 
85  unscaling_method = other_unscaling_layer.unscaling_method;
86 
87  display = other_unscaling_layer.display;
88  }
89 
90  return(*this);
91 }
92 
93 
94 // EQUAL TO OPERATOR
95 
96 // bool operator == (const UnscalingLayer&) const method
97 
101 
102 bool UnscalingLayer::operator == (const UnscalingLayer& other_unscaling_layer) const
103 {
104  if(
105  // statistics == other_unscaling_layer.statistics
106  unscaling_method == other_unscaling_layer.unscaling_method
107  && display == other_unscaling_layer.display)
108  {
109  return(true);
110  }
111  else
112  {
113  return(false);
114  }
115 }
116 
117 
118 // size_t get_unscaling_neurons_number(void) const method
119 
121 
123 {
124  return(statistics.size());
125 }
126 
127 
128 // Vector< Statistics<double> > get_statistics(void) method
129 
138 
140 {
141  return(statistics);
142 }
143 
144 
145 
146 // Matrix<double> arrange_statistics(void) const method
147 
151 
153 {
154  const size_t unscaling_neurons_number = get_unscaling_neurons_number();
155 
156  Matrix<double> statistics_matrix(unscaling_neurons_number, 4);
157 
158  for(size_t i = 0; i < unscaling_neurons_number; i++)
159  {
160  statistics_matrix.set_row(i, statistics[i].to_vector());
161  }
162 
163  return(statistics_matrix);
164 }
165 
166 
167 // Vector<double> arrange_minimums(void) const method
168 
171 
173 {
174  const size_t unscaling_neurons_number = get_unscaling_neurons_number();
175 
176  Vector<double> minimums(unscaling_neurons_number, 4);
177 
178  for(size_t i = 0; i < unscaling_neurons_number; i++)
179  {
180  minimums[i] = statistics[i].minimum;
181  }
182 
183  return(minimums);
184 }
185 
186 
187 // Vector<double> arrange_maximums(void) const method
188 
191 
193 {
194  const size_t unscaling_neurons_number = get_unscaling_neurons_number();
195 
196  Vector<double> maximums(unscaling_neurons_number, 4);
197 
198  for(size_t i = 0; i < unscaling_neurons_number; i++)
199  {
200  maximums[i] = statistics[i].maximum;
201  }
202 
203  return(maximums);
204 }
205 
206 
207 // const UnscalingMethod& get_unscaling_method(void) const method
208 
211 
213 {
214  return(unscaling_method);
215 }
216 
217 
218 // std::string write_unscaling_method(void) const method
219 
222 
224 {
225  if(unscaling_method == NoUnscaling)
226  {
227  return("NoUnscaling");
228  }
229  else if(unscaling_method == MinimumMaximum)
230  {
231  return("MinimumMaximum");
232  }
233  else if(unscaling_method == MeanStandardDeviation)
234  {
235  return("MeanStandardDeviation");
236  }
237  else
238  {
239  std::ostringstream buffer;
240 
241  buffer << "OpenNN Exception: UnscalingLayer class.\n"
242  << "std::string write_unscaling_method(void) const method.\n"
243  << "Unknown unscaling method.\n";
244 
245  throw std::logic_error(buffer.str());
246  }
247 }
248 
249 
250 // std::string write_unscaling_method_text(void) const method
251 
254 
256 {
257  if(unscaling_method == NoUnscaling)
258  {
259  return("no unscaling");
260  }
261  else if(unscaling_method == MeanStandardDeviation)
262  {
263  return("mean and standard deviation");
264  }
265  else if(unscaling_method == MinimumMaximum)
266  {
267  return("minimum and maximum");
268  }
269  else
270  {
271  std::ostringstream buffer;
272 
273  buffer << "OpenNN Exception: UnscalingLayer class.\n"
274  << "std::string write_unscaling_method_text(void) const method.\n"
275  << "Unknown unscaling method.\n";
276 
277  throw std::logic_error(buffer.str());
278  }
279 }
280 
281 
282 // const bool& get_display(void) const method
283 
286 
287 const bool& UnscalingLayer::get_display(void) const
288 {
289  return(display);
290 }
291 
292 
293 // void set(void) method
294 
296 
298 {
299  statistics.set();
300 
301  set_default();
302 }
303 
304 
305 // void set(const size_t&) method
306 
309 
310 void UnscalingLayer::set(const size_t& new_unscaling_neurons_number)
311 {
312  statistics.set(new_unscaling_neurons_number);
313 
314  set_default();
315 }
316 
317 
318 // void set(const Vector< Statistics<double> >&) method
319 
324 
325 void UnscalingLayer::set(const Vector< Statistics<double> >& new_statistics)
326 {
327  statistics = new_statistics;
328 
329  set_default();
330 }
331 
332 
333 // void set(const tinyxml2::XMLDocument&) method
334 
337 
338 void UnscalingLayer::set(const tinyxml2::XMLDocument& new_unscaling_layer_document)
339 {
340  set_default();
341 
342  from_XML(new_unscaling_layer_document);
343 }
344 
345 
346 // void set(const UnscalingLayer&) method
347 
349 
350 void UnscalingLayer::set(const UnscalingLayer& new_unscaling_layer)
351 {
352  statistics = new_unscaling_layer.statistics;
353 
354  unscaling_method = new_unscaling_layer.unscaling_method;
355 
356  display = new_unscaling_layer.display;
357 }
358 
359 
360 // void set_default(void) method
361 
371 
373 {
374  set_unscaling_method(MinimumMaximum);
375 
376  set_display(true);
377 }
378 
379 
380 // void set_statistics(const Vector< Statistics<double> >&) method
381 
385 
386 
388 {
389  // Control sentence (if debug)
390 
391  #ifndef NDEBUG
392 
393  const size_t unscaling_neurons_number = get_unscaling_neurons_number();
394 
395  const size_t new_statistics_size = new_statistics.size();
396 
397  if(new_statistics_size != unscaling_neurons_number)
398  {
399  std::ostringstream buffer;
400 
401  buffer << "OpenNN Exception: UnscalingLayer class.\n"
402  << "void set_statistics(const Vector< Statistics<double> >&) method.\n"
403  << "Size of statistics must be equal to number of unscaling neurons.\n";
404 
405  throw std::logic_error(buffer.str());
406  }
407 
408  #endif
409 
410  // Set all statistics
411 
412  statistics = new_statistics;
413 }
414 
415 
416 // void set_item_statistics(const size_t&, const Statistics<double>&) method
417 
421 
422 void UnscalingLayer::set_item_statistics(const size_t& i, const Statistics<double>& item_statistics)
423 {
424  statistics[i] = item_statistics;
425 }
426 
427 
428 // void set_minimum(const size_t&, const double&) method
429 
433 
434 void UnscalingLayer::set_minimum(const size_t& i, const double& new_minimum)
435 {
436  statistics[i].set_minimum(new_minimum);
437 }
438 
439 
440 // void set_maximum(const size_t&, const double&) method
441 
445 
446 void UnscalingLayer::set_maximum(const size_t& i, const double& new_maximum)
447 {
448  statistics[i].set_maximum(new_maximum);
449 }
450 
451 
452 // void set_mean(const size_t&, const double&) method
453 
457 
458 void UnscalingLayer::set_mean(const size_t& i, const double& new_mean)
459 {
460  statistics[i].set_mean(new_mean);
461 }
462 
463 
464 // void set_standard_deviation(const size_t&, const double&) method
465 
469 
470 void UnscalingLayer::set_standard_deviation(const size_t& i, const double& new_standard_deviation)
471 {
472  statistics[i].set_standard_deviation(new_standard_deviation);
473 }
474 
475 
476 // void set_unscaling_method(const UnscalingMethod&) method
477 
480 
482 {
483  unscaling_method = new_unscaling_method;
484 }
485 
486 
487 // void set_unscaling_method(const std::string&) method
488 
492 
493 void UnscalingLayer::set_unscaling_method(const std::string& new_unscaling_method)
494 {
495  if(new_unscaling_method == "NoUnscaling")
496  {
497  set_unscaling_method(NoUnscaling);
498  }
499  else if(new_unscaling_method == "MeanStandardDeviation")
500  {
501  set_unscaling_method(MeanStandardDeviation);
502  }
503  else if(new_unscaling_method == "MinimumMaximum")
504  {
505  set_unscaling_method(MinimumMaximum);
506  }
507  else
508  {
509  std::ostringstream buffer;
510 
511  buffer << "OpenNN Exception: UnscalingLayer class.\n"
512  << "void set_unscaling_method(const std::string&) method.\n"
513  << "Unknown unscaling method: " << new_unscaling_method << ".\n";
514 
515  throw std::logic_error(buffer.str());
516  }
517 }
518 
519 
520 // void set_display(const bool&) method
521 
526 
527 void UnscalingLayer::set_display(const bool& new_display)
528 {
529  display = new_display;
530 }
531 
532 
533 // void prune_unscaling_neuron(const size_t&) method
534 
537 
538 void UnscalingLayer::prune_unscaling_neuron(const size_t& index)
539 {
540  // Control sentence (if debug)
541 
542  #ifndef NDEBUG
543 
544  const size_t unscaling_neurons_number = get_unscaling_neurons_number();
545 
546  if(index >= unscaling_neurons_number)
547  {
548  std::ostringstream buffer;
549 
550  buffer << "OpenNN Exception: UnscalingLayer class.\n"
551  << "void prune_unscaling_neuron(const size_t&) method.\n"
552  << "Index of unscaling neuron is equal or greater than number of unscaling neurons.\n";
553 
554  throw std::logic_error(buffer.str());
555  }
556 
557  #endif
558 
559  statistics.erase(statistics.begin() + index);
560 }
561 
562 
563 // void check_range(const Vector<double>&) const method
564 
568 
569 void UnscalingLayer::check_range(const Vector<double>& outputs) const
570 {
571  const size_t unscaling_neurons_number = get_unscaling_neurons_number();
572 
573  // Control sentence (if debug)
574 
575  #ifndef NDEBUG
576 
577  const size_t size = outputs.size();
578 
579  if(size != unscaling_neurons_number)
580  {
581  std::ostringstream buffer;
582 
583  buffer << "OpenNN Exception: UnscalingLayer class.\n"
584  << "void check_range(const Vector<double>&) const method.\n"
585  << "Size of outputs must be equal to number of unscaling neurons.\n";
586 
587  throw std::logic_error(buffer.str());
588  }
589 
590  #endif
591 
592  // Check outputs
593 
594  if(display)
595  {
596  for(size_t i = 0; i < unscaling_neurons_number; i++)
597  {
598  if(outputs[i] < statistics[i].minimum)
599  {
600  std::cout << "OpenNN Warning: UnscalingLayer class.\n"
601  << "void check_range(const Vector<double>&) const method.\n"
602  << "Output variable " << i << " is less than outputs.\n";
603  }
604 
605  if(outputs[i] > statistics[i].maximum)
606  {
607  std::cout << "OpenNN Warning: UnscalingLayer class.\n"
608  << "void check_range(const Vector<double>&) const method.\n"
609  << "Output variable " << i << " is greater than maximum.\n";
610  }
611  }
612  }
613 }
614 
615 
616 // bool is_empty(void) const method
617 
619 
620 bool UnscalingLayer::is_empty(void) const
621 {
622  const size_t unscaling_neurons_number = get_unscaling_neurons_number();
623 
624  if(unscaling_neurons_number == 0)
625  {
626  return(true);
627  }
628  else
629  {
630  return(false);
631  }
632 }
633 
634 
635 // void initialize_random(void) method
636 
639 
641 {
642  const size_t unscaling_neurons_number = get_unscaling_neurons_number();
643 
644  // Statistics
645 
646  for(size_t i = 0; i < unscaling_neurons_number; i++)
647  {
648  statistics[i].initialize_random();
649  }
650 
651  // Unscaling method
652 
653  switch(rand()%2)
654  {
655  case 0:
656  {
657  unscaling_method = MinimumMaximum;
658  }
659  break;
660 
661  case 1:
662  {
663  unscaling_method = MeanStandardDeviation;
664  }
665  break;
666 
667  default:
668  {
669  std::ostringstream buffer;
670 
671  buffer << "OpenNN Exception: UnscalingLayer class.\n"
672  << "void initialize_random(void) method.\n"
673  << "Unknown unscaling method.\n";
674 
675  throw std::logic_error(buffer.str());
676  }
677  break;
678  }
679 }
680 
681 
682 // Vector<double> calculate_outputs(const Vector<double>&) const method
683 
686 
688 {
689  // Control sentence (if debug)
690 
691  #ifndef NDEBUG
692 
693  const size_t unscaling_neurons_number = get_unscaling_neurons_number();
694 
695  const size_t size = inputs.size();
696 
697  if(size != unscaling_neurons_number)
698  {
699  std::ostringstream buffer;
700 
701  buffer << "OpenNN Exception: UnscalingLayer class.\n"
702  << "Vector<double> calculate_outputs(const Vector<double>&) const method.\n"
703  << "Size must be equal to number of unscaling neurons.\n";
704 
705  throw std::logic_error(buffer.str());
706  }
707 
708  #endif
709 
710  // Unscale
711 
712  switch(unscaling_method)
713  {
714  case MinimumMaximum:
715  {
716  return(calculate_minimum_maximum_outputs(inputs));
717  }
718  break;
719 
720  case MeanStandardDeviation:
721  {
723  }
724  break;
725 
726  case NoUnscaling:
727  {
728  return(inputs);
729  }
730  break;
731 
732  default:
733  {
734  std::ostringstream buffer;
735 
736  buffer << "OpenNN Exception: UnscalingLayer class.\n"
737  << "Vector<double> calculate_outputs(const Vector<double>&) const method.\n"
738  << "Unknown unscaling method.\n";
739 
740  throw std::logic_error(buffer.str());
741  }
742  break;
743  }
744 }
745 
746 
747 // Vector<double> calculate_derivatives(const Vector<double>&) const method
748 
751 
753 {
754  // Control sentence (if debug)
755 
756 // #ifndef NDEBUG
757 //
758 // const size_t size = scaled_output.size();
759 //
760 // if(size != unscaling_neurons_number)
761 // {
762 // std::ostringstream buffer;
763 
764 // buffer << "OpenNN Exception: UnscalingLayer class.\n"
765 // << "Vector<double> calculate_derivatives(const Vector<double>&) const method.\n"
766 // << "Size must be equal to number of unscaling neurons.\n";
767 //
768 // throw std::logic_error(buffer.str());
769 // }
770 //
771 // #endif
772 
773  switch(unscaling_method)
774  {
775  case MinimumMaximum:
776  {
778  }
779  break;
780 
781  case MeanStandardDeviation:
782  {
784  }
785  break;
786 
787  default:
788  {
789  std::ostringstream buffer;
790 
791  buffer << "OpenNN Exception: UnscalingLayer class.\n"
792  << "Vector<double> calculate_derivatives(const Vector<double>&) const.\n"
793  << "Unknown scaling and unscaling method.\n";
794 
795  throw std::logic_error(buffer.str());
796  }
797  break;
798 
799  }// end switch
800 }
801 
802 
803 // Vector<double> calculate_second_derivatives(const Vector<double>&) const
804 
807 
809 {
810  switch(unscaling_method)
811  {
812  case MinimumMaximum:
813  {
815  }
816  break;
817 
818  case MeanStandardDeviation:
819  {
821  }
822  break;
823 
824  default:
825  {
826  std::ostringstream buffer;
827 
828  buffer << "OpenNN Exception: UnscalingLayer class.\n"
829  << "Vector<double> calculate_second_derivatives(const Vector<double>&) const.\n"
830  << "Unknown scaling and unscaling method.\n";
831 
832  throw std::logic_error(buffer.str());
833  }// end default
834  break;
835 
836  }// end switch
837 }
838 
839 
840 // Vector<double> calculate_minimum_maximum_outputs(const Vector<double>&) const method
841 
844 
846 {
847  const size_t unscaling_neurons_number = get_unscaling_neurons_number();
848 
849  Vector<double> outputs(unscaling_neurons_number);
850 
851  for(size_t i = 0; i < unscaling_neurons_number; i++)
852  {
853  if(statistics[i].maximum - statistics[i].minimum < 1e-99)
854  {
855  if(display)
856  {
857  std::cout << "OpenNN Warning: UnscalingLayer class.\n"
858  << "Vector<double> calculate_minimum_maximum_outputs(Vector<double>&) const method.\n"
859  << "Minimum and maximum values of output variable " << i << " are equal.\n"
860  << "Those outputs won't be unscaled.\n";
861  }
862 
863  outputs[i] = inputs[i];
864  }
865  else
866  {
867  outputs[i] = 0.5*(inputs[i] + 1.0)*(statistics[i].maximum-statistics[i].minimum) + statistics[i].minimum;
868  }
869  }
870 
871  return(outputs);
872 }
873 
874 
875 // Vector<double> calculate_minimum_maximum_derivatives(const Vector<double>&) const method
876 
879 
881 {
882  const size_t unscaling_neurons_number = get_unscaling_neurons_number();
883 
884  Vector<double> derivative(unscaling_neurons_number);
885 
886  for(size_t i = 0; i < unscaling_neurons_number; i++)
887  {
888  if(statistics[i].maximum-statistics[i].minimum < 1e-99)
889  {
890  if(display)
891  {
892  std::cout << "OpenNN Warning: UnscalingLayer class.\n"
893  << "Vector<double> calculate_minimum_maximum_derivatives(const Vector<double>&) const.\n"
894  << "Minimum and maximum values of output variable " << i << " are equal.\n"
895  << "Those derivatives won't be unscaled.\n";
896  }
897 
898  derivative[i] = 1.0;
899  }
900  else
901  {
902  derivative[i] = 0.5*(statistics[i].maximum-statistics[i].minimum);
903  }
904  }
905 
906  return(derivative);
907 }
908 
909 
910 // Vector<double> calculate_minimum_maximum_second_derivatives(const Vector<double>&) const method
911 
914 
916 {
917  const size_t unscaling_neurons_number = get_unscaling_neurons_number();
918 
919  const Vector<double> unscaled_second_derivative(unscaling_neurons_number, 0.0);
920 
921  return(unscaled_second_derivative);
922 }
923 
924 
925 // Vector<double> calculate_mean_standard_deviation_outputs(const Vector<double>&) const method
926 
929 
931 {
932  const size_t unscaling_neurons_number = get_unscaling_neurons_number();
933 
934  Vector<double> outputs(unscaling_neurons_number);
935 
936  for(size_t i = 0; i < unscaling_neurons_number; i++)
937  {
938  if(statistics[i].standard_deviation < 1e-99)
939  {
940  if(display)
941  {
942  std::cout << "OpenNN Warning: UnscalingLayer class.\n"
943  << "Vector<double> calculate_mean_standard_deviation_outputs(const Vector<double>&) const method.\n"
944  << "Standard deviation of output variable " << i << " is zero.\n"
945  << "Those outputs won't be unscaled.\n";
946  }
947 
948  outputs[i] = inputs[i];
949  }
950  else
951  {
952  outputs[i] = inputs[i]*statistics[i].standard_deviation + statistics[i].mean;
953  }
954  }
955 
956  return(outputs);
957 }
958 
959 
960 // Vector<double> calculate_mean_standard_deviation_derivatives(const Vector<double>&) const method
961 
964 
966 {
967  const size_t unscaling_neurons_number = get_unscaling_neurons_number();
968 
969  Vector<double> unscaled_derivative(unscaling_neurons_number, 0.0);
970 
971  for(size_t i = 0; i < unscaling_neurons_number; i++)
972  {
973  if(statistics[i].standard_deviation < 1e-99)
974  {
975  if(display)
976  {
977  std::cout << "OpenNN Warning: UnscalingLayer class.\n"
978  << "Vector<double> calculate_mean_standard_deviation_derivatives(const Vector<double>&) const.\n"
979  << "Standard deviation of output variable " << i << " is zero.\n"
980  << "Those derivatives won't be unscaled.\n";
981  }
982 
983  unscaled_derivative[i] = 1.0;
984  }
985  else
986  {
987  unscaled_derivative[i] = statistics[i].standard_deviation;
988  }
989  }
990 
991  return(unscaled_derivative);
992 }
993 
994 
995 // Vector<double> calculate_mean_standard_deviation_second_derivatives(const Vector<double>&) const method
996 
999 
1001 {
1002  const size_t unscaling_neurons_number = get_unscaling_neurons_number();
1003 
1004  const Vector<double> unscaled_second_derivative(unscaling_neurons_number, 0.0);
1005 
1006  return(unscaled_second_derivative);
1007 }
1008 
1009 
1010 // Matrix<double> arrange_Jacobian(const Vector<double>&) const method
1011 
1013 
1015 {
1016  const size_t unscaling_neurons_number = get_unscaling_neurons_number();
1017 
1018  Matrix<double> Jacobian(unscaling_neurons_number, unscaling_neurons_number, 0.0);
1019 
1020  Jacobian.set_diagonal(derivatives);
1021 
1022  return(Jacobian);
1023 }
1024 
1025 
1026 // Vector< Matrix<double> > arrange_Hessian_form(const Vector<double>&) const method
1027 
1029 
1031 {
1032  const size_t unscaling_neurons_number = get_unscaling_neurons_number();
1033 
1034  Vector< Matrix<double> > Hessian_form(unscaling_neurons_number);
1035 
1036  for(size_t i = 0; i < unscaling_neurons_number; i++)
1037  {
1038  Hessian_form[i].set(unscaling_neurons_number, unscaling_neurons_number, 0.0);
1039 
1040  Hessian_form[i](i,i) = second_derivative[i];
1041  }
1042  return(Hessian_form);
1043 }
1044 
1045 
1046 // std::string to_string(void) const method
1047 
1049 
1050 std::string UnscalingLayer::to_string(void) const
1051 {
1052  std::ostringstream buffer;
1053 
1054  const size_t unscaling_neurons_number = get_unscaling_neurons_number();
1055 
1056  buffer << "Unscaling layer\n";
1057 
1058  for(size_t i = 0; i < unscaling_neurons_number; i++)
1059  {
1060  buffer << "Statistics " << i+1 << ":\n"
1061  << "Minimum: " << statistics[i].minimum << "\n"
1062  << "Maximum: " << statistics[i].maximum << "\n"
1063  << "Mean: " << statistics[i].mean << "\n"
1064  << "Standard deviation: " << statistics[i].standard_deviation << "\n";
1065  }
1066 
1067  buffer << "Unscaling method: " << write_unscaling_method() << "\n"
1068  << "Display: " << display << "\n";
1069 
1070  return(buffer.str());
1071 }
1072 
1073 
1074 // tinyxml2::XMLDocument* to_XML(void) const method
1075 
1078 
1079 tinyxml2::XMLDocument* UnscalingLayer::to_XML(void) const
1080 {
1081  std::ostringstream buffer;
1082 
1083  tinyxml2::XMLDocument* document = new tinyxml2::XMLDocument;
1084 
1085  tinyxml2::XMLElement* unscaling_layer_element = document->NewElement("UnscalingLayer");
1086 
1087  document->InsertFirstChild(unscaling_layer_element);
1088 
1089  tinyxml2::XMLElement* element = NULL;
1090  tinyxml2::XMLText* text = NULL;
1091 
1092  const size_t unscaling_neurons_number = get_unscaling_neurons_number();
1093 
1094  // Unscaling neurons number
1095  {
1096  element = document->NewElement("UnscalingNeuronsNumber");
1097  unscaling_layer_element->LinkEndChild(element);
1098 
1099  buffer.str("");
1100  buffer << unscaling_neurons_number;
1101 
1102  text = document->NewText(buffer.str().c_str());
1103  element->LinkEndChild(text);
1104  }
1105 
1106  for(size_t i = 0; i < unscaling_neurons_number; i++)
1107  {
1108  tinyxml2::XMLElement* statistics_element = document->NewElement("Statistics");
1109  statistics_element->SetAttribute("Index", (unsigned)i+1);
1110 
1111  unscaling_layer_element->LinkEndChild(statistics_element);
1112 
1113  // Minimum
1114 
1115  tinyxml2::XMLElement* minimum_element = document->NewElement("Minimum");
1116  statistics_element->LinkEndChild(minimum_element);
1117 
1118  buffer.str("");
1119  buffer << statistics[i].minimum;
1120 
1121  tinyxml2::XMLText* minimum_text = document->NewText(buffer.str().c_str());
1122  minimum_element->LinkEndChild(minimum_text);
1123 
1124  // Maximum
1125 
1126  tinyxml2::XMLElement* maximum_element = document->NewElement("Maximum");
1127  statistics_element->LinkEndChild(maximum_element);
1128 
1129  buffer.str("");
1130  buffer << statistics[i].maximum;
1131 
1132  tinyxml2::XMLText* maximum_text = document->NewText(buffer.str().c_str());
1133  maximum_element->LinkEndChild(maximum_text);
1134 
1135  // Mean
1136 
1137  tinyxml2::XMLElement* mean_element = document->NewElement("Mean");
1138  statistics_element->LinkEndChild(mean_element);
1139 
1140  buffer.str("");
1141  buffer << statistics[i].mean;
1142 
1143  tinyxml2::XMLText* mean_text = document->NewText(buffer.str().c_str());
1144  mean_element->LinkEndChild(mean_text);
1145 
1146  // Standard deviation
1147 
1148  tinyxml2::XMLElement* standard_deviation_element = document->NewElement("StandardDeviation");
1149  statistics_element->LinkEndChild(standard_deviation_element);
1150 
1151  buffer.str("");
1152  buffer << statistics[i].standard_deviation;
1153 
1154  tinyxml2::XMLText* standard_deviation_text = document->NewText(buffer.str().c_str());
1155  standard_deviation_element->LinkEndChild(standard_deviation_text);
1156  }
1157 
1158  // Unscaling method
1159  {
1160  element = document->NewElement("UnscalingMethod");
1161  unscaling_layer_element->LinkEndChild(element);
1162 
1163  text = document->NewText(write_unscaling_method().c_str());
1164  element->LinkEndChild(text);
1165  }
1166 
1167  // Display
1168  {
1169  element = document->NewElement("Display");
1170  unscaling_layer_element->LinkEndChild(element);
1171 
1172  buffer.str("");
1173  buffer << display;
1174 
1175  text = document->NewText(buffer.str().c_str());
1176  element->LinkEndChild(text);
1177  }
1178 
1179  return(document);
1180 }
1181 
1182 
1183 // void from_XML(const tinyxml2::XMLDocument&) method
1184 
1187 
1188 void UnscalingLayer::from_XML(const tinyxml2::XMLDocument& document)
1189 {
1190  std::ostringstream buffer;
1191 
1192  const tinyxml2::XMLElement* root_element = document.FirstChildElement("UnscalingLayer");
1193 
1194  if(!root_element)
1195  {
1196  buffer << "OpenNN Exception: UnscalingLayer class.\n"
1197  << "void from_XML(const tinyxml2::XMLDocument&) method.\n"
1198  << "Unscaling layer element is NULL.\n";
1199 
1200  throw std::logic_error(buffer.str());
1201  }
1202 
1203  // Unscaling neurons number
1204 
1205  const tinyxml2::XMLElement* unscaling_neurons_number_element = root_element->FirstChildElement("UnscalingNeuronsNumber");
1206 
1207  if(!unscaling_neurons_number_element)
1208  {
1209  buffer << "OpenNN Exception: UnscalingLayer class.\n"
1210  << "void from_XML(const tinyxml2::XMLDocument&) method.\n"
1211  << "Unscaling neurons number element is NULL.\n";
1212 
1213  throw std::logic_error(buffer.str());
1214  }
1215 
1216  const size_t unscaling_neurons_number = atoi(unscaling_neurons_number_element->GetText());
1217 
1218  set(unscaling_neurons_number);
1219 
1220  unsigned index = 0; // size_t does not work
1221 
1222  const tinyxml2::XMLElement* start_element = unscaling_neurons_number_element;
1223 
1224  for(size_t i = 0; i < unscaling_neurons_number; i++)
1225  {
1226  const tinyxml2::XMLElement* statistics_element = start_element->NextSiblingElement("Statistics");
1227  start_element = statistics_element;
1228 
1229  if(!statistics_element)
1230  {
1231  buffer << "OpenNN Exception: UnscalingLayer class.\n"
1232  << "void from_XML(const tinyxml2::XMLElement*) method.\n"
1233  << "Statistics of unscaling neuron " << i+1 << " is NULL.\n";
1234 
1235  throw std::logic_error(buffer.str());
1236  }
1237 
1238  statistics_element->QueryUnsignedAttribute("Index", &index);
1239 
1240  if(index != i+1)
1241  {
1242  buffer << "OpenNN Exception: UnscalingLayer class.\n"
1243  << "void from_XML(const tinyxml2::XMLElement*) method.\n"
1244  << "Index " << index << " is not correct.\n";
1245 
1246  throw std::logic_error(buffer.str());
1247  }
1248 
1249  // Minimum
1250 
1251  const tinyxml2::XMLElement* minimum_element = statistics_element->FirstChildElement("Minimum");
1252 
1253  if(!minimum_element)
1254  {
1255  buffer << "OpenNN Exception: UnscalingLayer class.\n"
1256  << "void from_XML(const tinyxml2::XMLElement*) method.\n"
1257  << "Minimum element " << i+1 << " is NULL.\n";
1258 
1259  throw std::logic_error(buffer.str());
1260  }
1261 
1262  if(minimum_element->GetText())
1263  {
1264  statistics[i].minimum = atof(minimum_element->GetText());
1265  }
1266 
1267  // Maximum
1268 
1269  const tinyxml2::XMLElement* maximum_element = statistics_element->FirstChildElement("Maximum");
1270 
1271  if(!maximum_element)
1272  {
1273  buffer << "OpenNN Exception: UnscalingLayer class.\n"
1274  << "void from_XML(const tinyxml2::XMLElement*) method.\n"
1275  << "Maximum element " << i+1 << " is NULL.\n";
1276 
1277  throw std::logic_error(buffer.str());
1278  }
1279 
1280  if(maximum_element->GetText())
1281  {
1282  statistics[i].maximum = atof(maximum_element->GetText());
1283  }
1284 
1285  // Mean
1286 
1287  const tinyxml2::XMLElement* mean_element = statistics_element->FirstChildElement("Mean");
1288 
1289  if(!mean_element)
1290  {
1291  buffer << "OpenNN Exception: UnscalingLayer class.\n"
1292  << "void from_XML(const tinyxml2::XMLElement*) method.\n"
1293  << "Mean element " << i+1 << " is NULL.\n";
1294 
1295  throw std::logic_error(buffer.str());
1296  }
1297 
1298  if(mean_element->GetText())
1299  {
1300  statistics[i].mean = atof(mean_element->GetText());
1301  }
1302 
1303  // Standard deviation
1304 
1305  const tinyxml2::XMLElement* standard_deviation_element = statistics_element->FirstChildElement("StandardDeviation");
1306 
1307  if(!standard_deviation_element)
1308  {
1309  buffer << "OpenNN Exception: UnscalingLayer class.\n"
1310  << "void from_XML(const tinyxml2::XMLElement*) method.\n"
1311  << "Standard deviation element " << i+1 << " is NULL.\n";
1312 
1313  throw std::logic_error(buffer.str());
1314  }
1315 
1316  if(standard_deviation_element->GetText())
1317  {
1318  statistics[i].standard_deviation = atof(standard_deviation_element->GetText());
1319  }
1320  }
1321 
1322  // Unscaling method
1323 
1324  const tinyxml2::XMLElement* unscaling_method_element = root_element->FirstChildElement("UnscalingMethod");
1325 
1326  if(unscaling_method_element)
1327  {
1328  const std::string new_method = unscaling_method_element->GetText();
1329 
1330  try
1331  {
1332  set_unscaling_method(new_method);
1333  }
1334  catch(const std::logic_error& e)
1335  {
1336  std::cout << e.what() << std::endl;
1337  }
1338  }
1339 
1340  // Display
1341 
1342  const tinyxml2::XMLElement* element = root_element->FirstChildElement("Display");
1343 
1344  if(element)
1345  {
1346  std::string new_display_string = element->GetText();
1347 
1348  try
1349  {
1350  set_display(new_display_string != "0");
1351  }
1352  catch(const std::logic_error& e)
1353  {
1354  std::cout << e.what() << std::endl;
1355  }
1356  }
1357 }
1358 
1359 
1360 // std::string write_none_expression(const Vector<std::string>&, const Vector<std::string>&) const method
1361 
1365 
1366 std::string UnscalingLayer::write_none_expression(const Vector<std::string>& inputs_name, const Vector<std::string>& outputs_name) const
1367 {
1368  const size_t unscaling_neurons_number = get_unscaling_neurons_number();
1369 
1370  std::ostringstream buffer;
1371 
1372  for(size_t i = 0; i < unscaling_neurons_number; i++)
1373  {
1374  buffer << outputs_name[i] << "=" << inputs_name[i] << ";\n";
1375  }
1376 
1377  return(buffer.str());
1378 }
1379 
1380 
1381 // std::string write_minimum_maximum_expression(const Vector<std::string>&, const Vector<std::string>&) const method
1382 
1386 
1387 std::string UnscalingLayer::write_minimum_maximum_expression(const Vector<std::string>& inputs_name, const Vector<std::string>& outputs_name) const
1388 {
1389  const size_t unscaling_neurons_number = get_unscaling_neurons_number();
1390 
1391  std::ostringstream buffer;
1392 
1393  for(size_t i = 0; i < unscaling_neurons_number; i++)
1394  {
1395  buffer << outputs_name[i] << "=0.5*(" << inputs_name[i] << "+1.0)*(" << statistics[i].maximum << "-" << statistics[i].minimum << ")+" << statistics[i].minimum << ";\n";
1396  }
1397 
1398  return(buffer.str());
1399 }
1400 
1401 
1402 // std::string write_mean_stadard_deviation_expression(const Vector<std::string>&, const Vector<std::string>&) const method
1403 
1407 
1409 {
1410  const size_t unscaling_neurons_number = get_unscaling_neurons_number();
1411 
1412  std::ostringstream buffer;
1413 
1414  for(size_t i = 0; i < unscaling_neurons_number; i++)
1415  {
1416  buffer << outputs_name[i] << "=" << statistics[i].mean << "+" << statistics[i].standard_deviation << "*" << inputs_name[i] << ";\n";
1417  }
1418 
1419  return(buffer.str());
1420 }
1421 
1422 
1423 // std::string write_expression(const Vector<std::string>&, const Vector<std::string>&) const method
1424 
1428 
1429 std::string UnscalingLayer::write_expression(const Vector<std::string>& inputs_name, const Vector<std::string>& outputs_name) const
1430 {
1431  switch(unscaling_method)
1432  {
1433  case NoUnscaling:
1434  {
1435  return(write_none_expression(inputs_name, outputs_name));
1436  }
1437  break;
1438 
1439  case MinimumMaximum:
1440  {
1441  return(write_minimum_maximum_expression(inputs_name, outputs_name));
1442  }
1443  break;
1444 
1445  case MeanStandardDeviation:
1446  {
1447  return(write_mean_stadard_deviation_expression(inputs_name, outputs_name));
1448  }
1449  break;
1450 
1451  default:
1452  {
1453  std::ostringstream buffer;
1454 
1455  buffer << "OpenNN Exception: UnscalingLayer class.\n"
1456  << "std::string write_expression(const Vector<std::string>&, const Vector<std::string>&) const method.\n"
1457  << "Unknown unscaling method.\n";
1458 
1459  throw std::logic_error(buffer.str());
1460  }// end default
1461  break;
1462  }
1463 }
1464 
1465 }
1466 
1467 
1468 // OpenNN: Open Neural Networks Library.
1469 // Copyright (c) 2005-2015 Roberto Lopez.
1470 //
1471 // This library is free software; you can redistribute it and/or
1472 // modify it under the terms of the GNU Lesser General Public
1473 // License as published by the Free Software Foundation; either
1474 // version 2.1 of the License, or any later version.
1475 //
1476 // This library is distributed in the hope that it will be useful,
1477 // but WITHOUT ANY WARRANTY; without even the implied warranty of
1478 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1479 // Lesser General Public License for more details.
1480 
1481 // You should have received a copy of the GNU Lesser General Public
1482 // License along with this library; if not, write to the Free Software
1483 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
void set_item_statistics(const size_t &, const Statistics< double > &)
Vector< double > calculate_mean_standard_deviation_outputs(const Vector< double > &) const
Matrix< double > arrange_Jacobian(const Vector< double > &) const
Arranges a "Jacobian" matrix from the vector of derivatives.
bool is_empty(void) const
Returns true if the number of unscaling neurons is zero, and false otherwise.
void set_unscaling_method(const UnscalingMethod &)
std::string write_unscaling_method_text(void) const
bool display
Display warning messages to screen.
Vector< double > calculate_mean_standard_deviation_derivatives(const Vector< double > &) const
Vector< double > arrange_minimums(void) const
void set_minimum(const size_t &, const double &)
virtual ~UnscalingLayer(void)
Destructor.
std::string to_string(void) const
Returns a string representation of the current unscaling layer object.
void set(void)
Sets the size of a vector to zero.
Definition: vector.h:656
void set_maximum(const size_t &, const double &)
void prune_unscaling_neuron(const size_t &)
const bool & get_display(void) const
virtual void set_default(void)
void set_diagonal(const T &)
Definition: matrix.h:1858
Matrix< double > arrange_statistics(void) const
std::string write_unscaling_method(void) const
const UnscalingMethod & get_unscaling_method(void) const
void check_range(const Vector< double > &) const
Vector< double > calculate_second_derivatives(const Vector< double > &) const
Vector< double > calculate_derivatives(const Vector< double > &) const
Vector< double > calculate_mean_standard_deviation_second_derivatives(const Vector< double > &) const
UnscalingMethod unscaling_method
Unscaling method for the output variables.
UnscalingLayer(void)
Default constructor.
void set_mean(const size_t &, const double &)
UnscalingLayer & operator=(const UnscalingLayer &)
void set_statistics(const Vector< Statistics< double > > &)
Vector< Matrix< double > > arrange_Hessian_form(const Vector< double > &) const
Arranges a "Hessian form" vector of matrices from the vector of second derivatives.
std::string write_none_expression(const Vector< std::string > &, const Vector< std::string > &) const
void from_XML(const tinyxml2::XMLDocument &)
std::string write_expression(const Vector< std::string > &, const Vector< std::string > &) const
std::string write_minimum_maximum_expression(const Vector< std::string > &, const Vector< std::string > &) const
size_t get_unscaling_neurons_number(void) const
Returns the number of unscaling neurons in this layer.
Vector< double > calculate_outputs(const Vector< double > &) const
bool operator==(const UnscalingLayer &) const
Vector< Statistics< double > > get_statistics(void) const
Vector< double > calculate_minimum_maximum_second_derivatives(const Vector< double > &) const
Vector< double > calculate_minimum_maximum_outputs(const Vector< double > &) const
void set(void)
Sets the unscaling layer to be empty.
void set_standard_deviation(const size_t &, const double &)
void set_display(const bool &)
tinyxml2::XMLDocument * to_XML(void) const
Vector< double > arrange_maximums(void) const
Vector< Statistics< double > > statistics
Statistics of output variables.
void set_row(const size_t &, const Vector< T > &)
Definition: matrix.h:1691
std::string write_mean_stadard_deviation_expression(const Vector< std::string > &, const Vector< std::string > &) const
UnscalingMethod
Enumeration of available methods for input variables, output variables and independent parameters sca...
Vector< double > calculate_minimum_maximum_derivatives(const Vector< double > &) const