OpenNN  2.2
Open Neural Networks Library
model_selection.cpp
1 /****************************************************************************************************************/
2 /* */
3 /* OpenNN: Open Neural Networks Library */
4 /* www.artelnics.com/opennn */
5 /* */
6 /* M O D E L S E L E C T I O N 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 "model_selection.h"
17 
18 namespace OpenNN
19 {
20 
21 // DEFAULT CONSTRUCTOR
22 
24 
26  : training_strategy_pointer(NULL)
27 {
28  set_default();
29 }
30 
31 
32 // TRAINING STRATEGY CONSTRUCTOR
33 
36 
37 ModelSelection::ModelSelection(TrainingStrategy* new_training_strategy_pointer)
38  : training_strategy_pointer(new_training_strategy_pointer)
39 {
40  set_default();
41 }
42 
43 
44 // FILE CONSTRUCTOR
45 
48 
49 ModelSelection::ModelSelection(const std::string& file_name)
50  : training_strategy_pointer(NULL)
51 {
52  load(file_name);
53 }
54 
55 
56 // XML CONSTRUCTOR
57 
60 
61 ModelSelection::ModelSelection(const tinyxml2::XMLDocument& model_selection_document)
62  : training_strategy_pointer(NULL)
63 {
64  from_XML(model_selection_document);
65 }
66 
67 
68 // DESTRUCTOR
69 
71 
73 {
74 }
75 
76 
77 // METHODS
78 
79 // TrainingStrategy* get_training_strategy_pointer(void) const method
80 
82 
84 {
85  #ifndef NDEBUG
86 
88  {
89  std::ostringstream buffer;
90 
91  buffer << "OpenNN Exception: ModelSelection class.\n"
92  << "DataSet* get_training_strategy_pointer(void) const method.\n"
93  << "Training strategy pointer is NULL.\n";
94 
95  throw std::logic_error(buffer.str());
96  }
97 
98  #endif
99 
100  return(training_strategy_pointer);
101 }
102 
103 
104 // const Vector<size_t>& get_hidden_perceptrons_numbers(void) const method
105 
107 
109 {
111 }
112 
113 
114 // const size_t& get_parameters_assays_number(void) const method
115 
117 
119 {
120  return(parameters_assays_number);
121 }
122 
123 
124 // const bool& get_reserve_parameters_data(void) const method
125 
127 
129 {
130  return(reserve_parameters_data);
131 }
132 
133 
134 // const bool& get_reserve_performance_data(void) const method
135 
137 
139 {
140  return(reserve_performance_data);
141 }
142 
143 
144 // const bool& get_reserve_generalization_performance_data(void) const method
145 
147 
149 {
151 }
152 
153 
154 // const bool& get_reserve_minimal_parameters(void) const method
155 
157 
159 {
161 }
162 
163 
164 // const bool& get_reserve_performance_data_statistics(void) const method
165 
167 
169 {
171 }
172 
173 
174 // const bool& get_reserve_generalization_performance_data_statistics(void) const method
175 
177 
179 {
181 }
182 
183 
184 // const bool& get_reserve_model_order_selection_plot(void) const method
185 
187 
189 {
191 }
192 
193 
194 // const InputsSelectionMethod& get_inputs_selection_method(void) const method
195 
197 
199 {
200  return(inputs_selection_method);
201 }
202 
203 
204 // const bool& get_display(void) const method
205 
208 
209 const bool& ModelSelection::get_display(void) const
210 {
211  return(display);
212 }
213 
214 
215 // void set_training_strategy_pointer(TrainingStrategy*) method
216 
219 
221 {
222  training_strategy_pointer = new_training_strategy_pointer;
223 }
224 
225 
226 // void set_default(void) method
227 
229 
231 {
232 // set_assays_numbers(5, 3);
233 
234  correlation_goal = 0.5;
235 
236  display = true;
237 }
238 
239 
240 // void set_hidden_perceptrons_numbers(const Vector<size_t>&) method
241 
244 
245 void ModelSelection::set_hidden_perceptrons_numbers(const Vector<size_t>& new_hidden_perceptrons_numbers)
246 {
247  hidden_perceptrons_numbers = new_hidden_perceptrons_numbers;
248 }
249 
250 
251 // void set_parameters_assays_number(size_t) method
252 
255 
256 void ModelSelection::set_parameters_assays_number(const size_t& new_parameters_assays_number)
257 {
258  parameters_assays_number = new_parameters_assays_number;
259 }
260 
261 
262 // void set_assays_numbers(const size_t&, const size_t&) method
263 
267 
268 void ModelSelection::set_assays_numbers(const size_t& new_complexity_assays_number, const size_t& new_parameters_assays_number)
269 {
270  // Control sentence (if debug)
271 
272  #ifndef NDEBUG
273 
275  {
276  std::ostringstream buffer;
277 
278  buffer << "OpenNN Exception: ModelSelection class.\n"
279  << "void set_assays_numbers(const size_t&, const size_t&) method.\n"
280  << "Pointer to training strategy is NULL.\n";
281 
282  throw std::logic_error(buffer.str());
283  }
284 
286  {
287  std::ostringstream buffer;
288 
289  buffer << "OpenNN Exception: ModelSelection class.\n"
290  << "void set_assays_numbers(const size_t&, const size_t&) method.\n"
291  << "Pointer to performance functional is NULL.\n";
292 
293  throw std::logic_error(buffer.str());
294  }
295 
297  {
298  std::ostringstream buffer;
299 
300  buffer << "OpenNN Exception: ModelSelection class.\n"
301  << "void set_assays_numbers(const size_t&, const size_t&) method.\n"
302  << "Pointer to multilayer perceptron is NULL.\n";
303 
304  throw std::logic_error(buffer.str());
305  }
306 
307  #endif
308 
309  hidden_perceptrons_numbers.set(new_complexity_assays_number);
310 
311 // const size_t inputs_number = training_strategy_pointer->get_performance_functional_pointer()->get_multilayer_perceptron_pointer()->get_inputs_number();
312 // const size_t outputs_number = training_strategy_pointer->get_performance_functional_pointer()->get_multilayer_perceptron_pointer()->get_outputs_number();
313 
314 // const size_t minimum_hidden_layer_size = (int)sqrt((double)(inputs_number*inputs_number) + (double)(outputs_number*outputs_number));
315 // const size_t maximum_hidden_layer_size = 2*(int)sqrt((double)(inputs_number*inputs_number) + (double)(outputs_number*outputs_number));
316 
317 
318  const size_t minimum_hidden_layer_size = 1;
319  const size_t maximum_hidden_layer_size = 12;
320 
321  size_t hidden_layer_size_increment = (maximum_hidden_layer_size-minimum_hidden_layer_size)/new_complexity_assays_number;
322 
323  if(hidden_layer_size_increment < 1)
324  {
325  hidden_layer_size_increment = 1;
326  }
327 
328  for(size_t i = 0; i < new_complexity_assays_number; i++)
329  {
330  hidden_perceptrons_numbers[i] = minimum_hidden_layer_size + hidden_layer_size_increment*i;
331  }
332 
333  parameters_assays_number = new_parameters_assays_number;
334 }
335 
336 
337 // void set_reserve_parameters_data(const bool&)
338 
341 
342 void ModelSelection::set_reserve_parameters_data(const bool& new_reserve_parameters_data)
343 {
344  reserve_parameters_data = new_reserve_parameters_data;
345 }
346 
347 
348 // void set_reserve_performance_data(const bool&)
349 
352 
353 void ModelSelection::set_reserve_performance_data(const bool& new_reserve_performance_data)
354 {
355  reserve_performance_data = new_reserve_performance_data;
356 }
357 
358 
359 // void set_reserve_generalization_performance_data(const bool&)
360 
363 
364 void ModelSelection::set_reserve_generalization_performance_data(const bool& new_reserve_generalization_performance_data)
365 {
366  reserve_generalization_performance_data = new_reserve_generalization_performance_data;
367 }
368 
369 
370 // void set_reserve_minimal_parameters(const bool&)
371 
374 
375 void ModelSelection::set_reserve_minimal_parameters(const bool& new_reserve_minimal_parameters)
376 {
377  reserve_minimal_parameters = new_reserve_minimal_parameters;
378 }
379 
380 
381 // void set_reserve_performance_data_statistics(const bool&)
382 
385 
386 void ModelSelection::set_reserve_performance_data_statistics(const bool& new_reserve_performance_data_statistics)
387 {
388  reserve_performance_data_statistics = new_reserve_performance_data_statistics;
389 }
390 
391 
392 // void set_reserve_generalization_performance_data_statistics(const bool&)
393 
396 
397 void ModelSelection::set_reserve_generalization_performance_data_statistics(const bool& new_reserve_generalization_performance_data_statistics)
398 {
399  reserve_generalization_performance_data_statistics = new_reserve_generalization_performance_data_statistics;
400 }
401 
402 
403 // void set_reserve_model_order_selection_plot(const bool&)
404 
407 
408 void ModelSelection::set_reserve_model_order_selection_plot(const bool& new_reserve_model_order_selection_plot)
409 {
410  reserve_model_order_selection_plot = new_reserve_model_order_selection_plot;
411 }
412 
413 
414 // void set_inputs_selection_method(const InputsSelectionMethod&) method
415 
418 
420 {
421  inputs_selection_method = new_inputs_selection_method;
422 }
423 
424 
425 // void set_display(const bool&) method
426 
431 
432 void ModelSelection::set_display(const bool& new_display)
433 {
434  display = new_display;
435 }
436 
437 
438 // void check(void) const method
439 
441 
442 void ModelSelection::check(void) const
443 {
444  // Training algorithm stuff
445 
446  std::ostringstream buffer;
447 
449  {
450  buffer << "OpenNN Exception: ModelSelection class.\n"
451  << "void check(void) const method.\n"
452  << "Pointer to training strategy is NULL.\n";
453 
454  throw std::logic_error(buffer.str());
455  }
456 
457  // Performance functional stuff
458 
460 
461  if(!performance_functional_pointer)
462  {
463  buffer << "OpenNN Exception: ModelSelection class.\n"
464  << "void check(void) const method.\n"
465  << "Pointer to performance functional is NULL.\n";
466 
467  throw std::logic_error(buffer.str());
468  }
469 
470  // Neural network stuff
471 
472  const NeuralNetwork* neural_network_pointer = performance_functional_pointer->get_neural_network_pointer();
473 
474  if(!neural_network_pointer)
475  {
476  buffer << "OpenNN Exception: ModelSelection class.\n"
477  << "void check(void) const method.\n"
478  << "Pointer to neural network is NULL.\n";
479 
480  throw std::logic_error(buffer.str());
481  }
482 
483  const MultilayerPerceptron* multilayer_perceptron_pointer = neural_network_pointer->get_multilayer_perceptron_pointer();
484 
485  if(!multilayer_perceptron_pointer)
486  {
487  buffer << "OpenNN Exception: ModelSelection class.\n"
488  << "void check(void) const method.\n"
489  << "Pointer to multilayer perceptron is NULL.\n";
490 
491  throw std::logic_error(buffer.str());
492  }
493 
494  // Data set stuff
495 
496  const DataSet* data_set_pointer = performance_functional_pointer->get_data_set_pointer();
497 
498  if(!data_set_pointer)
499  {
500  buffer << "OpenNN Exception: ModelSelection class.\n"
501  << "void check(void) const method.\n"
502  << "Pointer to data set is NULL.\n";
503 
504  throw std::logic_error(buffer.str());
505  }
506 
507  const Instances& instances = data_set_pointer->get_instances();
508 
509  const size_t generalization_instances_number = instances.count_generalization_instances_number();
510 
511  if(generalization_instances_number == 0)
512  {
513  buffer << "OpenNN Exception: ModelSelection class.\n"
514  << "void check(void) const method.\n"
515  << "Number of generalization instances is zero.\n";
516 
517  throw std::logic_error(buffer.str());
518  }
519 
520 }
521 
522 
523 // Matrix<double> calculate_linear_correlations(void) const method
524 
528 
530 {
531  // Control sentence (if debug)
532 
533  #ifndef NDEBUG
534 
535  check();
536 
537  #endif
538 
540 
541  const DataSet* data_set_pointer = performance_functional_pointer->get_data_set_pointer();
542 
543  const Variables& variables = data_set_pointer->get_variables();
544 
545  const size_t inputs_number = variables.count_inputs_number();
546  const size_t targets_number = variables.count_targets_number();
547 
548  Matrix<double> linear_correlations(inputs_number, targets_number);
549 
550  const Vector<size_t> input_indices = variables.arrange_inputs_indices();
551  const Vector<size_t> target_indices = variables.arrange_targets_indices();
552 
553  Vector<double> input(inputs_number);
554  Vector<double> target(targets_number);
555 
556  for(size_t i = 0; i < inputs_number; i++)
557  {
558  input = data_set_pointer->get_variable(input_indices[i]);
559 
560  for(size_t j = 0; j < inputs_number; j++)
561  {
562  target = data_set_pointer->get_variable(target_indices[j]);
563 
564  linear_correlations(i,j) = target.calculate_linear_correlation(input);
565  }
566  }
567 
568  return(linear_correlations);
569 }
570 
571 
572 // Matrix<double> calculate_logistic_correlations(void) const method
573 
577 
579 {
580  // Control sentence (if debug)
581 
582  #ifndef NDEBUG
583 
584  check();
585 
586  #endif
587 
588  // Problem stuff
589 
591 
592  const DataSet* data_set_pointer = performance_functional_pointer->get_data_set_pointer();
593 
594  const Variables& variables = data_set_pointer->get_variables();
595 
596  const size_t inputs_number = variables.count_inputs_number();
597  const size_t targets_number = variables.count_targets_number();
598 
599  const Instances& instances = data_set_pointer->get_instances();
600 
601  const size_t generalization_instances_number = instances.count_generalization_instances_number();
602 
603  const Vector<size_t> input_indices = variables.arrange_inputs_indices();
604  const Vector<size_t> target_indices = variables.arrange_targets_indices();
605 
606  if(display)
607  {
608  std::cout << "Calculating logistic correlations...\n";
609  }
610 
611  Matrix<double> correlations(inputs_number, targets_number);
612 
613  for(size_t i = 0; i < inputs_number; i++)
614  {
615  const Vector<double> inputs = data_set_pointer->get_variable(input_indices[i]);
616 
617  for(size_t j = 0; j < targets_number; j++)
618  {
619  if(display)
620  {
621  std::cout << "\n#Input " << i+1 << ", target " << j+1 << ".\n\n";
622  }
623 
624  const Vector<double> targets = data_set_pointer->get_variable(target_indices[j]);
625 
626  Matrix<double> data(generalization_instances_number, 2);
627 
628  data.set_column(0, inputs);
629  data.set_column(1, targets);
630 
631  DataSet data_set(data);
632 
633  data_set.scale_inputs("MinimumMaximum");
634 
635  Instances* instances_pointer = data_set.get_instances_pointer();
636 
637  instances_pointer->set_training();
638 
639  NeuralNetwork neural_network(1, 1);
640 
641  MultilayerPerceptron* multilayer_perceptron_pointer = neural_network.get_multilayer_perceptron_pointer();
642 
643  multilayer_perceptron_pointer->set_layer_activation_function(0, Perceptron::Logistic);
644 
645  PerformanceFunctional performance_functional(&neural_network, &data_set);
646 
647  TrainingStrategy training_strategy(&performance_functional);
648 
649  training_strategy.set_main_type(TrainingStrategy::LEVENBERG_MARQUARDT_ALGORITHM);
650 
651  training_strategy.perform_training();
652 
653  const Vector<double> outputs = neural_network.calculate_output_data(inputs.to_column_matrix()).to_vector();
654 
655  correlations(i,j) = outputs.calculate_linear_correlation(inputs);
656  }
657  }
658 
659  return(correlations);
660 }
661 
662 
663 // void perform_maximum_linear_correlation_inputs_selection(void) const method
664 
666 
668 {
669  const Matrix<double> correlations = calculate_linear_correlations();
670 
671  const Matrix<size_t> indices = correlations.calculate_greater_than_indices(correlation_goal);
672 }
673 
674 
675 // void perform_maximum_logistic_correlation_inputs_selection(void) const method
676 
678 
680 {
681  const Matrix<double> correlations = calculate_logistic_correlations();
682 
683  const Matrix<size_t> indices = correlations.calculate_greater_than_indices(correlation_goal);
684 }
685 
686 
687 // void perform_exhaustive_inputs_selection(void) const method
688 
690 
692 {
693  std::ostringstream buffer;
694 
695  buffer << "OpenNN Exception: ModelSelection class.\n"
696  << "ModelSelectionResults perform_inputs_selection(void) const method.\n"
697  << "This method is under development.\n";
698 
699  throw std::logic_error(buffer.str());
700 
701  #ifndef NDEBUG
702 
703  check();
704 
705  #endif
706 
707  /*
708 
709  // Training strategy stuff
710 
711  // Performance functional stuff
712 
713  const PerformanceFunctional* performance_functional_pointer = training_strategy_pointer->get_performance_functional_pointer();
714 
715  // Neural network stuff
716 
717  NeuralNetwork* neural_network_pointer = performance_functional_pointer->get_neural_network_pointer();
718 
719  MultilayerPerceptron* multilayer_perceptron_pointer = neural_network_pointer->get_multilayer_perceptron_pointer();
720 
721  #ifndef NDEBUG
722 
723  const size_t layers_number = multilayer_perceptron_pointer->get_layers_number();
724 
725  if(layers_number != 2)
726  {
727  std::ostringstream buffer;
728 
729  buffer << "OpenNN Exception: ModelSelection class.\n"
730  << "ModelSelectionResults perform_inputs_selection(void) method.\n"
731  << "Number of layers in multilayer perceptron must be two.\n";
732 
733  throw std::logic_error(buffer.str());
734  }
735 
736  #endif
737 
738  // Model order selection stuff
739 
740  const size_t inputs_assays_number = hidden_perceptrons_numbers.size();
741 
742  ModelSelectionResults model_selection_results;
743  model_selection_results.parameters_data.set(inputs_assays_number, parameters_assays_number);
744  model_selection_results.performance_data.set(inputs_assays_number, parameters_assays_number);
745  model_selection_results.generalization_performance_data.set(inputs_assays_number, parameters_assays_number);
746 
747  time_t beginning_time, current_time;
748  time(&beginning_time);
749  double elapsed_time;
750 
751  size_t assay_count = 0;
752 
753  for(size_t i = 0; i < inputs_assays_number; i++)
754  {
755  //multilayer_perceptron_pointer->set_inputs_number(1);
756 
757  for(size_t j = 0; j < parameters_assays_number; j++)
758  {
759  try
760  {
761  assay_count++;
762 
763  // Multilayer perceptron
764 
765  multilayer_perceptron_pointer->randomize_parameters_normal();
766 
767  // Performance functional
768 
769  // Training algorithm
770 
771  training_strategy_pointer->perform_training();
772 
773  // Model order selection
774 
775  model_selection_results.parameters_data(i,j) = multilayer_perceptron_pointer->arrange_parameters();
776 
777  model_selection_results.performance_data(i,j) = training_strategy_pointer->get_performance_functional_pointer()->calculate_performance();
778  model_selection_results.generalization_performance_data(i,j) = training_strategy_pointer->get_performance_functional_pointer()->calculate_generalization_performance();
779 
780  time(&current_time);
781  elapsed_time = difftime(current_time, beginning_time);
782 
783  if(display)
784  {
785  std::cout << "Assay " << assay_count << ";\n"
786  << "Hidden layer size: " << hidden_perceptrons_numbers[i] << "\n"
787  << "Parameters set: " << j+1 << "\n"
788  << "performance: " << model_selection_results.performance_data(i,j) << "\n"
789  << "Generalization performance: " << model_selection_results.generalization_performance_data(i,j) << "\n"
790  << "Elapsed time: " << elapsed_time << std::endl;
791  }
792  }
793  catch(const std::logic_error& e)
794  {
795  std::cerr << e.what() << std::endl;
796  }
797  }
798  }
799 
800  const Vector<size_t> minimal_indices = model_selection_results.generalization_performance_data.calculate_minimal_indices();
801 
802  const Vector<double> minimal_parameters = model_selection_results.parameters_data[minimal_indices [0]][minimal_indices[1]];
803 
804  multilayer_perceptron_pointer->set_parameters(minimal_parameters);
805 
806  return(model_selection_results);
807  */
808 }
809 
810 
811 // ModelSelectionResults perform_inputs_selection(void) const method
812 
814 
816 {
818  {
819  case MaximumLinearCorrelation:
820  {
822  }
823  break;
824 
825  case MaximumLogisticCorrelation:
826  {
828  }
829  break;
830 
831  case Exhaustive:
832  {
834  }
835  break;
836 
837  default:
838  {
839  std::ostringstream buffer;
840 
841  buffer << "OpenNN Exception: ModelSelection class.\n"
842  << "void perform_inputs_selection(void) const method.\n"
843  << "Unknown inputs selection method.\n";
844 
845  throw std::logic_error(buffer.str());
846  }
847  break;
848  }
849 }
850 
851 
852 // ModelSelectionResults perform_order_selection(void) method
853 
855 
857 {
858  std::ostringstream buffer;
859 
860  buffer << "OpenNN Exception: ModelSelection class.\n"
861  << "ModelSelectionResults perform_order_selection(void) method.\n"
862  << "This method is under development.\n";
863 
864  throw std::logic_error(buffer.str());
865 /*
866  // Training algorithm stuff
867 
868  #ifndef NDEBUG
869 
870  check();
871 
872  #endif
873 
874  // Performance functional stuff
875 
876  const PerformanceFunctional* performance_functional_pointer = training_strategy_pointer->get_performance_functional_pointer();
877 
878  DataSet* data_set_pointer = performance_functional_pointer->get_objective_pointer()->get_data_set_pointer();
879 
880  Variables* variables_pointer = data_set_pointer->get_variables_pointer();
881 
882  // Neural network stuff
883 
884  NeuralNetwork* neural_network_pointer = performance_functional_pointer->get_neural_network_pointer();
885 
886  MultilayerPerceptron* multilayer_perceptron_pointer = neural_network_pointer->get_multilayer_perceptron_pointer();
887 
888  #ifndef NDEBUG
889 
890  const size_t layers_number = multilayer_perceptron_pointer->get_layers_number();
891 
892  if(layers_number != 2)
893  {
894  std::ostringstream buffer;
895 
896  buffer << "OpenNN Exception: ModelSelection class.\n"
897  << "ModelSelectionResults perform_order_selection(void) method.\n"
898  << "Number of layers in multilayer perceptron must be two.\n";
899 
900  throw std::logic_error(buffer.str());
901  }
902 
903  #endif
904 
905  // Model order selection stuff
906 
907  const size_t complexity_assays_number = hidden_perceptrons_numbers.size();
908 
909  ModelSelectionResults model_order_selection_results;
910 
911  if(reserve_parameters_data)
912  {
913  model_order_selection_results.parameters_data.set(complexity_assays_number, parameters_assays_number);
914  }
915 
916  if(reserve_performance_data)
917  {
918  model_order_selection_results.performance_data.set(complexity_assays_number, parameters_assays_number);
919  }
920 
921  if(reserve_generalization_performance_data)
922  {
923  model_order_selection_results.generalization_performance_data.set(complexity_assays_number, parameters_assays_number);
924  }
925 
926  time_t beginning_time, current_time;
927  time(&beginning_time);
928  double elapsed_time;
929 
930  size_t assay_count = 0;
931 
932  for(size_t i = 0; i < complexity_assays_number; i++)
933  {
934 // variables_pointer->set_inputs_indices(inputs_indices[i]);
935 
936  //multilayer_perceptron_pointer->set_layer_perceptrons_number(0, hidden_perceptrons_numbers[i]);
937 
938  for(size_t j = 0; j < parameters_assays_number; j++)
939  {
940  try
941  {
942  assay_count++;
943 
944  // Multilayer perceptron
945 
946  multilayer_perceptron_pointer->randomize_parameters_normal();
947 
948  // Performance functional
949 
950  // Training algorithm
951 
952  training_strategy_pointer->perform_training();
953 
954  // Model order selection
955 
956  if(reserve_parameters_data)
957  {
958  model_order_selection_results.parameters_data(i,j) = multilayer_perceptron_pointer->arrange_parameters();
959  }
960 
961  if(reserve_parameters_data)
962  {
963  model_order_selection_results.performance_data(i,j) = training_strategy_pointer->get_performance_functional_pointer()->calculate_performance();
964  }
965 
966  if(reserve_parameters_data)
967  {
968  model_order_selection_results.generalization_performance_data(i,j) = training_strategy_pointer->get_performance_functional_pointer()->calculate_generalization_performance();
969  }
970 
971  time(&current_time);
972  elapsed_time = difftime(current_time, beginning_time);
973 
974  if(display)
975  {
976  std::cout << "Assay " << assay_count << ";\n"
977  << "Hidden layer size: " << hidden_perceptrons_numbers[i] << "\n"
978  << "Parameters set: " << j+1 << "\n"
979  << "performance: " << model_order_selection_results.performance_data(i,j) << "\n"
980  << "Generalization performance: " << model_order_selection_results.generalization_performance_data(i,j) << "\n"
981  << "Elapsed time: " << elapsed_time << std::endl;
982  }
983  }
984  catch(const std::logic_error& e)
985  {
986  std::cerr << e.what() << std::endl;
987  }
988  }
989  }
990 
991  const Vector<size_t> minimal_indices = model_order_selection_results.generalization_performance_data.calculate_minimal_indices();
992 
993  const Vector<double> minimal_parameters = model_order_selection_results.parameters_data[minimal_indices [0]][minimal_indices[1]];
994 
995  multilayer_perceptron_pointer->set_parameters(minimal_parameters);
996 
997  if(reserve_minimal_parameters)
998  {
999  }
1000 
1001 // Vector< Vector<double> > calculate_performance_data_statistics(const ModelSelectionResults&) const method
1002 // return(model_order_selection_data.performance_data.calculate_transpose().calculate_statistics());
1003 
1004 // Vector< Vector<double> > calculate_generalization_performance_data_statistics(const ModelSelectionResults&) const method
1005  // return(model_order_selection_data.generalization_performance_data.calculate_transpose().calculate_statistics());
1006 
1007 // const size_t complexities_number = hidden_perceptrons_numbers.size();
1008 
1009 // file << "# Performance data\n\n";
1010 
1011 // for(size_t i = 0; i < complexities_number; i++)
1012 // {
1013 // file << hidden_perceptrons_numbers[i] << " ";
1014 
1015 // for(size_t j = 0; j < parameters_assays_number; j++)
1016 // {
1017 // file << model_order_selection_data.performance_data(i,j) << " ";
1018 // }
1019 
1020 // file << "\n";
1021 // }
1022 
1023 // file << "\n";
1024 
1025 // file << "# Generalization performance data\n\n";
1026 
1027 // for(size_t i = 0; i < complexities_number; i++)
1028 // {
1029 // file << hidden_perceptrons_numbers[i] << " ";
1030 
1031 // for(size_t j = 0; j < parameters_assays_number; j++)
1032 // {
1033 // file << model_order_selection_data.generalization_performance_data(i,j) << " ";
1034 // }
1035 
1036 // file << "\n";
1037 // }
1038 
1039 
1040  return(model_order_selection_results);
1041 */
1042 }
1043 
1044 
1045 // ModelSelectionResults perform_model_selection(void) const method
1046 
1048 
1050 {
1051  std::ostringstream buffer;
1052 
1053  buffer << "OpenNN Exception: ModelSelection class.\n"
1054  << "ModelSelectionResults perform_model_selection(void) method.\n"
1055  << "This method is under development.\n";
1056 
1057  throw std::logic_error(buffer.str());
1058 
1059 // ModelSelectionResults model_selection_results;
1060 
1061 // return(model_selection_results);
1062 }
1063 
1064 
1065 // tinyxml2::XMLDocument* to_XML(void) const method
1066 
1069 
1070 tinyxml2::XMLDocument* ModelSelection::to_XML(void) const
1071 {
1072  tinyxml2::XMLDocument* document = new tinyxml2::XMLDocument;
1073 
1074  std::ostringstream buffer;
1075 
1076  // Model order selection
1077 
1078  tinyxml2::XMLElement* model_selection_element = document->NewElement("ModelSelection");
1079 
1080  document->InsertFirstChild(model_selection_element);
1081 
1082  // Hidden layer sizes
1083 
1084  tinyxml2::XMLElement* hidden_perceptrons_numbers_element = document->NewElement("HiddenLayerSizes");
1085  model_selection_element->LinkEndChild(hidden_perceptrons_numbers_element);
1086 
1087  buffer.str("");
1088  buffer << hidden_perceptrons_numbers;
1089 
1090  tinyxml2::XMLText* hidden_perceptrons_numbers_text = document->NewText(buffer.str().c_str());
1091  hidden_perceptrons_numbers_element->LinkEndChild(hidden_perceptrons_numbers_text);
1092 
1093  // ParametersAssaysNumber
1094 
1095  tinyxml2::XMLElement* parameters_assays_number_element = document->NewElement("ParametersAssaysNumber");
1096  model_selection_element->LinkEndChild(parameters_assays_number_element);
1097 
1098  buffer.str("");
1099  buffer << parameters_assays_number;
1100 
1101  tinyxml2::XMLText* parameters_assays_number_text = document->NewText(buffer.str().c_str());
1102  parameters_assays_number_element->LinkEndChild(parameters_assays_number_text);
1103 
1104  return(document);
1105 }
1106 
1107 
1108 // void from_XML(const tinyxml2::XMLDocument&) method
1109 
1111 
1112 void ModelSelection::from_XML(const tinyxml2::XMLDocument&)
1113 {
1114 }
1115 
1116 
1117 // void print(void) method
1118 
1120 
1121 void ModelSelection::print(void) const
1122 {
1123  std::cout << to_XML();
1124 }
1125 
1126 
1127 // void save(const std::string&) const method
1128 
1131 
1132 void ModelSelection::save(const std::string& file_name) const
1133 {
1134  tinyxml2::XMLDocument* document = to_XML();
1135 
1136  document->SaveFile(file_name.c_str());
1137 
1138  delete document;
1139 }
1140 
1141 
1142 // void load(const std::string&) method
1143 
1146 
1147 void ModelSelection::load(const std::string& file_name)
1148 {
1149  std::ostringstream buffer;
1150 
1151  tinyxml2::XMLDocument* document = new tinyxml2::XMLDocument;
1152 
1153  if(document->LoadFile(file_name.c_str()))
1154  {
1155  buffer << "OpenNN Exception: ModelSelection class.\n"
1156  << "void load(const std::string&) method.\n"
1157  << "Cannot load XML file " << file_name << ".\n";
1158 
1159  throw std::logic_error(buffer.str());
1160  }
1161 
1162  // Root
1163 
1164  tinyxml2::XMLElement* model_selection_element = document->FirstChildElement("ModelSelection");
1165 
1166  if(!model_selection_element)
1167  {
1168  buffer << "OpenNN Exception: ModelSelection class.\n"
1169  << "void load(const std::string&) method.\n"
1170  << "Invalid model order selection XML root element.\n";
1171 
1172  throw std::logic_error(buffer.str());
1173  }
1174 
1175  // Hidden layer sizes
1176 
1177  tinyxml2::XMLElement* hidden_perceptrons_numbers_element = model_selection_element->FirstChildElement("HiddenLayerSizes");
1178 
1179  if(hidden_perceptrons_numbers_element)
1180  {
1181  Vector<size_t> new_hidden_perceptrons_numbers;
1182  new_hidden_perceptrons_numbers.parse(hidden_perceptrons_numbers_element->GetText());
1183 
1184  try
1185  {
1186  set_hidden_perceptrons_numbers(new_hidden_perceptrons_numbers);
1187  }
1188  catch(const std::logic_error& e)
1189  {
1190  std::cout << e.what() << std::endl;
1191  }
1192  }
1193 
1194  // Parameters assays number
1195 
1196  tinyxml2::XMLElement* parameters_assays_number_element = model_selection_element->FirstChildElement("ParametersAssaysNumber");
1197 
1198  if(parameters_assays_number_element)
1199  {
1200  parameters_assays_number = atoi(parameters_assays_number_element->GetText());
1201  }
1202 }
1203 
1204 }
1205 
1206 // OpenNN: Open Neural Networks Library.
1207 // Copyright (c) 2005-2015 Roberto Lopez.
1208 //
1209 // This library is free software; you can redistribute it and/or
1210 // modify it under the terms of the GNU Lesser General Public
1211 // License as published by the Free Software Foundation; either
1212 // version 2.1 of the License, or any later version.
1213 //
1214 // This library is distributed in the hope that it will be useful,
1215 // but WITHOUT ANY WARRANTY; without even the implied warranty of
1216 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1217 // Lesser General Public License for more details.
1218 
1219 // You should have received a copy of the GNU Lesser General Public
1220 // License along with this library; if not, write to the Free Software
1221 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
void parse(const std::string &)
Definition: vector.h:5217
const bool & get_reserve_performance_data(void) const
Returns true if the performance functional performances are to be reserved, and false otherwise...
void from_XML(const tinyxml2::XMLDocument &)
const Vector< size_t > & get_hidden_perceptrons_numbers(void) const
Returns the complexities of the neural networks given by the numbers of hidden perceptrons.
PerformanceFunctional * get_performance_functional_pointer(void) const
Returns a pointer to the performance functional object to which the training strategy is associated...
InputsSelectionMethod
Enumeration of available methods for inputs selection.
const InputsSelectionMethod & get_inputs_selection_method(void) const
Returns the method for selecting those inputs which have more impact on the targets.
void perform_maximum_linear_correlation_inputs_selection(void) const
const Variables & get_variables(void) const
Returns a constant reference to the variables object composing this data set object.
Definition: data_set.cpp:202
void set_assays_numbers(const size_t &, const size_t &)
ModelSelectionResults perform_model_selection(void) const
bool reserve_generalization_performance_data
True if the generalization performance of all neural networks are to be reserved. ...
const bool & get_reserve_performance_data_statistics(void) const
Returns true if the statistics of the performance data are to be inclued in the model selection resul...
void perform_maximum_logistic_correlation_inputs_selection(void) const
TrainingStrategy * get_training_strategy_pointer(void) const
Returns a pointer to the training strategy object.
bool reserve_minimal_parameters
True if the vector parameters of the neural network presenting minimum generalization performance is ...
size_t parameters_assays_number
Number of trials for each neural network.
void set_reserve_generalization_performance_data_statistics(const bool &)
void set(void)
Sets the size of a vector to zero.
Definition: vector.h:656
NeuralNetwork * get_neural_network_pointer(void) const
Returns a pointer to the neural network associated to the performance functional. ...
const bool & get_reserve_model_order_selection_plot(void) const
Returns true if the necessary data for plotting the model selection results is to be included in the ...
const size_t & get_parameters_assays_number(void) const
Returns the number of trials for each network architecture.
const bool & get_reserve_generalization_performance_data(void) const
Returns true if the performance functional generalization performances are to be reserved, and false otherwise.
bool display
Display messages to screen.
void set_reserve_model_order_selection_plot(const bool &)
Matrix< size_t > calculate_greater_than_indices(const T &) const
Definition: matrix.h:3986
void load(const std::string &)
void set_training(void)
Sets all the instances in the data set for training.
Definition: instances.cpp:778
const bool & get_reserve_parameters_data(void) const
Returns true if the neural network parameters are to be reserved, and false otherwise.
bool reserve_parameters_data
True if the parameters of all neural networks are to be reserved.
void set_inputs_selection_method(const InputsSelectionMethod &)
void check(void) const
Checks that the different pointers needed for performing the model selection are not NULL...
Vector< size_t > arrange_targets_indices(void) const
Returns the indices of the target variables.
Definition: variables.cpp:519
Vector< Statistics< double > > scale_inputs(const std::string &)
Definition: data_set.cpp:2118
size_t count_generalization_instances_number(void) const
Returns the number of instances in the data set which will be used for generalization.
Definition: instances.cpp:409
void set_reserve_performance_data(const bool &)
void set_reserve_parameters_data(const bool &)
void set_reserve_performance_data_statistics(const bool &)
void perform_inputs_selection(void) const
void perform_exhaustive_inputs_selection(void) const
MultilayerPerceptron * get_multilayer_perceptron_pointer(void) const
Returns a pointer to the multilayer perceptron composing this neural network.
double correlation_goal
Goal value for a single input-target correlation.
bool reserve_performance_data
True if the performance of all neural networks are to be reserved.
const bool & get_reserve_generalization_performance_data_statistics(void) const
Returns true if the statistics of the generalization performance data are to be inclued in the model ...
Instances * get_instances_pointer(void)
Returns a pointer to the variables object composing this data set object.
Definition: data_set.cpp:232
DataSet * get_data_set_pointer(void) const
Returns a pointer to the data set associated to the performance functional.
bool reserve_performance_data_statistics
True if the statistics of the different performances are to be reserved.
Matrix< double > calculate_output_data(const Matrix< double > &) const
void set_column(const size_t &, const Vector< T > &)
Definition: matrix.h:1774
bool reserve_model_order_selection_plot
True if the data needed to plot the results from the model selection plot is to be reserved...
void set_training_strategy_pointer(TrainingStrategy *)
ModelSelection(void)
Default constructor.
ModelSelectionResults perform_order_selection(void) const
tinyxml2::XMLDocument * to_XML(void) const
void set_main_type(const MainType &)
TrainingStrategy * training_strategy_pointer
Pointer to a training strategy object.
virtual ~ModelSelection(void)
Destructor.
Matrix< double > calculate_linear_correlations(void) const
void save(const std::string &) const
Vector< double > get_variable(const size_t &) const
Definition: data_set.cpp:749
T calculate_linear_correlation(const Vector< T > &) const
Definition: vector.h:2675
size_t count_inputs_number(void) const
Returns the number of input variables of the data set.
Definition: variables.cpp:249
void set_parameters_assays_number(const size_t &)
Matrix< double > calculate_logistic_correlations(void) const
size_t count_targets_number(void) const
Returns the number of target variables of the data set.
Definition: variables.cpp:271
Matrix< T > to_column_matrix(void) const
Definition: vector.h:5196
const bool & get_display(void) const
void set_reserve_minimal_parameters(const bool &)
void print(void) const
Prints to the screen the XML representation of this model selection object.
void set_layer_activation_function(const size_t &, const Perceptron::ActivationFunction &)
void set_display(const bool &)
void set_reserve_generalization_performance_data(const bool &)
InputsSelectionMethod inputs_selection_method
Inputs selection method variable.
bool reserve_generalization_performance_data_statistics
True if the statistics of the different generalization performances are to be reserved.
const bool & get_reserve_minimal_parameters(void) const
Returns true if the parameters vector of the neural network with minimum performance functional perfo...
Vector< size_t > hidden_perceptrons_numbers
Complexities for selecting the network architecture.
void set_hidden_perceptrons_numbers(const Vector< size_t > &)
Vector< size_t > arrange_inputs_indices(void) const
Returns the indices of the input variables.
Definition: variables.cpp:493
const Instances & get_instances(void) const
Returns a constant reference to the instances object composing this data set object.
Definition: data_set.cpp:222