OpenNN  2.2
Open Neural Networks Library
neural_network.cpp
1 /****************************************************************************************************************/
2 /* */
3 /* OpenNN: Open Neural Networks Library */
4 /* www.artelnics.com/opennn */
5 /* */
6 /* N E U R A L N E T W O R K 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 "neural_network.h"
17 
18 namespace OpenNN
19 {
20 
21 // DEFAULT CONSTRUCTOR
22 
27 
29  : multilayer_perceptron_pointer(NULL)
30  , scaling_layer_pointer(NULL)
31  , unscaling_layer_pointer(NULL)
32  , bounding_layer_pointer(NULL)
33  , probabilistic_layer_pointer(NULL)
34  , conditions_layer_pointer(NULL)
35  , inputs_pointer(NULL)
36  , outputs_pointer(NULL)
37  , independent_parameters_pointer(NULL)
38 {
39  set_default();
40 }
41 
42 
43 // MULTILAYER PERCEPTRON CONSTRUCTOR
44 
49 
50 NeuralNetwork::NeuralNetwork(const MultilayerPerceptron& new_multilayer_perceptron)
51  : multilayer_perceptron_pointer(NULL)
52  , scaling_layer_pointer(NULL)
53  , unscaling_layer_pointer(NULL)
54  , bounding_layer_pointer(NULL)
55  , probabilistic_layer_pointer(NULL)
56  , conditions_layer_pointer(NULL)
57  , inputs_pointer(NULL)
58  , outputs_pointer(NULL)
59  , independent_parameters_pointer(NULL)
60 {
61  multilayer_perceptron_pointer = new MultilayerPerceptron(new_multilayer_perceptron);
62 
63  const size_t inputs_number = multilayer_perceptron_pointer->get_inputs_number();
64  const size_t outputs_number = multilayer_perceptron_pointer->get_outputs_number();
65 
66  inputs_pointer = new Inputs(inputs_number);
67  outputs_pointer = new Outputs(outputs_number);
68 
69  set_default();
70 }
71 
72 
73 // MULTILAYER PERCEPTRON ARCHITECTURE CONSTRUCTOR
74 
82 
83 NeuralNetwork::NeuralNetwork(const Vector<size_t>& new_multilayer_perceptron_architecture)
84  : multilayer_perceptron_pointer(NULL)
85  , scaling_layer_pointer(NULL)
86  , unscaling_layer_pointer(NULL)
87  , bounding_layer_pointer(NULL)
88  , probabilistic_layer_pointer(NULL)
89  , conditions_layer_pointer(NULL)
90  , inputs_pointer(NULL)
91  , outputs_pointer(NULL)
92  , independent_parameters_pointer(NULL)
93 {
94  multilayer_perceptron_pointer = new MultilayerPerceptron(new_multilayer_perceptron_architecture);
95 
96  const size_t inputs_number = multilayer_perceptron_pointer->get_inputs_number();
97  const size_t outputs_number = multilayer_perceptron_pointer->get_outputs_number();
98 
99  inputs_pointer = new Inputs(inputs_number);
100  outputs_pointer = new Outputs(outputs_number);
101 
102  set(new_multilayer_perceptron_architecture);
103 }
104 
105 
106 // ONE LAYER CONSTRUCTOR
107 
114 
115 NeuralNetwork::NeuralNetwork(const size_t& new_inputs_number, const size_t& new_perceptrons_number)
116  : multilayer_perceptron_pointer(NULL)
117  , scaling_layer_pointer(NULL)
118  , unscaling_layer_pointer(NULL)
119  , bounding_layer_pointer(NULL)
120  , probabilistic_layer_pointer(NULL)
121  , conditions_layer_pointer(NULL)
122  , inputs_pointer(NULL)
123  , outputs_pointer(NULL)
124  , independent_parameters_pointer(NULL)
125 {
126  multilayer_perceptron_pointer = new MultilayerPerceptron(new_inputs_number, new_perceptrons_number);
127 
128  const size_t inputs_number = multilayer_perceptron_pointer->get_inputs_number();
129  const size_t outputs_number = multilayer_perceptron_pointer->get_outputs_number();
130 
131  inputs_pointer = new Inputs(inputs_number);
132  outputs_pointer = new Outputs(outputs_number);
133 
134  set_default();
135 }
136 
137 
138 // TWO LAYERS CONSTRUCTOR
139 
147 
148 NeuralNetwork::NeuralNetwork(const size_t& new_inputs_number, const size_t& new_hidden_perceptrons_number, const size_t& new_output_perceptrons_number)
149  : multilayer_perceptron_pointer(NULL)
150  , scaling_layer_pointer(NULL)
151  , unscaling_layer_pointer(NULL)
152  , bounding_layer_pointer(NULL)
153  , probabilistic_layer_pointer(NULL)
154  , conditions_layer_pointer(NULL)
155  , inputs_pointer(NULL)
156  , outputs_pointer(NULL)
157  , independent_parameters_pointer(NULL)
158 {
159  multilayer_perceptron_pointer = new MultilayerPerceptron(new_inputs_number, new_hidden_perceptrons_number, new_output_perceptrons_number);
160 
161  const size_t inputs_number = multilayer_perceptron_pointer->get_inputs_number();
162  const size_t outputs_number = multilayer_perceptron_pointer->get_outputs_number();
163 
164  inputs_pointer = new Inputs(inputs_number);
165  outputs_pointer = new Outputs(outputs_number);
166 
167  set_default();
168 }
169 
170 
171 // INDEPENDENT PARAMETERS CONSTRUCTOR
172 
177 
178 NeuralNetwork::NeuralNetwork(const size_t& new_independent_parameters_number)
179  : multilayer_perceptron_pointer(NULL)
180  , scaling_layer_pointer(NULL)
181  , unscaling_layer_pointer(NULL)
182  , bounding_layer_pointer(NULL)
183  , probabilistic_layer_pointer(NULL)
184  , conditions_layer_pointer(NULL)
185  , inputs_pointer(NULL)
186  , outputs_pointer(NULL)
187  , independent_parameters_pointer(NULL)
188 {
189  independent_parameters_pointer = new IndependentParameters(new_independent_parameters_number);
190 
191  set_default();
192 }
193 
194 
195 // FILE CONSTRUCTOR
196 
201 
202 NeuralNetwork::NeuralNetwork(const std::string& file_name)
203  : multilayer_perceptron_pointer(NULL)
204  , scaling_layer_pointer(NULL)
205  , unscaling_layer_pointer(NULL)
206  , bounding_layer_pointer(NULL)
207  , probabilistic_layer_pointer(NULL)
208  , conditions_layer_pointer(NULL)
209  , inputs_pointer(NULL)
210  , outputs_pointer(NULL)
211  , independent_parameters_pointer(NULL)
212 {
213  load(file_name);
214 }
215 
216 
217 // XML CONSTRUCTOR
218 
222 
223 NeuralNetwork::NeuralNetwork(const tinyxml2::XMLDocument& document)
224  : multilayer_perceptron_pointer(NULL)
225  , scaling_layer_pointer(NULL)
226  , unscaling_layer_pointer(NULL)
227  , bounding_layer_pointer(NULL)
228  , probabilistic_layer_pointer(NULL)
229  , conditions_layer_pointer(NULL)
230  , inputs_pointer(NULL)
231  , outputs_pointer(NULL)
232  , independent_parameters_pointer(NULL)
233 {
234  from_XML(document);
235 }
236 
237 
238 // COPY CONSTRUCTOR
239 
243 
244 NeuralNetwork::NeuralNetwork(const NeuralNetwork& other_neural_network)
245  : multilayer_perceptron_pointer(NULL)
246  , scaling_layer_pointer(NULL)
247  , unscaling_layer_pointer(NULL)
248  , bounding_layer_pointer(NULL)
249  , probabilistic_layer_pointer(NULL)
250  , conditions_layer_pointer(NULL)
251  , inputs_pointer(NULL)
252  , outputs_pointer(NULL)
253  , independent_parameters_pointer(NULL)
254 {
255  set(other_neural_network);
256 }
257 
258 
259 // DESTRUCTOR
260 
262 
264 {
266  delete scaling_layer_pointer;
268  delete bounding_layer_pointer;
271  delete inputs_pointer;
272  delete outputs_pointer;
274 }
275 
276 
277 // ASSIGNMENT OPERATOR
278 
282 
284 {
285  if(this != &other_neural_network)
286  {
287  delete_pointers();
288 
289  inputs_pointer = new Inputs(*other_neural_network.inputs_pointer);
290 
291  outputs_pointer = new Outputs(*other_neural_network.outputs_pointer);
292 
294 
295  scaling_layer_pointer = new ScalingLayer(*other_neural_network.scaling_layer_pointer);
296 
298 
299  bounding_layer_pointer = new BoundingLayer(*other_neural_network.bounding_layer_pointer);
300 
302 
304 
306 
307  display = other_neural_network.display;
308  }
309 
310  return(*this);
311 }
312 
313 
314 // EQUAL TO OPERATOR
315 
318 
319 bool NeuralNetwork::operator == (const NeuralNetwork& other_neural_network) const
320 {
322  && *scaling_layer_pointer == *other_neural_network.scaling_layer_pointer
323  && *unscaling_layer_pointer == *other_neural_network.unscaling_layer_pointer
324  && *bounding_layer_pointer == *other_neural_network.bounding_layer_pointer
325  && *probabilistic_layer_pointer == *other_neural_network.probabilistic_layer_pointer
326  && *conditions_layer_pointer == *other_neural_network.conditions_layer_pointer
328  && *inputs_pointer == *other_neural_network.inputs_pointer
329  && *outputs_pointer == *other_neural_network.outputs_pointer
330  && display == other_neural_network.display)
331  {
332  return(true);
333  }
334  else
335  {
336  return(false);
337  }
338 }
339 
340 
341 // METHODS
342 
343 // bool has_multilayer_perceptron(void) const method
344 
347 
349 {
351  {
352  return(true);
353  }
354  else
355  {
356  return(false);
357  }
358 }
359 
360 
361 // bool has_inputs(void) const method
362 
365 
367 {
368  if(inputs_pointer)
369  {
370  return(true);
371  }
372  else
373  {
374  return(false);
375  }
376 }
377 
378 
379 // bool has_outputs(void) const method
380 
383 
385 {
386  if(outputs_pointer)
387  {
388  return(true);
389  }
390  else
391  {
392  return(false);
393  }
394 }
395 
396 
397 // bool has_scaling_layer(void) const method
398 
401 
403 {
405  {
406  return(true);
407  }
408  else
409  {
410  return(false);
411  }
412 }
413 
414 
415 // bool has_unscaling_layer(void) const method
416 
419 
421 {
423  {
424  return(true);
425  }
426  else
427  {
428  return(false);
429  }
430 }
431 
432 
433 // bool has_bounding_layer(void) const method
434 
437 
439 {
441  {
442  return(true);
443  }
444  else
445  {
446  return(false);
447  }
448 }
449 
450 
451 // bool has_probabilistic_layer(void) const method
452 
455 
457 {
459  {
460  return(true);
461  }
462  else
463  {
464  return(false);
465  }
466 }
467 
468 
469 // bool has_conditions_layer(void) const method
470 
473 
475 {
477  {
478  return(true);
479  }
480  else
481  {
482  return(false);
483  }
484 }
485 
486 
487 // bool has_independent_parameters(void) const method
488 
491 
493 {
495  {
496  return(true);
497  }
498  else
499  {
500  return(false);
501  }
502 }
503 
504 
505 // Multilayer perceptron* get_multilayer_perceptron_pointer(void) const method
506 
508 
510 {
511  #ifndef NDEBUG
512 
514  {
515  std::ostringstream buffer;
516 
517  buffer << "OpenNN Exception: NeuralNetwork class.\n"
518  << "MultilayerPerceptron* get_multilayer_perceptron_pointer(void) const method.\n"
519  << "Multilayer perceptron pointer is NULL.\n";
520 
521  throw std::logic_error(buffer.str());
522  }
523 
524  #endif
525 
527 }
528 
529 
530 // ScalingLayer get_scaling_layer_pointer(void) const method
531 
533 
535 {
536  #ifndef NDEBUG
537 
539  {
540  std::ostringstream buffer;
541 
542  buffer << "OpenNN Exception: NeuralNetwork class.\n"
543  << "ScalingLayer* get_scaling_layer_pointer(void) const method.\n"
544  << "Scaling layer pointer is NULL.\n";
545 
546  throw std::logic_error(buffer.str());
547  }
548 
549  #endif
550 
551  return(scaling_layer_pointer);
552 }
553 
554 
555 // UnscalingLayer* get_unscaling_layer_pointer(void) const method
556 
558 
560 {
561  #ifndef NDEBUG
562 
564  {
565  std::ostringstream buffer;
566 
567  buffer << "OpenNN Exception: NeuralNetwork class.\n"
568  << "UnscalingLayer* get_unscaling_layer_pointer(void) const method.\n"
569  << "Unscaling layer pointer is NULL.\n";
570 
571  throw std::logic_error(buffer.str());
572  }
573 
574  #endif
575 
576  return(unscaling_layer_pointer);
577 }
578 
579 
580 // BoundingLayer* get_bounding_layer_pointer(void) const method
581 
583 
585 {
586  #ifndef NDEBUG
587 
589  {
590  std::ostringstream buffer;
591 
592  buffer << "OpenNN Exception: NeuralNetwork class.\n"
593  << "BoundingLayer* get_bounding_layer_pointer(void) const method.\n"
594  << "Bounding layer pointer is NULL.\n";
595 
596  throw std::logic_error(buffer.str());
597  }
598 
599  #endif
600 
601  return(bounding_layer_pointer);
602 }
603 
604 
605 // ProbabilisticLayer* get_probabilistic_layer_pointer(void) const method
606 
608 
610 {
611  #ifndef NDEBUG
612 
614  {
615  std::ostringstream buffer;
616 
617  buffer << "OpenNN Exception: NeuralNetwork class.\n"
618  << "ProbabilisticLayer* get_probabilistic_layer_pointer(void) const method.\n"
619  << "Probabilistic layer pointer is NULL.\n";
620 
621  throw std::logic_error(buffer.str());
622  }
623 
624  #endif
625 
627 }
628 
629 
630 // ConditionsLayer* get_conditions_layer(void) const method
631 
633 
635 {
636  #ifndef NDEBUG
637 
639  {
640  std::ostringstream buffer;
641 
642  buffer << "OpenNN Exception: NeuralNetwork class.\n"
643  << "ConditionsLayer* get_conditions_layer_pointer(void) const method.\n"
644  << "Conditions layer pointer is NULL.\n";
645 
646  throw std::logic_error(buffer.str());
647  }
648 
649  #endif
650 
651  return(conditions_layer_pointer);
652 }
653 
654 
655 // Inputs& get_inputs_pointer(void) const method
656 
658 
660 {
661  #ifndef NDEBUG
662 
663  if(!inputs_pointer)
664  {
665  std::ostringstream buffer;
666 
667  buffer << "OpenNN Exception: NeuralNetwork class.\n"
668  << "Inputs* get_inputs_pointer(void) const method.\n"
669  << "Inputs pointer is NULL.\n";
670 
671  throw std::logic_error(buffer.str());
672  }
673 
674  #endif
675 
676  return(inputs_pointer);
677 }
678 
679 
680 // Inputs& get_outputs_pointer(void) const method
681 
683 
685 {
686  #ifndef NDEBUG
687 
688  if(!outputs_pointer)
689  {
690  std::ostringstream buffer;
691 
692  buffer << "OpenNN Exception: NeuralNetwork class.\n"
693  << "Outputs* get_outputs_pointer(void) const method.\n"
694  << "Outputs pointer is NULL.\n";
695 
696  throw std::logic_error(buffer.str());
697  }
698 
699  #endif
700 
701  return(outputs_pointer);
702 }
703 
704 
705 // IndependentParameters* get_independent_parameters_pointer(void) const method
706 
708 
710 {
711  #ifndef NDEBUG
712 
714  {
715  std::ostringstream buffer;
716 
717  buffer << "OpenNN Exception: NeuralNetwork class.\n"
718  << "IndependentParameters* get_independent_parameters_pointer(void) const method.\n"
719  << "Independent parameters pointer is NULL.\n";
720 
721  throw std::logic_error(buffer.str());
722  }
723 
724  #endif
725 
727 }
728 
729 
730 // const bool& get_display(void) const method
731 
734 
735 const bool& NeuralNetwork::get_display(void) const
736 {
737  return(display);
738 }
739 
740 
741 // void set(void) method
742 
745 
747 {
748  delete_pointers();
749 
750  set_default();
751 }
752 
753 
754 // void set(const MultilayerPerceptron&) method
755 
759 
760 void NeuralNetwork::set(const MultilayerPerceptron& new_multilayer_perceptron)
761 {
762  delete_pointers();
763 
764  multilayer_perceptron_pointer = new MultilayerPerceptron(new_multilayer_perceptron);
765 
766  const size_t inputs_number = multilayer_perceptron_pointer->get_inputs_number();
767 
768  inputs_pointer = new Inputs(inputs_number);
769 
770  const size_t outputs_number = multilayer_perceptron_pointer->get_outputs_number();
771 
772  outputs_pointer = new Outputs(outputs_number);
773 
774  set_default();
775 }
776 
777 
778 // void set(const Vector<size_t>&) method
779 
783 
784 void NeuralNetwork::set(const Vector<size_t>& new_multilayer_perceptron_architecture)
785 {
786  delete_pointers();
787 
788  multilayer_perceptron_pointer = new MultilayerPerceptron(new_multilayer_perceptron_architecture);
789 
790  const size_t inputs_number = multilayer_perceptron_pointer->get_inputs_number();
791 
792  inputs_pointer = new Inputs(inputs_number);
793 
794  const size_t outputs_number = multilayer_perceptron_pointer->get_outputs_number();
795 
796  outputs_pointer = new Outputs(outputs_number);
797 
798  set_default();
799 }
800 
801 
802 // void set(const size_t&, const size_t&) method
803 
808 
809 void NeuralNetwork::set(const size_t& new_inputs_number, const size_t& new_outputs_number)
810 {
811  delete_pointers();
812 
813  inputs_pointer = new Inputs(new_inputs_number);
814 
815  multilayer_perceptron_pointer = new MultilayerPerceptron(new_inputs_number, new_outputs_number);
816 
817  outputs_pointer = new Outputs(new_outputs_number);
818 
819  set_default();
820 }
821 
822 
823 // void set(const size_t&, const size_t&, const size_t&) method
824 
830 
831 void NeuralNetwork::set(const size_t& new_inputs_number, const size_t& new_hidden_neurons_number, const size_t& new_outputs_number)
832 {
833  delete_pointers();
834 
835  inputs_pointer = new Inputs(new_inputs_number);
836 
837  multilayer_perceptron_pointer = new MultilayerPerceptron(new_inputs_number, new_hidden_neurons_number, new_outputs_number);
838 
839  outputs_pointer = new Outputs(new_outputs_number);
840 
841  set_default();
842 }
843 
844 
845 // void set(const size_t&) method
846 
850 
851 void NeuralNetwork::set(const size_t& new_independent_parameters_number)
852 {
853  delete_pointers();
854 
855  independent_parameters_pointer = new IndependentParameters(new_independent_parameters_number);
856 
857  set_default();
858 }
859 
860 
861 // void set(const std::string&) method
862 
865 
866 void NeuralNetwork::set(const std::string& file_name)
867 {
868  delete_pointers();
869 
870  load(file_name);
871 }
872 
873 
874 // void set(const NeuralNetwork&) method
875 
878 
879 void NeuralNetwork::set(const NeuralNetwork& other_neural_network)
880 {
881  // Pointers
882 
883  delete_pointers();
884 
885  if(other_neural_network.multilayer_perceptron_pointer)
886  {
888  }
889 
890  if(other_neural_network.scaling_layer_pointer)
891  {
892  scaling_layer_pointer = new ScalingLayer(*other_neural_network.scaling_layer_pointer);
893  }
894 
895  if(other_neural_network.unscaling_layer_pointer)
896  {
898  }
899 
900  if(other_neural_network.bounding_layer_pointer)
901  {
902  bounding_layer_pointer = new BoundingLayer(*other_neural_network.bounding_layer_pointer);
903  }
904 
905  if(other_neural_network.probabilistic_layer_pointer)
906  {
908  }
909 
910  if(other_neural_network.conditions_layer_pointer)
911  {
913  }
914 
915  if(other_neural_network.inputs_pointer)
916  {
917  inputs_pointer = new Inputs(*other_neural_network.inputs_pointer);
918  }
919 
920  if(other_neural_network.outputs_pointer)
921  {
922  outputs_pointer = new Outputs(*other_neural_network.outputs_pointer);
923  }
924 
925  if(other_neural_network.independent_parameters_pointer)
926  {
928  }
929 
930  // Other
931 
932  display = other_neural_network.display;
933 }
934 
935 
936 // void set_default(void) method
937 
939 
941 {
942  display = true;
943 }
944 
945 
946 // void set_multilayer_perceptron_pointer(MultilayerPerceptron*) method
947 
951 
953 {
954  if(new_multilayer_perceptron_pointer != multilayer_perceptron_pointer)
955  {
957 
958  multilayer_perceptron_pointer = new_multilayer_perceptron_pointer;
959  }
960 }
961 
962 
963 // void set_scaling_layer_pointer(ScalingLayer*) method
964 
968 
970 {
971  if(new_scaling_layer_pointer != scaling_layer_pointer)
972  {
973  delete scaling_layer_pointer;
974 
975  scaling_layer_pointer = new_scaling_layer_pointer;
976  }
977 }
978 
979 
980 // void set_unscaling_layer_pointer(UnscalingLayer*) method
981 
985 
987 {
988  if(new_unscaling_layer_pointer != unscaling_layer_pointer)
989  {
991 
992  unscaling_layer_pointer = new_unscaling_layer_pointer;
993  }
994 }
995 
996 
997 // void set_bounding_layer_pointer(BoundingLayer*) method
998 
1002 
1004 {
1005  if(new_bounding_layer_pointer != bounding_layer_pointer)
1006  {
1007  delete bounding_layer_pointer;
1008 
1009  bounding_layer_pointer = new_bounding_layer_pointer;
1010  }
1011 }
1012 
1013 
1014 // void set_probabilistic_layer_pointer(ProbabilisticLayer*) method
1015 
1019 
1021 {
1022  if(new_probabilistic_layer_pointer != probabilistic_layer_pointer)
1023  {
1025 
1026  probabilistic_layer_pointer = new_probabilistic_layer_pointer;
1027  }
1028 }
1029 
1030 
1031 // void set_conditions_layer_pointer(ConditionsLayer*) method
1032 
1036 
1038 {
1039  if(new_conditions_layer_pointer != conditions_layer_pointer)
1040  {
1041  delete conditions_layer_pointer;
1042 
1043  conditions_layer_pointer = new_conditions_layer_pointer;
1044  }
1045 }
1046 
1047 
1048 // void set_inputs_pointer(Inputs*) method
1049 
1053 
1055 {
1056  if(new_inputs_pointer != inputs_pointer)
1057  {
1058  delete inputs_pointer;
1059 
1060  inputs_pointer = new_inputs_pointer;
1061  }
1062 }
1063 
1064 
1065 // void set_outputs_pointer(Outputs*) method
1066 
1070 
1072 {
1073  if(new_outputs_pointer != outputs_pointer)
1074  {
1075  delete outputs_pointer;
1076 
1077  outputs_pointer = new_outputs_pointer;
1078  }
1079 }
1080 
1081 
1082 // void set_independent_parameters_pointer(IndependentParameters*) method
1083 
1087 
1089 {
1090  if(new_independent_parameters_pointer != independent_parameters_pointer)
1091  {
1093 
1094  independent_parameters_pointer = new_independent_parameters_pointer;
1095  }
1096 }
1097 
1098 
1099 // size_t get_inputs_number(void) const method
1100 
1102 
1104 {
1105  size_t inputs_number;
1106 
1108  {
1110  }
1112  {
1114  }
1115  else if(conditions_layer_pointer)
1116  {
1118  }
1119  else if(unscaling_layer_pointer)
1120  {
1122  }
1124  {
1126  }
1127  else if(bounding_layer_pointer)
1128  {
1130  }
1131  else
1132  {
1133  inputs_number = 0;
1134  }
1135 
1136  return(inputs_number);
1137 }
1138 
1139 
1140 // size_t get_inputs_number(void) const method
1141 
1143 
1145 {
1146  size_t outputs_number;
1147 
1149  {
1151  }
1152  else if(conditions_layer_pointer)
1153  {
1155  }
1156  else if(unscaling_layer_pointer)
1157  {
1159  }
1161  {
1163  }
1164  else if(bounding_layer_pointer)
1165  {
1167  }
1168  else
1169  {
1170  outputs_number = 0;
1171  }
1172 
1173  return(outputs_number);
1174 }
1175 
1176 
1177 // Vector<size_t> arrange_architecture(void) const
1178 
1189 
1191 {
1192  Vector<size_t> architecture;
1193 
1194  const size_t inputs_number = get_inputs_number();
1195 
1196  if(inputs_number == 0)
1197  {
1198  return(architecture);
1199  }
1200 
1201  architecture.push_back(inputs_number);
1202 
1203  // Scaling layer
1204 
1206  {
1207  architecture.push_back(scaling_layer_pointer->get_scaling_neurons_number());
1208  }
1209 
1210  // Multilayer perceptron
1211 
1213  {
1215  }
1216 
1217  // Conditions
1218 
1220  {
1221  architecture.push_back(conditions_layer_pointer->get_conditions_neurons_number());
1222  }
1223 
1224  // Unscaling layer
1225 
1227  {
1228  architecture.push_back(unscaling_layer_pointer->get_unscaling_neurons_number());
1229  }
1230 
1231  // Probabilistic layer
1232 
1234  {
1236  }
1237 
1238  // Bounding layer
1239 
1241  {
1242  architecture.push_back(bounding_layer_pointer->get_bounding_neurons_number());
1243  }
1244 
1245  return(architecture);
1246 }
1247 
1248 
1249 // size_t count_parameters_number(void) const method
1250 
1253 
1255 {
1256  size_t parameters_number = 0;
1257 
1259  {
1261  }
1262 
1264  {
1265  parameters_number += independent_parameters_pointer->get_parameters_number();
1266  }
1267 
1268  return(parameters_number);
1269 }
1270 
1271 
1272 // Vector<double> arrange_parameters(void) const method
1273 
1276 
1278 {
1279  // Only network parameters
1280 
1282  {
1284  }
1285 
1286  // Only independent parameters
1287 
1289  {
1291  }
1292 
1293  // Both neural and independent parameters
1294 
1296  {
1297  const Vector<double> network_parameters = multilayer_perceptron_pointer->arrange_parameters();
1298 
1299  const Vector<double> scaled_independent_parameters = independent_parameters_pointer->calculate_scaled_parameters();
1300 
1301  return(network_parameters.assemble(scaled_independent_parameters));
1302  }
1303 
1304  // None neural neither independent parameters
1305 
1306  else
1307  {
1308  const Vector<double> parameters;
1309 
1310  return(parameters);
1311  }
1312 }
1313 
1314 
1315 // void set_parameters(const Vector<double>&) method
1316 
1319 
1321 {
1322  // Control sentence (if debug)
1323 
1324  #ifndef NDEBUG
1325 
1326  const size_t size = new_parameters.size();
1327 
1328  const size_t parameters_number = count_parameters_number();
1329 
1330  if(size != parameters_number)
1331  {
1332  std::ostringstream buffer;
1333 
1334  buffer << "OpenNN Exception: NeuralNetwork class.\n"
1335  << "void set_parameters(const Vector<double>&) method.\n"
1336  << "Size must be equal to number of parameters.\n";
1337 
1338  throw std::logic_error(buffer.str());
1339  }
1340 
1341  #endif
1342 
1344  {// Only network parameters
1345 
1347  }
1349  {// Only independent parameters
1350 
1352  }
1354  {// Both network and independent parameters
1355 
1356  // Multilayer perceptron parameters
1357 
1358  const size_t neural_parameters_number = multilayer_perceptron_pointer->count_parameters_number();
1359  const size_t independent_parameters_number = independent_parameters_pointer->get_parameters_number();
1360 
1361  const Vector<double> network_parameters = new_parameters.take_out(0, neural_parameters_number);
1362 
1363  multilayer_perceptron_pointer->set_parameters(network_parameters);
1364 
1365  // Independent parameters
1366 
1367  const Vector<double> scaled_independent_parameters = new_parameters.take_out(neural_parameters_number, independent_parameters_number);
1368 
1369  independent_parameters_pointer->unscale_parameters(scaled_independent_parameters);
1370  }
1371  else
1372  {// None neural neither independent parameters
1373 
1374  return;
1375  }
1376 }
1377 
1378 
1379 // void delete_pointers(void) method
1380 
1393 
1395 {
1397  delete scaling_layer_pointer;
1398  delete unscaling_layer_pointer;
1399  delete bounding_layer_pointer;
1401  delete conditions_layer_pointer;
1402  delete inputs_pointer;
1403  delete outputs_pointer;
1405 
1407  scaling_layer_pointer = NULL;
1408  unscaling_layer_pointer = NULL;
1409  bounding_layer_pointer = NULL;
1411  conditions_layer_pointer = NULL;
1412  inputs_pointer = NULL;
1413  outputs_pointer = NULL;
1415 }
1416 
1417 
1418 // void construct_multilayer_perceptron(void) method
1419 
1421 
1423 {
1425  {
1427  }
1428 }
1429 
1430 
1431 // void construct_scaling_layer(void) method
1432 
1435 
1437 {
1439  {
1440  size_t inputs_number = 0;
1441 
1443  {
1445  }
1446 
1447  scaling_layer_pointer = new ScalingLayer(inputs_number);
1448  }
1449 }
1450 
1451 
1452 // void construct_unscaling_layer(void) method
1453 
1456 
1458 {
1460  {
1461  size_t outputs_number = 0;
1462 
1464  {
1466  }
1467 
1468  unscaling_layer_pointer = new UnscalingLayer(outputs_number);
1469  }
1470 }
1471 
1472 
1473 // void construct_bounding_layer(void) method
1474 
1477 
1479 {
1481  {
1482  size_t outputs_number = 0;
1483 
1485  {
1487  }
1488 
1489  bounding_layer_pointer = new BoundingLayer(outputs_number);
1490  }
1491 }
1492 
1493 
1494 // void construct_probabilistic_layer(void) method
1495 
1498 
1500 {
1502  {
1503  size_t outputs_number = 0;
1504 
1506  {
1508  }
1509 
1510  probabilistic_layer_pointer = new ProbabilisticLayer(outputs_number);
1511  }
1512 }
1513 
1514 
1515 // void construct_conditions_layer(void) method
1516 
1520 
1522 {
1524  {
1525  size_t inputs_number = 0;
1526  size_t outputs_number = 0;
1527 
1529  {
1532  }
1533 
1534  conditions_layer_pointer = new ConditionsLayer(inputs_number, outputs_number);
1535  }
1536 }
1537 
1538 
1539 // void construct_inputs(void) method
1540 
1543 
1545 {
1546  if(!inputs_pointer)
1547  {
1548  size_t inputs_number = 0;
1549 
1551  {
1553  }
1554 
1555  inputs_pointer = new Inputs(inputs_number);
1556  }
1557 }
1558 
1559 
1560 // void construct_outputs(void) method
1561 
1564 
1566 {
1567  if(!outputs_pointer)
1568  {
1569  size_t outputs_number = 0;
1570 
1572  {
1574  }
1575 
1576  outputs_pointer = new Outputs(outputs_number);
1577  }
1578 }
1579 
1580 
1581 // void construct_independent_parameters(void) method
1582 
1585 
1587 {
1589  {
1591  }
1592 }
1593 
1594 
1595 // void destruct_multilayer_perceptron(void) method
1596 
1598 
1600 {
1602 
1604 }
1605 
1606 
1607 // void destruct_scaling_layer(void) method
1608 
1610 
1612 {
1613  delete scaling_layer_pointer;
1614 
1615  scaling_layer_pointer = NULL;
1616 }
1617 
1618 
1619 // void destruct_unscaling_layer(void) method
1620 
1622 
1624 {
1625  delete unscaling_layer_pointer;
1626 
1627  unscaling_layer_pointer = NULL;
1628 }
1629 
1630 
1631 // void destruct_bounding_layer(void) method
1632 
1634 
1636 {
1637  delete bounding_layer_pointer;
1638 
1639  bounding_layer_pointer = NULL;
1640 }
1641 
1642 
1643 // void destruct_probabilistic_layer(void) method
1644 
1646 
1648 {
1650 
1652 }
1653 
1654 
1655 // void destruct_conditions_layer(void) method
1656 
1658 
1660 {
1661  delete conditions_layer_pointer;
1662 
1663  conditions_layer_pointer = NULL;
1664 }
1665 
1666 
1667 // void destruct_inputs(void) method
1668 
1670 
1672 {
1673  delete inputs_pointer;
1674 
1675  inputs_pointer = NULL;
1676 }
1677 
1678 
1679 // void destruct_outputs(void) method
1680 
1682 
1684 {
1685  delete outputs_pointer;
1686 
1687  outputs_pointer = NULL;
1688 }
1689 
1690 
1691 // void destruct_independent_parameters(void) method
1692 
1694 
1696 {
1698 
1700 }
1701 
1702 
1703 // void initialize_random(void) method
1704 
1707 
1709 {
1710  size_t inputs_number;
1711  size_t outputs_number;
1712 
1713  // Multilayer perceptron
1714 
1715  if(rand()%5)
1716  {
1718  {
1720  }
1721 
1723 
1726  }
1727  else
1728  {
1729  inputs_number = rand()%10 + 1;
1730  outputs_number = rand()%10 + 1;
1731  }
1732 
1733  // Scaling layer
1734 
1735  if(rand()%5)
1736  {
1738  {
1739  scaling_layer_pointer = new ScalingLayer(inputs_number);
1740  }
1741 
1743  }
1744 
1745  // Unscaling layer
1746 
1747  if(rand()%5)
1748  {
1750  {
1751  unscaling_layer_pointer = new UnscalingLayer(outputs_number);
1752  }
1753 
1755  }
1756 
1757  // Bounding layer
1758 
1759  if(rand()%5)
1760  {
1762  {
1763  bounding_layer_pointer = new BoundingLayer(outputs_number);
1764  }
1765 
1767  }
1768 
1769  // Probabilistic layer
1770 
1771  if(rand()%5)
1772  {
1774  {
1775  probabilistic_layer_pointer = new ProbabilisticLayer(outputs_number);
1776  }
1777 
1779  }
1780 
1781  // Conditions layer
1782 
1783  if(rand()%5)
1784  {
1786  {
1787  conditions_layer_pointer = new ConditionsLayer(inputs_number, outputs_number);
1788  }
1789 
1791  }
1792 
1793  // Inputs
1794 
1795  if(rand()%5)
1796  {
1797  if(!inputs_pointer)
1798  {
1799  inputs_pointer = new Inputs(inputs_number);
1800  }
1801  }
1802 
1803  // Outputs
1804 
1805  if(rand()%5)
1806  {
1807  if(!outputs_pointer)
1808  {
1809  outputs_pointer = new Outputs(outputs_number);
1810  }
1811  }
1812 
1813  // Independent parameters
1814 
1815  if(rand()%5)
1816  {
1818  {
1820  }
1821 
1823  }
1824 
1825 }
1826 
1827 
1828 // void set_display(const bool&) method
1829 
1834 
1835 void NeuralNetwork::set_display(const bool& new_display)
1836 {
1837  display = new_display;
1838 }
1839 
1840 
1841 // void prune_input(const size_t&) method
1842 
1847 
1848 void NeuralNetwork::prune_input(const size_t& index)
1849 {
1851  {
1852  std::ostringstream buffer;
1853 
1854  buffer << "OpenNN Exception: NeuralNetwork class.\n"
1855  << "void prune_input(const size_t&) method.\n"
1856  << "Pointer to multilayer perceptron is NULL.\n";
1857 
1858  throw std::logic_error(buffer.str());
1859  }
1860 
1862 
1864  {
1866  }
1867 
1869  {
1870  }
1871 
1872  if(inputs_pointer)
1873  {
1874  inputs_pointer->prune_input(index);
1875  }
1876 }
1877 
1878 
1879 // void prune_output(const size_t&) method
1880 
1885 
1886 void NeuralNetwork::prune_output(const size_t& index)
1887 {
1889  {
1890  std::ostringstream buffer;
1891 
1892  buffer << "OpenNN Exception: NeuralNetwork class.\n"
1893  << "void prune_output(const size_t&) method.\n"
1894  << "Pointer to multilayer perceptron is NULL.\n";
1895 
1896  throw std::logic_error(buffer.str());
1897  }
1898 
1900 
1902  {
1904  }
1905 
1907  {
1909  }
1910 
1912  {
1914  }
1915 
1917  {
1918  }
1919 
1920  if(outputs_pointer)
1921  {
1922  outputs_pointer->prune_output(index);
1923  }
1924 }
1925 
1926 
1927 // void resize_inputs_number(const size_t&) method
1928 
1930 
1932 {
1933 
1934 }
1935 
1936 
1937 // void resize_outputs_number(const size_t&) method
1938 
1940 
1942 {
1943 
1944 }
1945 
1946 
1947 // size_t get_layers_number(void) method
1948 
1951 
1953 {
1954  size_t layers_number = 0;
1955 
1957  {
1959  }
1960 
1962  {
1963  layers_number += 1;
1964  }
1965 
1967  {
1968  layers_number += 1;
1969  }
1970 
1972  {
1973  layers_number += 1;
1974  }
1975 
1977  {
1978  layers_number += 1;
1979  }
1980 
1982  {
1983  layers_number += 1;
1984  }
1985 
1986  return(layers_number);
1987 }
1988 
1989 
1990 // void initialize_parameters(const double&) method
1991 
1993 
1994 void NeuralNetwork::initialize_parameters(const double& value)
1995 {
1997  {
1999  }
2000 
2002  {
2004  }
2005 }
2006 
2007 
2008 // void randomize_parameters_uniform(void) method
2009 
2012 
2014 {
2016  {
2018  }
2019 
2021  {
2023  }
2024 }
2025 
2026 
2027 // void randomize_parameters_uniform(const double&, const double&) method
2028 
2033 
2034 void NeuralNetwork::randomize_parameters_uniform(const double& minimum, const double& maximum)
2035 {
2037  {
2039  }
2040 
2042  {
2044  }
2045 }
2046 
2047 
2048 // void randomize_parameters_uniform(const Vector<double>&, const Vector<double>&) method
2049 
2055 
2057 {
2059  {
2061  }
2062 
2064  {
2066  }
2067 }
2068 
2069 
2070 // void randomize_parameters_uniform(const Vector< Vector<double> >&) method
2071 
2078 
2080 {
2082  {
2084  }
2085 
2087  {
2089  }
2090 }
2091 
2092 
2093 // void randomize_parameters_normal(void) method
2094 
2097 
2099 {
2101  {
2103  }
2104 
2106  {
2108  }
2109 }
2110 
2111 
2112 // void randomize_parameters_normal(const double&, const double&) method
2113 
2119 
2120 void NeuralNetwork::randomize_parameters_normal(const double& mean, const double& standard_deviation)
2121 {
2123  {
2124  multilayer_perceptron_pointer->randomize_parameters_normal(mean, standard_deviation);
2125  }
2126 
2128  {
2129  independent_parameters_pointer->randomize_parameters_normal(mean, standard_deviation);
2130  }
2131 }
2132 
2133 
2134 // void randomize_parameters_normal(const Vector<double>&, const Vector<double>&) method
2135 
2141 
2143 {
2145  {
2146  multilayer_perceptron_pointer->randomize_parameters_normal(mean, standard_deviation);
2147  }
2148 
2150  {
2151  independent_parameters_pointer->randomize_parameters_normal(mean, standard_deviation);
2152  }
2153 }
2154 
2155 
2156 // void randomize_parameters_normal(const Vector< Vector<double> >&) method
2157 
2165 
2167 {
2169  {
2171  }
2172 
2174  {
2176  }
2177 }
2178 
2179 
2180 // double calculate_parameters_norm(void) const method
2181 
2183 
2185 {
2186  const Vector<double> parameters = arrange_parameters();
2187 
2188  const double parameters_norm = parameters.calculate_norm();
2189 
2190  return(parameters_norm);
2191 }
2192 
2193 
2194 // Statistics<double> calculate_parameters_statistics(void) const method
2195 
2198 
2200 {
2201  const Vector<double> parameters = arrange_parameters();
2202 
2203  return(parameters.calculate_statistics());
2204 }
2205 
2206 
2207 // Histogram calculate_parameters_histogram(const size_t& = 10) const method
2208 
2212 
2214 {
2215  const Vector<double> parameters = arrange_parameters();
2216 
2217  return(parameters.calculate_histogram(bins_number));
2218 }
2219 
2220 
2221 // Vector<double> calculate_outputs(const Vector<double>&) method method
2222 
2234 
2236 {
2237  // Control sentence (if debug)
2238 
2239  #ifndef NDEBUG
2240 
2242  {
2243  const size_t inputs_size = inputs.size();
2244 
2245  const size_t inputs_number = multilayer_perceptron_pointer->get_inputs_number();
2246 
2247  if(inputs_size != inputs_number)
2248  {
2249  std::ostringstream buffer;
2250 
2251  buffer << "OpenNN Exception: NeuralNetwork class.\n"
2252  << "Vector<double> calculate_outputs(const Vector<double>&) const method.\n"
2253  << "Size of inputs must be equal to number of inputs.\n";
2254 
2255  throw std::logic_error(buffer.str());
2256  }
2257  }
2258 
2259  #endif
2260 
2261  Vector<double> outputs(inputs);
2262 
2263  // Scaling layer
2264 
2266  {
2267  outputs = scaling_layer_pointer->calculate_outputs(inputs);
2268  }
2269 
2270  // Multilayer perceptron
2271 
2273  {
2275  }
2276 
2277  // Conditions
2278 
2280  {
2281  outputs = conditions_layer_pointer->calculate_outputs(inputs, outputs);
2282  }
2283 
2284  // Unscaling layer
2285 
2287  {
2288  outputs = unscaling_layer_pointer->calculate_outputs(outputs);
2289  }
2290 
2291  // Probabilistic layer
2292 
2294  {
2295  outputs = probabilistic_layer_pointer->calculate_outputs(outputs);
2296  }
2297 
2298  // Bounding layer
2299 
2301  {
2302  outputs = bounding_layer_pointer->calculate_outputs(outputs);
2303  }
2304 
2305  return(outputs);
2306 }
2307 
2308 
2309 // Matrix<double> calculate_directional_input_data(const size_t&, const Vector<double>&, const double&, const double&, const size_t& = 101) const
2310 
2317 
2319  const Vector<double>& point,
2320  const double& minimum,
2321  const double& maximum,
2322  const size_t& points_number) const
2323 {
2324  const size_t inputs_number = inputs_pointer->get_inputs_number();
2325 
2326  Matrix<double> directional_input_data(points_number, inputs_number);
2327 
2328  Vector<double> inputs(inputs_number);
2329 
2330  inputs = point;
2331 
2332  for(size_t i = 0; i < points_number; i++)
2333  {
2334  inputs[direction] = minimum + (maximum-minimum)*i/(double)(points_number-1);
2335 
2336  directional_input_data.set_row(i, inputs);
2337  }
2338 
2339  return(directional_input_data);
2340 }
2341 
2342 
2343 // Vector<double> calculate_outputs(const Vector<double>&, const Vector<double>&) const method
2344 
2348 
2350 {
2351  // Control sentence (if debug)
2352 
2353  #ifndef NDEBUG
2354 
2355  const size_t inputs_size = inputs.size();
2356 
2357  const size_t inputs_number = multilayer_perceptron_pointer->get_inputs_number();
2358 
2359  if(inputs_size != inputs_number)
2360  {
2361  std::ostringstream buffer;
2362 
2363  buffer << "OpenNN Exception: NeuralNetwork class.\n"
2364  << "Vector<double> calculate_outputs(const Vector<double>&, const Vector<double>&) const method.\n"
2365  << "Size of inputs (" << inputs_size << ") must be equal to number of inputs (" << inputs_number << ").\n";
2366 
2367  throw std::logic_error(buffer.str());
2368  }
2369 
2370  const size_t parameters_size = parameters.size();
2371 
2372  const size_t parameters_number = count_parameters_number();
2373 
2374  if(parameters_size != parameters_number)
2375  {
2376  std::ostringstream buffer;
2377 
2378  buffer << "OpenNN Exception: NeuralNetwork class.\n"
2379  << "Vector<double> calculate_outputs(const Vector<double>&, const Vector<double>&) const method.\n"
2380  << "Size of potential parameters (" << parameters_size << ") must be equal to number of parameters (" << parameters_number << ").\n";
2381 
2382  throw std::logic_error(buffer.str());
2383  }
2384 
2385  #endif
2386 
2387  Vector<double> outputs(inputs);
2388 
2389  // Scaling layer
2390 
2392  {
2393  outputs = scaling_layer_pointer->calculate_outputs(inputs);
2394  }
2395 
2396  // Multilayer perceptron
2397 
2399  {
2400  outputs = multilayer_perceptron_pointer->calculate_outputs(outputs, parameters);
2401  }
2402 
2403  // Conditions
2404 
2406  {
2407  outputs = conditions_layer_pointer->calculate_outputs(inputs, outputs);
2408  }
2409 
2410  // Unscaling layer
2411 
2413  {
2414  outputs = unscaling_layer_pointer->calculate_outputs(outputs);
2415  }
2416 
2417  // Probabilistic layer
2418 
2420  {
2421  outputs = probabilistic_layer_pointer->calculate_outputs(outputs);
2422  }
2423 
2424  // Bounding layer
2425 
2427  {
2428  outputs = bounding_layer_pointer->calculate_outputs(outputs);
2429  }
2430 
2431  return(outputs);
2432 }
2433 
2434 
2435 // Matrix<double> calculate_output_data(const Matrix<double>&) const method
2436 
2440 
2442 {
2443  const size_t inputs_number = multilayer_perceptron_pointer->get_inputs_number();
2444  const size_t outputs_number = multilayer_perceptron_pointer->get_outputs_number();
2445 
2446  // Control sentence (if debug)
2447 
2448  #ifndef NDEBUG
2449 
2450  const size_t columns_number = input_data.get_columns_number();
2451 
2452  if(columns_number != inputs_number)
2453  {
2454  std::ostringstream buffer;
2455 
2456  buffer << "OpenNN Exception: NeuralNetwork class.\n"
2457  << "Matrix<double> calculate_output_data(const Matrix<double>&) const method.\n"
2458  << "Number of columns must be equal to number of inputs.\n";
2459 
2460  throw std::logic_error(buffer.str());
2461  }
2462 
2463  #endif
2464 
2465  const size_t input_vectors_number = input_data.get_rows_number();
2466 
2467  Matrix<double> output_data(input_vectors_number, outputs_number);
2468 
2469  Vector<double> inputs(inputs_number);
2470  Vector<double> outputs(outputs_number);
2471 
2472  for(size_t i = 0; i < input_vectors_number; i++)
2473  {
2474  inputs = input_data.arrange_row(i);
2475  outputs = calculate_outputs(inputs);
2476  output_data.set_row(i, outputs);
2477  }
2478 
2479  return(output_data);
2480 }
2481 
2482 
2483 // Matrix<double> calculate_Jacobian(const Vector<double>&) const method
2484 
2488 
2490 {
2491  #ifndef NDEBUG
2492 
2493  const size_t size = inputs.size();
2494 
2495  const size_t inputs_number = multilayer_perceptron_pointer->get_inputs_number();
2496 
2497  if(size != inputs_number)
2498  {
2499  std::ostringstream buffer;
2500 
2501  buffer << "OpenNN Exception: NeuralNetwork class.\n"
2502  << "void calculate_Jacobian(const Vector<double>&) const method.\n"
2503  << "Size must be equal to number of inputs.\n";
2504 
2505  throw std::logic_error(buffer.str());
2506  }
2507 
2508  #endif
2509 
2510  Vector<double> outputs(inputs);
2511 
2512  Matrix<double> scaling_layer_Jacobian;
2513  Matrix<double> unscaling_layer_Jacobian;
2514  Matrix<double> multilayer_perceptron_Jacobian;
2515  Matrix<double> bounding_layer_Jacobian;
2516  Matrix<double> conditions_layer_Jacobian;
2517  Matrix<double> probabilistic_layer_Jacobian;
2518 
2519  // Scaling layer
2520 
2522  {
2523  const Vector<double> scaling_layer_derivative = scaling_layer_pointer->calculate_derivatives(outputs);
2524  scaling_layer_Jacobian = scaling_layer_pointer->arrange_Jacobian(scaling_layer_derivative);
2525 
2526  outputs = scaling_layer_pointer->calculate_outputs(outputs);
2527  }
2528 
2529  // Multilayer perceptron
2530 
2532  {
2533  multilayer_perceptron_Jacobian = multilayer_perceptron_pointer->calculate_Jacobian(outputs);
2534 
2536  }
2537 
2538  // Unscaling layer
2539 
2541  {
2542  const Vector<double> unscaling_layer_derivative = unscaling_layer_pointer->calculate_derivatives(outputs);
2543  unscaling_layer_Jacobian = unscaling_layer_pointer->arrange_Jacobian(unscaling_layer_derivative);
2544 
2545  outputs = unscaling_layer_pointer->calculate_outputs(outputs);
2546  }
2547 
2548  // Probabilistic layer
2549 
2551  {
2552  probabilistic_layer_Jacobian = probabilistic_layer_pointer->calculate_Jacobian(outputs);
2553 
2554  outputs = probabilistic_layer_pointer->calculate_outputs(outputs);
2555  }
2556 
2557  // Bounding layer
2558 
2560  {
2561  const Vector<double>& derivatives = bounding_layer_pointer->calculate_derivative(outputs);
2562 
2563  bounding_layer_Jacobian = bounding_layer_pointer->arrange_Jacobian(derivatives);
2564 
2565  outputs = bounding_layer_pointer->calculate_outputs(outputs);
2566  }
2567 
2568  const size_t outputs_number = multilayer_perceptron_pointer->get_outputs_number();
2569 
2570  Matrix<double> Jacobian(outputs_number, outputs_number, 0.0);
2571  Jacobian.set_diagonal(1.0);
2572 
2573  // Bounding layer
2574 
2576  {
2577  Jacobian = Jacobian.dot(bounding_layer_Jacobian);
2578  }
2579 
2580  // Probabilistic outputs
2581 
2583  {
2584  Jacobian = Jacobian.dot(probabilistic_layer_Jacobian);
2585  }
2586 
2587  // Unscaling layer
2588 
2590  {
2591  Jacobian = Jacobian.dot(unscaling_layer_Jacobian);
2592  }
2593 
2594  // Multilayer perceptron
2595 
2597  {
2598  Jacobian = Jacobian.dot(multilayer_perceptron_Jacobian);
2599  }
2600 
2601  // Scaling layer
2602 
2604  {
2605  Jacobian = Jacobian.dot(scaling_layer_Jacobian);
2606  }
2607 
2608  // Conditions
2609 
2611  {
2612  conditions_layer_Jacobian = conditions_layer_pointer->calculate_Jacobian(inputs, outputs, Jacobian);
2613 
2614  outputs = conditions_layer_pointer->calculate_outputs(inputs, outputs);
2615  }
2616 
2618  {
2619  Jacobian = Jacobian.dot(conditions_layer_Jacobian);
2620  }
2621 
2622  return(Jacobian);
2623 }
2624 
2625 
2626 // Vector< Matrix<double> > calculate_Jacobian_data(const Matrix<double>&) const method
2627 
2631 
2633 {
2634  const size_t inputs_number = inputs_pointer->get_inputs_number();
2635 
2636  const size_t input_data_size = input_data.get_rows_number();
2637 
2638  Vector< Matrix<double> > Jacobian_data(input_data_size);
2639 
2640  Vector<double> input_values(inputs_number);
2641 
2642  for(size_t i = 0; i < input_data_size; i++)
2643  {
2644  input_values = input_data.arrange_row(i);
2645 
2646  Jacobian_data[i] = calculate_Jacobian(input_values);
2647  }
2648 
2649  return(Jacobian_data);
2650 }
2651 
2652 
2653 // Matrix<double> calculate_Jacobian(const Vector<double>&, const Vector<double>&) const method
2654 
2657 
2659 {
2660  return(multilayer_perceptron_pointer->calculate_Jacobian(inputs, parameters));
2661 }
2662 
2663 
2664 // Vector< Matrix<double> > calculate_Hessian_form(const Vector<double>&) const method
2665 
2668 
2670 {
2672 }
2673 
2674 
2675 // Vector< Matrix<double> > calculate_Hessian_form(const Vector<double>&, const Vector<double>&) const method
2676 
2679 
2681 {
2682  return(multilayer_perceptron_pointer->calculate_Hessian_form(inputs, parameters));
2683 }
2684 
2685 
2686 // std::string to_string(void) const method
2687 
2689 
2690 std::string NeuralNetwork::to_string(void) const
2691 {
2692  std::ostringstream buffer;
2693 
2694  buffer << "NeuralNetwork\n";
2695 
2696  // Multilayer perceptron
2697 
2699  {
2701  }
2702 
2703  // Scaling layer
2704 
2706  {
2707  buffer << scaling_layer_pointer->to_string();
2708  }
2709 
2710  // Unscaling layer
2711 
2713  {
2714  buffer << unscaling_layer_pointer->to_string();
2715  }
2716 
2717  // Bounding layer
2718 
2720  {
2721  buffer << bounding_layer_pointer->to_string();
2722  }
2723 
2724  // Probabilistic layer
2725 
2727  {
2729  }
2730 
2731  // Conditions layer
2732 
2734  {
2735  buffer << conditions_layer_pointer->to_string();
2736  }
2737 
2738  // Inputs
2739 
2740  if(inputs_pointer)
2741  {
2742  buffer << inputs_pointer->to_string();
2743  }
2744 
2745  // Outputs
2746 
2747  if(outputs_pointer)
2748  {
2749  buffer << outputs_pointer->to_string();
2750  }
2751 
2752  // Independent parameters
2753 
2755  {
2757  }
2758 
2759  buffer << "Display: " << display << "\n";
2760 
2761  return(buffer.str());
2762 }
2763 
2764 
2765 // tinyxml2::XMLDocument* to_XML(void) const method
2766 
2769 
2770 tinyxml2::XMLDocument* NeuralNetwork::to_XML(void) const
2771 {
2772  tinyxml2::XMLDocument* document = new tinyxml2::XMLDocument;
2773 
2774  tinyxml2::XMLElement* neural_network_element = document->NewElement("NeuralNetwork");
2775 
2776  document->InsertFirstChild(neural_network_element);
2777 
2778  std::ostringstream buffer;
2779 
2780  // Inputs
2781 
2782  if(inputs_pointer)
2783  {
2784  tinyxml2::XMLElement* element = document->NewElement("Inputs");
2785  neural_network_element->LinkEndChild(element);
2786 
2787  const tinyxml2::XMLDocument* inputs_document = inputs_pointer->to_XML();
2788 
2789  const tinyxml2::XMLElement* inputsElement = inputs_document->FirstChildElement("Inputs");
2790 
2791  DeepClone(element, inputsElement, document, NULL);
2792 
2793  delete inputs_document;
2794  }
2795 
2796  // Scaling layer
2797 
2799  {
2800  tinyxml2::XMLElement* element = document->NewElement("ScalingLayer");
2801  neural_network_element->LinkEndChild(element);
2802 
2803  const tinyxml2::XMLDocument* scaling_layer_document = scaling_layer_pointer->to_XML();
2804 
2805  const tinyxml2::XMLElement* scaling_layer_element = scaling_layer_document->FirstChildElement("ScalingLayer");
2806 
2807  DeepClone(element, scaling_layer_element, document, NULL);
2808 
2809  delete scaling_layer_document;
2810  }
2811 
2812  // Multilayer perceptron
2813 
2815  {
2816  tinyxml2::XMLElement* element = document->NewElement("MultilayerPerceptron");
2817  neural_network_element->LinkEndChild(element);
2818 
2819  const tinyxml2::XMLDocument* multilayer_perceptron_document = multilayer_perceptron_pointer->to_XML();
2820 
2821  const tinyxml2::XMLElement* multilayer_perceptron_element = multilayer_perceptron_document->FirstChildElement("MultilayerPerceptron");
2822 
2823  DeepClone(element, multilayer_perceptron_element, document, NULL);
2824 
2825  delete multilayer_perceptron_document;
2826  }
2827 
2828  // Unscaling layer
2829 
2831  {
2832  tinyxml2::XMLElement* element = document->NewElement("UnscalingLayer");
2833  neural_network_element->LinkEndChild(element);
2834 
2835  const tinyxml2::XMLDocument* unscaling_layer_document = unscaling_layer_pointer->to_XML();
2836 
2837  const tinyxml2::XMLElement* unscaling_layer_element = unscaling_layer_document->FirstChildElement("UnscalingLayer");
2838 
2839  DeepClone(element, unscaling_layer_element, document, NULL);
2840 
2841  delete unscaling_layer_document;
2842  }
2843 
2844  // Probabilistic layer
2845 
2847  {
2848  tinyxml2::XMLElement* element = document->NewElement("ProbabilisticLayer");
2849  neural_network_element->LinkEndChild(element);
2850 
2851  const tinyxml2::XMLDocument* probabilistic_layer_document = probabilistic_layer_pointer->to_XML();
2852 
2853  const tinyxml2::XMLElement* probabilistic_layer_element = probabilistic_layer_document->FirstChildElement("ProbabilisticLayer");
2854 
2855  DeepClone(element, probabilistic_layer_element, document, NULL);
2856 
2857  delete probabilistic_layer_document;
2858  }
2859 
2860  // Bounding layer
2861 
2863  {
2864  tinyxml2::XMLElement* element = document->NewElement("BoundingLayer");
2865  neural_network_element->LinkEndChild(element);
2866 
2867  const tinyxml2::XMLDocument* bounding_layer_document = bounding_layer_pointer->to_XML();
2868 
2869  const tinyxml2::XMLElement* bounding_layer_element = bounding_layer_document->FirstChildElement("BoundingLayer");
2870 
2871  DeepClone(element, bounding_layer_element, document, NULL);
2872 
2873  delete bounding_layer_document;
2874  }
2875 
2876  // Conditions layer
2877 
2879  {
2880  tinyxml2::XMLElement* element = document->NewElement("ConditionsLayer");
2881  neural_network_element->LinkEndChild(element);
2882 
2883  const tinyxml2::XMLDocument* conditions_layer_document = conditions_layer_pointer->to_XML();
2884 
2885  const tinyxml2::XMLElement* conditions_layer_element = conditions_layer_document->FirstChildElement("ConditionsLayer");
2886 
2887  DeepClone(element, conditions_layer_element, document, NULL);
2888 
2889  delete conditions_layer_document;
2890  }
2891 
2892  // Outputs
2893 
2894  if(outputs_pointer)
2895  {
2896  tinyxml2::XMLElement* element = document->NewElement("Outputs");
2897  neural_network_element->LinkEndChild(element);
2898 
2899  const tinyxml2::XMLDocument* outputs_document = outputs_pointer->to_XML();
2900 
2901  const tinyxml2::XMLElement* outputs_element = outputs_document->FirstChildElement("Outputs");
2902 
2903  DeepClone(element, outputs_element, document, NULL);
2904 
2905  delete outputs_document;
2906  }
2907 
2908  // Independent parameters
2909 
2911  {
2912  tinyxml2::XMLElement* element = document->NewElement("IndependentParameters");
2913  neural_network_element->LinkEndChild(element);
2914 
2915  const tinyxml2::XMLDocument* independent_parameters_document = independent_parameters_pointer->to_XML();
2916 
2917  const tinyxml2::XMLElement* independent_parameters_element = independent_parameters_document->FirstChildElement("IndependentParameters");
2918 
2919  DeepClone(element, independent_parameters_element, document, NULL);
2920 
2921  delete independent_parameters_document;
2922  }
2923 
2924  // Display warnings
2925  {
2926  tinyxml2::XMLElement* display_element = document->NewElement("Display");
2927  neural_network_element->LinkEndChild(display_element);
2928 
2929  buffer.str("");
2930  buffer << display;
2931 
2932  tinyxml2::XMLText* display_text = document->NewText(buffer.str().c_str());
2933  display_element->LinkEndChild(display_text);
2934  }
2935 
2936  return(document);
2937 }
2938 
2939 
2940 // void from_XML(const tinyxml2::XMLDocument&) method
2941 
2944 
2945 void NeuralNetwork::from_XML(const tinyxml2::XMLDocument& document)
2946 {
2947  std::ostringstream buffer;
2948 
2949  const tinyxml2::XMLElement* root_element = document.FirstChildElement("NeuralNetwork");
2950 
2951  if(!root_element)
2952  {
2953  buffer << "OpenNN Exception: NeuralNetwork class.\n"
2954  << "void from_XML(const tinyxml2::XMLDocument&) method.\n"
2955  << "Neural network element is NULL.\n";
2956 
2957  throw std::logic_error(buffer.str());
2958  }
2959 
2960  // Inputs
2961  {
2962  const tinyxml2::XMLElement* element = root_element->FirstChildElement("Inputs");
2963 
2964  if(element)
2965  {
2966  if(!inputs_pointer)
2967  {
2968  inputs_pointer = new Inputs();
2969  }
2970 
2971  tinyxml2::XMLDocument inputs_document;
2972 
2973  tinyxml2::XMLElement* element_clone = inputs_document.NewElement("Inputs");
2974  inputs_document.InsertFirstChild(element_clone);
2975 
2976  DeepClone(element_clone, element, &inputs_document, NULL);
2977 
2978  inputs_pointer->from_XML(inputs_document);
2979  }
2980  }
2981 
2982  // Outputs
2983  {
2984  const tinyxml2::XMLElement* element = root_element->FirstChildElement("Outputs");
2985 
2986  if(element)
2987  {
2988  if(!outputs_pointer)
2989  {
2990  outputs_pointer = new Outputs();
2991  }
2992 
2993  tinyxml2::XMLDocument outputs_document;
2994 
2995  tinyxml2::XMLElement* element_clone = outputs_document.NewElement("Outputs");
2996  outputs_document.InsertFirstChild(element_clone);
2997 
2998  DeepClone(element_clone, element, &outputs_document, NULL);
2999 
3000  outputs_pointer->from_XML(outputs_document);
3001  }
3002  }
3003 
3004  // Multilayer perceptron
3005  {
3006  const tinyxml2::XMLElement* element = root_element->FirstChildElement("MultilayerPerceptron");
3007 
3008  if(element)
3009  {
3011  {
3013  }
3014 
3015  tinyxml2::XMLDocument multilayer_perceptron_document;
3016 
3017  tinyxml2::XMLElement* element_clone = multilayer_perceptron_document.NewElement("MultilayerPerceptron");
3018  multilayer_perceptron_document.InsertFirstChild(element_clone);
3019 
3020  DeepClone(element_clone, element, &multilayer_perceptron_document, NULL);
3021 
3022  multilayer_perceptron_pointer->from_XML(multilayer_perceptron_document);
3023  }
3024  }
3025 
3026  // Scaling layer
3027  {
3028  const tinyxml2::XMLElement* element = root_element->FirstChildElement("ScalingLayer");
3029 
3030  if(element)
3031  {
3033  {
3035  }
3036 
3037  tinyxml2::XMLDocument scaling_layer_document;
3038 
3039  tinyxml2::XMLElement* element_clone = scaling_layer_document.NewElement("ScalingLayer");
3040  scaling_layer_document.InsertFirstChild(element_clone);
3041 
3042  DeepClone(element_clone, element, &scaling_layer_document, NULL);
3043 
3044  scaling_layer_pointer->from_XML(scaling_layer_document);
3045  }
3046  }
3047 
3048  // Unscaling layer
3049  {
3050  const tinyxml2::XMLElement* element = root_element->FirstChildElement("UnscalingLayer");
3051 
3052  if(element)
3053  {
3055  {
3057  }
3058 
3059  tinyxml2::XMLDocument unscaling_layer_document;
3060 
3061  tinyxml2::XMLElement* element_clone = unscaling_layer_document.NewElement("UnscalingLayer");
3062  unscaling_layer_document.InsertFirstChild(element_clone);
3063 
3064  DeepClone(element_clone, element, &unscaling_layer_document, NULL);
3065 
3066  unscaling_layer_pointer->from_XML(unscaling_layer_document);
3067  }
3068  }
3069 
3070  // Bounding layer
3071  {
3072  const tinyxml2::XMLElement* element = root_element->FirstChildElement("BoundingLayer");
3073 
3074  if(element)
3075  {
3077  {
3079  }
3080 
3081  tinyxml2::XMLDocument bounding_layer_document;
3082 
3083  tinyxml2::XMLElement* element_clone = bounding_layer_document.NewElement("BoundingLayer");
3084  bounding_layer_document.InsertFirstChild(element_clone);
3085 
3086  DeepClone(element_clone, element, &bounding_layer_document, NULL);
3087 
3088  bounding_layer_pointer->from_XML(bounding_layer_document);
3089  }
3090  }
3091 
3092  // Probabilistic layer
3093  {
3094  const tinyxml2::XMLElement* element = root_element->FirstChildElement("ProbabilisticLayer");
3095 
3096  if(element)
3097  {
3099  {
3101  }
3102 
3103  tinyxml2::XMLDocument probabilistic_layer_document;
3104 
3105  tinyxml2::XMLElement* element_clone = probabilistic_layer_document.NewElement("ProbabilisticLayer");
3106  probabilistic_layer_document.InsertFirstChild(element_clone);
3107 
3108  DeepClone(element_clone, element, &probabilistic_layer_document, NULL);
3109 
3110  probabilistic_layer_pointer->from_XML(probabilistic_layer_document);
3111  }
3112  }
3113 
3114  // Conditions layer
3115  {
3116  const tinyxml2::XMLElement* element = root_element->FirstChildElement("ConditionsLayer");
3117 
3118  if(element)
3119  {
3121  {
3123  }
3124 
3125  tinyxml2::XMLDocument conditions_layer_document;
3126 
3127  tinyxml2::XMLElement* element_clone = conditions_layer_document.NewElement("ConditionsLayer");
3128  conditions_layer_document.InsertFirstChild(element_clone);
3129 
3130  DeepClone(element_clone, element, &conditions_layer_document, NULL);
3131 
3132  conditions_layer_pointer->from_XML(conditions_layer_document);
3133  }
3134  }
3135 
3136  // Indpependent parameters
3137  {
3138  const tinyxml2::XMLElement* element = root_element->FirstChildElement("IndependentParameters");
3139 
3140  if(element)
3141  {
3143  {
3145  }
3146 
3147  tinyxml2::XMLDocument independent_parameters_document;
3148 
3149  tinyxml2::XMLElement* element_clone = independent_parameters_document.NewElement("IndependentParameters");
3150  independent_parameters_document.InsertFirstChild(element_clone);
3151 
3152  DeepClone(element_clone, element, &independent_parameters_document, NULL);
3153 
3154  independent_parameters_pointer->from_XML(independent_parameters_document);
3155  }
3156  }
3157 
3158  // Display
3159  {
3160  const tinyxml2::XMLElement* element = root_element->FirstChildElement("Display");
3161 
3162  if(element)
3163  {
3164  const std::string new_display_string = element->GetText();
3165 
3166  try
3167  {
3168  set_display(new_display_string != "0");
3169  }
3170  catch(const std::logic_error& e)
3171  {
3172  std::cout << e.what() << std::endl;
3173  }
3174  }
3175  }
3176 }
3177 
3178 
3179 // void print(void) const method
3180 
3182 
3183 void NeuralNetwork::print(void) const
3184 {
3185  if(display)
3186  {
3187  std::cout << to_string();
3188  }
3189 }
3190 
3191 
3192 // void save(const std::string&) const method
3193 
3196 
3197 void NeuralNetwork::save(const std::string& file_name) const
3198 {
3199  tinyxml2::XMLDocument* document = to_XML();
3200 
3201  document->SaveFile(file_name.c_str());
3202 
3203  delete document;
3204  }
3205 
3206 
3207 // void save_parameters(const std::string&) const method
3208 
3211 
3212 void NeuralNetwork::save_parameters(const std::string& file_name) const
3213 {
3214  std::ofstream file(file_name.c_str());
3215 
3216  if(!file.is_open())
3217  {
3218  std::ostringstream buffer;
3219 
3220  buffer << "OpenNN Exception: NeuralNetwork class.\n"
3221  << "void save_parameters(const std::string&) const method.\n"
3222  << "Cannot open parameters data file.\n";
3223 
3224  throw std::logic_error(buffer.str());
3225  }
3226 
3227  const Vector<double> parameters = arrange_parameters();
3228 
3229  file << parameters << std::endl;
3230 
3231  // Close file
3232 
3233  file.close();
3234 }
3235 
3236 
3237 // void load(const std::string&) method
3238 
3242 
3243 void NeuralNetwork::load(const std::string& file_name)
3244 {
3245  set_default();
3246 
3247  tinyxml2::XMLDocument document;
3248 
3249  if(document.LoadFile(file_name.c_str()))
3250  {
3251  std::ostringstream buffer;
3252 
3253  buffer << "OpenNN Exception: NeuralNetwork class.\n"
3254  << "void load(const std::string&) method.\n"
3255  << "Cannot load XML file " << file_name << ".\n";
3256 
3257  throw std::logic_error(buffer.str());
3258  }
3259 
3260  from_XML(document);
3261 }
3262 
3263 
3264 // void load_parameters(const std::string&) method
3265 
3269 
3270 void NeuralNetwork::load_parameters(const std::string& file_name)
3271 {
3272  std::ifstream file(file_name.c_str());
3273 
3274  if(!file.is_open())
3275  {
3276  std::ostringstream buffer;
3277 
3278  buffer << "OpenNN Exception: NeuralNetwork class.\n"
3279  << "void load_parameters(const std::string&) method.\n"
3280  << "Cannot open parameters data file.\n";
3281 
3282  throw std::logic_error(buffer.str());
3283  }
3284 
3285  const size_t parameters_number = count_parameters_number();
3286 
3287  Vector<double> new_parameters(parameters_number);
3288 
3289  new_parameters.load(file_name);
3290 
3291  set_parameters(new_parameters);
3292 
3293  file.close();
3294 }
3295 
3296 
3297 // std::string write_expression(void) const method
3298 
3300 
3301 std::string NeuralNetwork::write_expression(void) const
3302 {
3303  std::ostringstream buffer;
3304 
3305  #ifndef NDEBUG
3306 
3307  if(!inputs_pointer)
3308  {
3309  buffer.str("");
3310 
3311  buffer << "OpenNN Exception: NeuralNetwork class.\n"
3312  << "std::string write_expression(void) const method.\n"
3313  << "Pointer to inputs is NULL.\n";
3314 
3315  throw std::logic_error(buffer.str());
3316  }
3317 
3319  {
3320  buffer.str("");
3321 
3322  buffer << "OpenNN Exception: NeuralNetwork class.\n"
3323  << "std::string write_expression(void) const method.\n"
3324  << "Pointer to multilayer perceptron is NULL.\n";
3325 
3326  throw std::logic_error(buffer.str());
3327  }
3328 
3329  if(!outputs_pointer)
3330  {
3331  buffer.str("");
3332 
3333  buffer << "OpenNN Exception: NeuralNetwork class.\n"
3334  << "std::string write_expression(void) const method.\n"
3335  << "Pointer to outputs is NULL.\n";
3336 
3337  throw std::logic_error(buffer.str());
3338  }
3339 
3340  #endif
3341 
3342  const size_t inputs_number = multilayer_perceptron_pointer->get_inputs_number();
3343  const size_t outputs_number = multilayer_perceptron_pointer->get_outputs_number();
3344 
3345  const Vector<std::string> inputs_name = inputs_pointer->arrange_names();
3346  const Vector<std::string> outputs_name = outputs_pointer->arrange_names();
3347 
3348  // Scaled inputs
3349 
3350  Vector<std::string> scaled_inputs_name(inputs_number);
3351 
3352  for(size_t i = 0; i < inputs_number; i++)
3353  {
3354  buffer.str("");
3355 
3356  buffer << "scaled_" << inputs_name[i];
3357 
3358  scaled_inputs_name[i] = buffer.str();
3359  }
3360 
3361  // Scaled outputs
3362 
3363  Vector<std::string> scaled_outputs_name(outputs_number);
3364 
3365  for(size_t i = 0; i < outputs_number; i++)
3366  {
3367  buffer.str("");
3368 
3369  buffer << "scaled_" << outputs_name[i];
3370 
3371  scaled_outputs_name[i] = buffer.str();
3372  }
3373 
3374  // Non probabilistic outputs
3375 
3376  Vector<std::string> non_probabilistic_outputs_name(outputs_number);
3377 
3378  for(size_t i = 0; i < outputs_number; i++)
3379  {
3380  buffer.str("");
3381 
3382  buffer << "non_probabilistic_" << outputs_name[i];
3383 
3384  non_probabilistic_outputs_name[i] = buffer.str();
3385  }
3386 
3387  buffer.str("");
3388 
3389  // Scaling layer
3390 
3392  {
3393  buffer << scaling_layer_pointer->write_expression(inputs_name, scaled_inputs_name);
3394  }
3395 
3396  // Multilayer perceptron
3397 
3399  {
3401  {
3402  buffer << multilayer_perceptron_pointer->write_expression(scaled_inputs_name, scaled_outputs_name);
3403  }
3405  {
3406  buffer << multilayer_perceptron_pointer->write_expression(scaled_inputs_name, non_probabilistic_outputs_name);
3407  }
3408  else
3409  {
3410  buffer << multilayer_perceptron_pointer->write_expression(inputs_name, outputs_name);
3411  }
3412  }
3413 
3414  // Outputs unscaling
3415 
3417  {
3418  buffer << unscaling_layer_pointer->write_expression(scaled_outputs_name, outputs_name);
3419  }
3420 
3421  // Probabilistic layer
3422 
3424  {
3425  buffer << probabilistic_layer_pointer->write_expression(non_probabilistic_outputs_name, outputs_name);
3426  }
3427 
3428  // Conditions layer
3429 
3430 // if(conditions_layer_pointer)
3431 // {
3432 // buffer << conditions_layer_pointer->write_expression(inputs_name, outputs_name);
3433 // }
3434 
3435  std::string expression = buffer.str();
3436 
3437  size_t pos;
3438 
3439  std::string search;
3440  std::string replace;
3441 
3442  pos = 0;
3443 
3444  search = "--";
3445  replace = "+";
3446 
3447  while((pos = expression.find(search, pos)) != std::string::npos)
3448  {
3449  expression.replace(pos, search.length(), replace);
3450  pos += replace.length();
3451  }
3452 
3453  pos = 0;
3454 
3455  search = "+-";
3456  replace = "-";
3457 
3458  while((pos = expression.find(search, pos)) != std::string::npos)
3459  {
3460  expression.replace(pos, search.length(), replace);
3461  pos += replace.length();
3462  }
3463 
3464  return(expression);
3465 }
3466 
3467 
3468 // void save_expression(const std::string&) method
3469 
3472 
3473 void NeuralNetwork::save_expression(const std::string& file_name)
3474 {
3475  std::ofstream file(file_name.c_str());
3476 
3477  if(!file.is_open())
3478  {
3479  std::ostringstream buffer;
3480 
3481  buffer << "OpenNN Exception: NeuralNetwork class.\n"
3482  << "void save_expression(const std::string&) method.\n"
3483  << "Cannot open expression text file.\n";
3484 
3485  throw std::logic_error(buffer.str());
3486  }
3487 
3488  file << write_expression();
3489 
3490  file.close();
3491 }
3492 
3493 
3494 // void save_data(const std::string&) const method
3495 
3498 
3499 void NeuralNetwork::save_data(const std::string& file_name) const
3500 {
3501  #ifndef NDEBUG
3502 
3503  std::ostringstream buffer;
3504 
3506  {
3507  buffer << "OpenNN Exception: NeuralNetwork class.\n"
3508  << "void save_data(const std::string&) const method.\n"
3509  << "Pointer to multilayer perceptron is NULL.\n";
3510 
3511  throw std::logic_error(buffer.str());
3512  }
3513 
3514  #endif
3515 
3516  const size_t inputs_number = multilayer_perceptron_pointer->get_inputs_number();
3517 
3518  #ifndef NDEBUG
3519 
3520  if(inputs_number != 1)
3521  {
3522  buffer << "OpenNN Exception: NeuralNetwork class.\n"
3523  << "void save_data(const std::string&) const method.\n"
3524  << "Number of inputs is not 1.\n";
3525 
3526  throw std::logic_error(buffer.str());
3527  }
3528 
3529  #endif
3530 
3531  #ifndef NDEBUG
3532 
3534  {
3535  buffer << "OpenNN Exception: NeuralNetwork class.\n"
3536  << "void save_data(const std::string&) const method.\n"
3537  << "Pointer to scaling layer is NULL.\n";
3538 
3539  throw std::logic_error(buffer.str());
3540  }
3541 
3542  #endif
3543 
3544  const size_t outputs_number = multilayer_perceptron_pointer->get_outputs_number();
3545 
3546  const size_t variables_number = inputs_number + outputs_number;
3547 
3548  const Vector< Statistics<double> > scaling_layer_statistics = scaling_layer_pointer->get_statistics();
3549 // const Vector< Statistics<double> > unscaling_layer_statistics = unscaling_layer_pointer->get_statistics();
3550 
3551 // const Vector< Statistics<double> > inputs_minimums = scaling_layer_pointer->get_minimums();
3552 // const Vector<double> inputs_maximums = scaling_layer_pointer->get_maximums();
3553 
3554  const size_t points_number = 101;
3555 
3556  Matrix<double> data(points_number, variables_number);
3557 
3558  Vector<double> inputs(inputs_number);
3559  Vector<double> outputs(outputs_number);
3560  Vector<double> row(variables_number);
3561 
3562  Vector<double> increments(inputs_number);
3563 
3564  for(size_t i = 0; i < inputs_number; i++)
3565  {
3566  inputs[i] = scaling_layer_statistics[i].minimum;
3567  increments[i] = (scaling_layer_statistics[i].maximum - scaling_layer_statistics[i].minimum)/(double)(points_number-1.0);
3568  }
3569 
3570  for(size_t i = 0; i < points_number; i++)
3571  {
3572  outputs = calculate_outputs(inputs);
3573 
3574  row = inputs.assemble(outputs);
3575 
3576  data.set_row(i, row);
3577 
3578  inputs += increments;
3579  }
3580 
3581  data.save(file_name);
3582 
3583 }
3584 
3585 }
3586 
3587 // OpenNN: Open Neural Networks Library.
3588 // Copyright (c) 2005-2015 Roberto Lopez.
3589 //
3590 // This library is free software; you can redistribute it and/or
3591 // modify it under the terms of the GNU Lesser General Public
3592 // License as published by the Free Software Foundation; either
3593 // version 2.1 of the License, or any later version.
3594 //
3595 // This library is distributed in the hope that it will be useful,
3596 // but WITHOUT ANY WARRANTY; without even the implied warranty of
3597 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3598 // Lesser General Public License for more details.
3599 
3600 // You should have received a copy of the GNU Lesser General Public
3601 // License along with this library; if not, write to the Free Software
3602 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Vector< std::string > arrange_names(void) const
Definition: outputs.cpp:147
void load_parameters(const std::string &)
std::string to_string(void) const
Returns a string representation of the current bonding layer object.
bool has_independent_parameters(void) const
const size_t & get_external_inputs_number(void) const
Returns the number of external inputs.
size_t count_parameters_number(void) const
tinyxml2::XMLDocument * to_XML(void) const
Vector< double > calculate_outputs(const Vector< double > &) const
bool has_inputs(void) const
Outputs * get_outputs_pointer(void) const
Returns a pointer to the outputs object composing this neural network.
void construct_scaling_layer(void)
void prune_scaling_neuron(const size_t &)
Matrix< double > calculate_Jacobian(const Vector< double > &, const Vector< double > &, const Matrix< double > &) const
size_t get_layers_number(void)
tinyxml2::XMLDocument * to_XML(void) const
Matrix< double > arrange_Jacobian(const Vector< double > &) const
Arranges a "Jacobian" matrix from the vector of derivatives.
ScalingLayer * get_scaling_layer_pointer(void) const
Returns a pointer to the scaling layer composing this neural network.
bool display
Display messages to screen.
std::string write_expression(const Vector< std::string > &, const Vector< std::string > &) const
size_t get_scaling_neurons_number(void) const
Returns the number of unscaling neurons in this layer.
Vector< double > calculate_derivative(const Vector< double > &) const
Vector< T > take_out(const size_t &, const size_t &) const
Definition: vector.h:4928
size_t get_inputs_number(void) const
Returns the number of inputs in the multilayer perceptron.
Definition: inputs.h:103
size_t get_inputs_number(void) const
Returns the number of inputs to the neural network.
void from_XML(const tinyxml2::XMLDocument &)
size_t get_parameters_number(void) const
void destruct_independent_parameters(void)
This method deletes the independent parameters object within the neural network.
bool has_outputs(void) const
Matrix< double > arrange_Jacobian(const Vector< double > &) const
Vector< double > arrange_parameters(void) const
std::string write_expression(void) const
Returns a string with the expression of the function represented by the neural network.
Vector< double > calculate_outputs(const Vector< double > &, const Vector< double > &) const
Vector< Matrix< double > > calculate_Hessian_form(const Vector< double > &) const
void save(const std::string &) const
Definition: matrix.h:6417
void set_outputs_pointer(Outputs *)
size_t get_inputs_number(void) const
Returns the number of inputs to the multilayer perceptron.
BoundingLayer * bounding_layer_pointer
Pointer to a bounding layer object.
const size_t & get_conditions_neurons_number(void) const
Returns the number of conditions neurons.
std::string to_string(void) const
Returns a string representation of the current unscaling layer object.
double calculate_parameters_norm(void) const
Returns the norm of the vector of parameters.
std::string to_string(void) const
Returns a string representation of the current outputs object.
Definition: outputs.cpp:733
Vector< double > calculate_outputs(const Vector< double > &) const
virtual void from_XML(const tinyxml2::XMLDocument &)
void from_XML(const tinyxml2::XMLDocument &)
IndependentParameters * independent_parameters_pointer
Pointer to an independent parameters object.
std::string to_string(void) const
Returns a string representation of the current probabilistic layer object.
size_t get_layers_number(void) const
Returns the number of layers in the multilayer perceptron.
ScalingLayer * scaling_layer_pointer
Pointer to a scaling layer object.
Vector< double > calculate_outputs(const Vector< double > &) const
void construct_bounding_layer(void)
void from_XML(const tinyxml2::XMLDocument &)
NeuralNetwork & operator=(const NeuralNetwork &)
virtual void set_default(void)
Sets those members which are not pointer to their default values.
void set_scaling_layer_pointer(ScalingLayer *)
Matrix< double > calculate_directional_input_data(const size_t &, const Vector< double > &, const double &, const double &, const size_t &=101) const
void prune_unscaling_neuron(const size_t &)
size_t get_bounding_neurons_number(void) const
Returns the number of bounding neurons in the layer.
Vector< size_t > arrange_layers_perceptrons_numbers(void) const
Returns a vector with the size of each layer.
Vector< Statistics< double > > get_statistics(void) const
void prune_output(const size_t &)
void destruct_scaling_layer(void)
This method deletes the scaling layer within the neural network.
size_t get_outputs_number(void) const
Returns the number of outputs neurons in the multilayer perceptron.
Vector< double > calculate_outputs(const Vector< double > &) const
void initialize_parameters(const double &)
Initializes all the neural and the independent parameters with a given value.
void set_independent_parameters_pointer(IndependentParameters *)
void destruct_inputs(void)
This method deletes the inputs object within the neural network.
void randomize_parameters_uniform(void)
ProbabilisticLayer * get_probabilistic_layer_pointer(void) const
Returns a pointer to the probabilistic layer composing this neural network.
void destruct_conditions_layer(void)
This method deletes the conditions layer within the neural network.
void set_parameters(const Vector< double > &)
void set_diagonal(const T &)
Definition: matrix.h:1858
virtual tinyxml2::XMLDocument * to_XML(void) const
Definition: outputs.cpp:760
virtual tinyxml2::XMLDocument * to_XML(void) const
Histogram< T > calculate_histogram(const size_t &=10) const
Definition: vector.h:1445
void destruct_multilayer_perceptron(void)
This method deletes the multilayer perceptron within the neural network.
virtual void from_XML(const tinyxml2::XMLDocument &)
Definition: outputs.cpp:839
ConditionsLayer * conditions_layer_pointer
Pointer to a conditions object.
ProbabilisticLayer * probabilistic_layer_pointer
Pointer to a probabilistic layer.
void initialize_random(void)
Initializes at random the probabilistic method.
const size_t & get_probabilistic_neurons_number(void) const
Returns the number of probabilistic neurons in the layer.
Statistics< double > calculate_parameters_statistics(void) const
void print(void) const
Prints to the screen the members of a neural network object in a XML-type format. ...
void construct_conditions_layer(void)
Vector< T > assemble(const Vector< T > &) const
Definition: vector.h:5110
void unscale_parameters(const Vector< double > &)
tinyxml2::XMLDocument * to_XML(void) const
const size_t & get_columns_number(void) const
Returns the number of columns in the matrix.
Definition: matrix.h:1090
Inputs * get_inputs_pointer(void) const
Returns a pointer to the inputs object composing this neural network.
void set_probabilistic_layer_pointer(ProbabilisticLayer *)
bool has_conditions_layer(void) const
void save_parameters(const std::string &) const
virtual void from_XML(const tinyxml2::XMLDocument &)
std::string to_string(void) const
Returns a string representation of the current inputs object.
Definition: inputs.cpp:791
virtual void from_XML(const tinyxml2::XMLDocument &)
Definition: inputs.cpp:899
Vector< double > calculate_outputs(const Vector< double > &) const
void destruct_probabilistic_layer(void)
This method deletes the probabilistic layer within the neural network.
MultilayerPerceptron * get_multilayer_perceptron_pointer(void) const
Returns a pointer to the multilayer perceptron composing this neural network.
double calculate_norm(void) const
Returns the vector norm.
Definition: vector.h:2358
void prune_input(const size_t &)
tinyxml2::XMLDocument * to_XML(void) const
Serializes the conditions layer object into a document of the TinyXML library.
Vector< double > calculate_derivatives(const Vector< double > &) const
std::string to_string(void) const
Returns a string representation of the current multilayer perceptron object.
bool has_unscaling_layer(void) const
virtual tinyxml2::XMLDocument * to_XML(void) const
Definition: inputs.cpp:819
void randomize_parameters_uniform(void)
Initializes the independent parameters with values comprised between -1 and +1.
UnscalingLayer * unscaling_layer_pointer
Pointer to an unscaling layer object.
tinyxml2::XMLDocument * to_XML(void) const
Matrix< double > calculate_Jacobian(const Vector< double > &) const
std::string to_string(void) const
Returns a string representation of the current conditions layer object.
Matrix< double > calculate_output_data(const Matrix< double > &) const
Outputs * outputs_pointer
Pointer to an outputs object.
void save_data(const std::string &) const
void set_multilayer_perceptron_pointer(MultilayerPerceptron *)
virtual tinyxml2::XMLDocument * to_XML(void) const
size_t get_outputs_number(void) const
Returns the number of outputs to the neural network.
Vector< Matrix< double > > calculate_Jacobian_data(const Matrix< double > &) const
Matrix< double > calculate_Jacobian(const Vector< double > &) const
void from_XML(const tinyxml2::XMLDocument &)
virtual void from_XML(const tinyxml2::XMLDocument &)
void resize_inputs_number(const size_t &)
void from_XML(const tinyxml2::XMLDocument &)
const size_t & get_rows_number(void) const
Returns the number of rows in the matrix.
Definition: matrix.h:1079
std::string write_expression(const Vector< std::string > &, const Vector< std::string > &) const
void prune_output(const size_t &)
Definition: outputs.cpp:674
void construct_independent_parameters(void)
UnscalingLayer * get_unscaling_layer_pointer(void) const
Returns a pointer to the unscaling layer composing this neural network.
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
std::string to_string(void) const
Returns a string representation of the current scaling layer object.
void set_unscaling_layer_pointer(UnscalingLayer *)
void initialize_random(void)
Matrix< double > arrange_Jacobian(const Vector< double > &) const
Arranges a "Jacobian" matrix from the vector of derivatives.
void construct_multilayer_perceptron(void)
This method constructs an empty multilayer perceptron within the neural network.
BoundingLayer * get_bounding_layer_pointer(void) const
Returns a pointer to the bounding layer composing this neural network.
ConditionsLayer * get_conditions_layer_pointer(void) const
Returns a pointer to the conditions layer composing this neural network.
IndependentParameters * get_independent_parameters_pointer(void) const
Returns a pointer to the independent parameters object composing this neural network.
std::string write_expression(const Vector< std::string > &, const Vector< std::string > &) const
void save(const std::string &) const
void load(const std::string &)
Definition: vector.h:4799
void initialize_parameters(const double &)
std::string to_string(void) const
Returns a string representation of the current independent parameters object.
void randomize_parameters_normal(void)
Vector< std::string > arrange_names(void) const
Definition: inputs.cpp:147
void destruct_bounding_layer(void)
This method deletes the bounding layer within the neural network.
void initialize_random(void)
Initializes the lower and upper bounds of all the bounding neurons with random values.
std::string to_string(void) const
Returns a string representation of the current neural network object.
Vector< double > dot(const Vector< double > &) const
Definition: matrix.h:5772
void set_bounding_layer_pointer(BoundingLayer *)
Vector< double > calculate_derivatives(const Vector< double > &) const
void set_inputs_pointer(Inputs *)
Statistics< T > calculate_statistics(void) const
Returns the minimum, maximum, mean and the standard deviation of the elements in the vector...
Definition: vector.h:2285
Vector< double > arrange_parameters(void) const
Returns the values of all the biases and synaptic weights in the multilayer perceptron as a single ve...
std::string write_expression(const Vector< std::string > &, const Vector< std::string > &) const
Returns a string with the expression of the inputs scaling process.
void construct_probabilistic_layer(void)
MultilayerPerceptron * multilayer_perceptron_pointer
Pointer to a multilayer perceptron object.
Inputs * inputs_pointer
Pointer to an inputs object.
bool has_probabilistic_layer(void) const
Vector< size_t > arrange_architecture(void) const
virtual ~NeuralNetwork(void)
Destructor.
Vector< Matrix< double > > calculate_Hessian_form(const Vector< double > &) const
const bool & get_display(void) const
Vector< double > calculate_scaled_parameters(void) const
void set_conditions_layer_pointer(ConditionsLayer *)
void destruct_outputs(void)
This method deletes the outputs object within the neural network.
tinyxml2::XMLDocument * to_XML(void) const
void prune_bounding_neuron(const size_t &)
void save_expression(const std::string &)
bool has_multilayer_perceptron(void) const
void resize_outputs_number(const size_t &)
void set_display(const bool &)
void destruct_unscaling_layer(void)
This method deletes the unscaling layer within the neural network.
void set_row(const size_t &, const Vector< T > &)
Definition: matrix.h:1691
virtual void load(const std::string &)
Vector< T > arrange_row(const size_t &) const
Definition: matrix.h:1505
void prune_input(const size_t &)
Definition: inputs.cpp:733
bool has_bounding_layer(void) const
void construct_unscaling_layer(void)
bool operator==(const NeuralNetwork &) const
bool has_scaling_layer(void) const
Matrix< double > calculate_Jacobian(const Vector< double > &) const
void set_parameters(const Vector< double > &)
size_t count_parameters_number(void) const
Returns the number of parameters (biases and synaptic weights) in the multilayer perceptron.
Histogram< double > calculate_parameters_histogram(const size_t &=10) const