OpenNN  2.2
Open Neural Networks Library
mean_squared_error.cpp
1 /****************************************************************************************************************/
2 /* */
3 /* OpenNN: Open Neural Networks Library */
4 /* www.artelnics.com/opennn */
5 /* */
6 /* M E A N S Q U A R E D E R R O R C L A S S */
7 /* */
8 /* Roberto Lopez */
9 /* Artelnics - Making intelligent use of data */
11 /* */
12 /****************************************************************************************************************/
13 
14 // OpenNN includes
15 
16 #include "mean_squared_error.h"
17 
18 namespace OpenNN
19 {
20 // DEFAULT CONSTRUCTOR
21 
26 
28 {
29 }
30 
31 
32 // NEURAL NETWORK CONSTRUCTOR
33 
39 
41 : PerformanceTerm(new_neural_network_pointer)
42 {
43 }
44 
45 
46 // DATA SET CONSTRUCTOR
47 
53 
55 : PerformanceTerm(new_data_set_pointer)
56 {
57 }
58 
59 
60 // NEURAL NETWORK AND DATA SET CONSTRUCTOR
61 
68 
69 MeanSquaredError::MeanSquaredError(NeuralNetwork* new_neural_network_pointer, DataSet* new_data_set_pointer)
70 : PerformanceTerm(new_neural_network_pointer, new_data_set_pointer)
71 {
72 }
73 
74 
75 // XML CONSTRUCTOR
76 
82 
83 MeanSquaredError::MeanSquaredError(const tinyxml2::XMLDocument& mean_squared_error_document)
84  : PerformanceTerm(mean_squared_error_document)
85 {
86 }
87 
88 
89 // COPY CONSTRUCTOR
90 
94 
95 MeanSquaredError::MeanSquaredError(const MeanSquaredError& other_mean_squared_error)
96 : PerformanceTerm(other_mean_squared_error)
97 {
98 }
99 
100 
101 // DESTRUCTOR
102 
104 
106 {
107 }
108 
109 
110 // METHODS
111 
112 // void check(void) const method
113 
117 
118 void MeanSquaredError::check(void) const
119 {
120  std::ostringstream buffer;
121 
122  // Neural network stuff
123 
125  {
126  buffer << "OpenNN Exception: MeanSquaredError class.\n"
127  << "void check(void) const method.\n"
128  << "Pointer to neural network is NULL.\n";
129 
130  throw std::logic_error(buffer.str());
131  }
132 
133  const MultilayerPerceptron* multilayer_perceptron_pointer = neural_network_pointer->get_multilayer_perceptron_pointer();
134 
135  if(!multilayer_perceptron_pointer)
136  {
137  buffer << "OpenNN Exception: MeanSquaredError class.\n"
138  << "void check(void) const method.\n"
139  << "Pointer to multilayer perceptron is NULL.\n";
140 
141  throw std::logic_error(buffer.str());
142  }
143 
144  const size_t inputs_number = multilayer_perceptron_pointer->get_inputs_number();
145  const size_t outputs_number = multilayer_perceptron_pointer->get_outputs_number();
146 
147  if(inputs_number == 0)
148  {
149  buffer << "OpenNN Exception: MeanSquaredError class.\n"
150  << "void check(void) const method.\n"
151  << "Number of inputs in multilayer perceptron object is zero.\n";
152 
153  throw std::logic_error(buffer.str());
154  }
155 
156  if(outputs_number == 0)
157  {
158  buffer << "OpenNN Exception: MeanSquaredError class.\n"
159  << "void check(void) const method.\n"
160  << "Number of outputs in multilayer perceptron object is zero.\n";
161 
162  throw std::logic_error(buffer.str());
163  }
164 
165  // Data set stuff
166 
167  if(!data_set_pointer)
168  {
169  buffer << "OpenNN Exception: MeanSquaredError class.\n"
170  << "void check(void) const method.\n"
171  << "Pointer to data set is NULL.\n";
172 
173  throw std::logic_error(buffer.str());
174  }
175 
176  // Sum squared error stuff
177 
178  const Variables& variables = data_set_pointer->get_variables();
179 
180  const size_t data_set_inputs_number = variables.count_inputs_number();
181  const size_t data_set_targets_number = variables.count_targets_number();
182 
183  if(inputs_number != data_set_inputs_number)
184  {
185  buffer << "OpenNN Exception: MeanSquaredError class.\n"
186  << "void check(void) const method.\n"
187  << "Number of inputs in multilayer perceptron must be equal to number of inputs in data set.\n";
188 
189  throw std::logic_error(buffer.str());
190  }
191 
192  if(outputs_number != data_set_targets_number)
193  {
194  buffer << "OpenNN Exception: MeanSquaredError class.\n"
195  << "void check(void) const method.\n"
196  << "Number of outputs in multilayer perceptron must be equal to number of targets in data set.\n";
197 
198  throw std::logic_error(buffer.str());
199  }
200 }
201 
202 
203 // double calculate_performance(void) const method
204 
206 
208 {
209  // Control sentence
210 
211  #ifndef NDEBUG
212 
213  check();
214 
215  #endif
216 
217  // Neural network stuff
218 
219  const MultilayerPerceptron* multilayer_perceptron_pointer = neural_network_pointer->get_multilayer_perceptron_pointer();
220 
221  const size_t inputs_number = multilayer_perceptron_pointer->get_inputs_number();
222  const size_t outputs_number = multilayer_perceptron_pointer->get_outputs_number();
223 
224  // Data set stuff
225 
226  const Instances& instances = data_set_pointer->get_instances();
227 
228  const size_t training_instances_number = instances.count_training_instances_number();
229 
230  const Vector<size_t> training_indices = instances.arrange_training_indices();
231 
232  size_t training_index;
233 
234  const Variables& variables = data_set_pointer->get_variables();
235 
236  const Vector<size_t> inputs_indices = variables.arrange_inputs_indices();
237  const Vector<size_t> targets_indices = variables.arrange_targets_indices();
238 
239  const MissingValues& missing_values = data_set_pointer->get_missing_values();
240 
241  // Mean squared error stuff
242 
243  Vector<double> inputs(inputs_number);
244  Vector<double> outputs(outputs_number);
245  Vector<double> targets(outputs_number);
246 
247  int i = 0;
248 
249  double sum_squared_error = 0.0;
250 
251  #pragma omp parallel for private(i, training_index, inputs, outputs, targets) reduction(+:sum_squared_error)
252 
253  for(i = 0; i < (int)training_instances_number; i++)
254  {
255  training_index = training_indices[i];
256 
257  if(missing_values.has_missing_values(training_index))
258  {
259  continue;
260  }
261 
262  // Input vector
263 
264  inputs = data_set_pointer->get_instance(training_index, inputs_indices);
265 
266  // Output vector
267 
268  outputs = multilayer_perceptron_pointer->calculate_outputs(inputs);
269 
270  // Target vector
271 
272  targets = data_set_pointer->get_instance(training_index, targets_indices);
273 
274  // Sum squared error
275 
276  sum_squared_error += outputs.calculate_sum_squared_error(targets);
277  }
278 
279  return(sum_squared_error/(double)training_instances_number);
280 }
281 
282 
283 // double calculate_performance(const Vector<double>&) const method
284 
288 
290 {
291  // Control sentence (if debug)
292 
293  #ifndef NDEBUG
294 
295  check();
296 
297  #endif
298 
299  #ifndef NDEBUG
300 
301  const size_t size = parameters.size();
302 
303  const size_t parameters_number = neural_network_pointer->count_parameters_number();
304 
305  if(size != parameters_number)
306  {
307  std::ostringstream buffer;
308 
309  buffer << "OpenNN Exception: MeanSquaredError class.\n"
310  << "double calculate_performance(const Vector<double>&) const method.\n"
311  << "Size (" << size << ") must be equal to number of parameters (" << parameters_number << ").\n";
312 
313  throw std::logic_error(buffer.str());
314  }
315 
316  #endif
317 
318  // Neural network stuff
319 
320  const MultilayerPerceptron* multilayer_perceptron_pointer = neural_network_pointer->get_multilayer_perceptron_pointer();
321 
322  const size_t inputs_number = multilayer_perceptron_pointer->get_inputs_number();
323  const size_t outputs_number = multilayer_perceptron_pointer->get_outputs_number();
324 
325  // Data set stuff
326 
327  const Instances& instances = data_set_pointer->get_instances();
328 
329  const size_t training_instances_number = instances.count_training_instances_number();
330 
331  const Vector<size_t> training_indices = instances.arrange_training_indices();
332 
333  size_t training_index;
334 
335  const Variables& variables = data_set_pointer->get_variables();
336 
337  const Vector<size_t> inputs_indices = variables.arrange_inputs_indices();
338  const Vector<size_t> targets_indices = variables.arrange_targets_indices();
339 
340  const MissingValues& missing_values = data_set_pointer->get_missing_values();
341 
342  // Mean squared error stuff
343 
344  Vector<double> inputs(inputs_number);
345  Vector<double> outputs(outputs_number);
346  Vector<double> targets(outputs_number);
347 
348  double sum_squared_error = 0.0;
349 
350  int i = 0;
351 
352  #pragma omp parallel for private(i, training_index, inputs, outputs, targets) reduction(+:sum_squared_error)
353 
354  for(i = 0; i < (int)training_instances_number; i++)
355  {
356  training_index = training_indices[i];
357 
358  if(missing_values.has_missing_values(training_index))
359  {
360  continue;
361  }
362 
363  // Input vector
364 
365  inputs = data_set_pointer->get_instance(training_index, inputs_indices);
366 
367  // Output vector
368 
369  outputs = multilayer_perceptron_pointer->calculate_outputs(inputs, parameters);
370 
371  // Target vector
372 
373  targets = data_set_pointer->get_instance(training_index, targets_indices);
374 
375  // Sum squared error
376 
377  sum_squared_error += outputs.calculate_sum_squared_error(targets);
378  }
379 
380  return(sum_squared_error/(double)training_instances_number);
381 }
382 
383 
384 // double calculate_generalization_performance(void) const method
385 
388 
390 {
391  // Control sentence (if debug)
392 
393  #ifndef NDEBUG
394 
395  check();
396 
397  #endif
398 
399  const MultilayerPerceptron* multilayer_perceptron_pointer = neural_network_pointer->get_multilayer_perceptron_pointer();
400 
401  const size_t inputs_number = multilayer_perceptron_pointer->get_inputs_number();
402  const size_t outputs_number = multilayer_perceptron_pointer->get_outputs_number();
403 
404  const Instances& instances = data_set_pointer->get_instances();
405 
406  const size_t generalization_instances_number = instances.count_generalization_instances_number();
407 
408  if(generalization_instances_number == 0)
409  {
410  return(0.0);
411  }
412 
413  const Vector<size_t> generalization_indices = instances.arrange_generalization_indices();
414 
415  size_t generalization_index;
416 
417  const Variables& variables = data_set_pointer->get_variables();
418 
419  const Vector<size_t> inputs_indices = variables.arrange_inputs_indices();
420  const Vector<size_t> targets_indices = variables.arrange_targets_indices();
421 
422  const MissingValues& missing_values = data_set_pointer->get_missing_values();
423 
424 
425  Vector<double> inputs(inputs_number);
426  Vector<double> outputs(outputs_number);
427  Vector<double> targets(outputs_number);
428 
429  double generalization_performance = 0.0;
430 
431  int i = 0;
432 
433  #pragma omp parallel for private(i, generalization_index, inputs, outputs, targets) reduction(+:generalization_performance)
434 
435  for(i = 0; i < (int)generalization_instances_number; i++)
436  {
437  generalization_index = generalization_indices[i];
438 
439  if(missing_values.has_missing_values(generalization_index))
440  {
441  continue;
442  }
443 
444  // Input vector
445 
446  inputs = data_set_pointer->get_instance(generalization_index, inputs_indices);
447 
448  // Output vector
449 
450  outputs = multilayer_perceptron_pointer->calculate_outputs(inputs);
451 
452  // Target vector
453 
454  targets = data_set_pointer->get_instance(generalization_index, targets_indices);
455 
456  // Sum of squares error
457 
458  generalization_performance += outputs.calculate_sum_squared_error(targets);
459  }
460 
461  return(generalization_performance/(double)generalization_instances_number);
462 }
463 
464 
465 // Vector<double> calculate_gradient(void) const method
466 
469 
471 {
472  // Control sentence (if debug)
473 
474  #ifndef NDEBUG
475 
476  check();
477 
478  #endif
479 
480  // Multilayer percepron stuff
481 
482  const MultilayerPerceptron* multilayer_perceptron_pointer = neural_network_pointer->get_multilayer_perceptron_pointer();
483 
484  const size_t inputs_number = multilayer_perceptron_pointer->get_inputs_number();
485  const size_t outputs_number = multilayer_perceptron_pointer->get_outputs_number();
486 
487  const size_t layers_number = multilayer_perceptron_pointer->get_layers_number();
488 
489  const size_t parameters_number = multilayer_perceptron_pointer->count_parameters_number();
490 
491  Vector< Vector< Vector<double> > > first_order_forward_propagation(2);
492 
493  const bool has_conditions_layer = neural_network_pointer->has_conditions_layer();
494 
495  const ConditionsLayer* conditions_layer_pointer = has_conditions_layer ? neural_network_pointer->get_conditions_layer_pointer() : NULL;
496 
497  Vector<double> particular_solution;
498  Vector<double> homogeneous_solution;
499 
500  // Data set stuff
501 
502  const Instances& instances = data_set_pointer->get_instances();
503 
504  const size_t training_instances_number = instances.count_training_instances_number();
505 
506  const Vector<size_t> training_indices = instances.arrange_training_indices();
507 
508  size_t training_index;
509 
510  const Variables& variables = data_set_pointer->get_variables();
511 
512  const Vector<size_t> inputs_indices = variables.arrange_inputs_indices();
513  const Vector<size_t> targets_indices = variables.arrange_targets_indices();
514 
515  const MissingValues& missing_values = data_set_pointer->get_missing_values();
516 
517  Vector<double> inputs(inputs_number);
518  Vector<double> targets(outputs_number);
519 
520  // Mean squared error stuff
521 
522  Vector< Vector<double> > layers_delta;
523 
524  Vector<double> output_gradient(outputs_number);
525 
526  Vector<double> point_gradient(parameters_number, 0.0);
527 
528  Vector<double> gradient(parameters_number, 0.0);
529 
530  int i = 0;
531 
532  #pragma omp parallel for private(i, training_index, inputs, targets, first_order_forward_propagation, \
533  output_gradient, layers_delta, particular_solution, homogeneous_solution, point_gradient)
534 
535  for(i = 0; i < (int)training_instances_number; i++)
536  {
537  training_index = training_indices[i];
538 
539  if(missing_values.has_missing_values(training_index))
540  {
541  continue;
542  }
543 
544  inputs = data_set_pointer->get_instance(training_index, inputs_indices);
545 
546  targets = data_set_pointer->get_instance(training_index, targets_indices);
547 
548  first_order_forward_propagation = multilayer_perceptron_pointer->calculate_first_order_forward_propagation(inputs);
549 
550  const Vector< Vector<double> >& layers_activation = first_order_forward_propagation[0];
551  const Vector< Vector<double> >& layers_activation_derivative = first_order_forward_propagation[1];
552 
553  if(!has_conditions_layer)
554  {
555  output_gradient = (layers_activation[layers_number-1]-targets)*(2.0/(double)training_instances_number);
556 
557  layers_delta = calculate_layers_delta(layers_activation_derivative, output_gradient);
558  }
559  else
560  {
561  particular_solution = conditions_layer_pointer->calculate_particular_solution(inputs);
562  homogeneous_solution = conditions_layer_pointer->calculate_homogeneous_solution(inputs);
563 
564  output_gradient = (particular_solution+homogeneous_solution*layers_activation[layers_number-1] - targets)*(2.0/(double)training_instances_number);
565 
566  layers_delta = calculate_layers_delta(layers_activation_derivative, homogeneous_solution, output_gradient);
567  }
568 
569  point_gradient = calculate_point_gradient(inputs, layers_activation, layers_delta);
570 
571  #pragma omp critical
572  gradient += point_gradient;
573  }
574 
575  return(gradient);
576 }
577 
578 
579 // Matrix<double> calculate_Hessian(void) const method
580 
582 
584 {
585  Matrix<double> H;
586 
587  return(H);
588 }
589 
590 
591 // FirstOrderPerformance calculate_first_order_performance(void) const method
592 
594 
596 {
597  // Control sentence
598 
599  #ifndef NDEBUG
600 
601  check();
602 
603  #endif
604 
605  FirstOrderPerformance first_order_performance;
606 
607  first_order_performance.performance = calculate_performance();
608  first_order_performance.gradient = calculate_gradient();
609 
610  return(first_order_performance);
611 }
612 
613 
614 // SecondOrderperformance calculate_second_order_performance(void) const method
615 
617 
619 {
620  // Control sentence
621 
622  #ifndef NDEBUG
623 
624  check();
625 
626  #endif
627 
628  SecondOrderPerformance second_order_performance;
629 
630  second_order_performance.performance = calculate_performance();
631  second_order_performance.gradient = calculate_gradient();
632  second_order_performance.Hessian = calculate_Hessian();
633 
634  return(second_order_performance);
635 }
636 
637 
638 // Vector<double> calculate_terms(void) const method
639 
642 
644 {
645  // Control sentence
646 
647  #ifndef NDEBUG
648 
649  check();
650 
651  #endif
652 
653  // Neural network stuff
654 
655  const MultilayerPerceptron* multilayer_perceptron_pointer = neural_network_pointer->get_multilayer_perceptron_pointer();
656 
657  const size_t inputs_number = multilayer_perceptron_pointer->get_inputs_number();
658  const size_t outputs_number = multilayer_perceptron_pointer->get_outputs_number();
659 
660  // Data set stuff
661 
662  const Instances& instances = data_set_pointer->get_instances();
663 
664  const size_t training_instances_number = instances.count_training_instances_number();
665 
666  const Vector<size_t> training_indices = instances.arrange_training_indices();
667 
668  size_t training_index;
669 
670  const Variables& variables = data_set_pointer->get_variables();
671 
672  const Vector<size_t> inputs_indices = variables.arrange_inputs_indices();
673  const Vector<size_t> targets_indices = variables.arrange_targets_indices();
674 
675  const MissingValues& missing_values = data_set_pointer->get_missing_values();
676 
677  // Mean squared error stuff
678 
679  Vector<double> performance_terms(training_instances_number);
680 
681  Vector<double> inputs(inputs_number);
682  Vector<double> outputs(outputs_number);
683  Vector<double> targets(outputs_number);
684 
685  int i = 0;
686 
687  #pragma omp parallel for private(i, training_index, inputs, outputs, targets)
688 
689  for(i = 0; i < (int)training_instances_number; i++)
690  {
691  training_index = training_indices[i];
692 
693  if(missing_values.has_missing_values(training_index))
694  {
695  continue;
696  }
697 
698  // Input vector
699 
700  inputs = data_set_pointer->get_instance(training_index, inputs_indices);
701 
702  // Output vector
703 
704  outputs = multilayer_perceptron_pointer->calculate_outputs(inputs);
705 
706  // Target vector
707 
708  targets = data_set_pointer->get_instance(training_index, targets_indices);
709 
710  // Error
711 
712  performance_terms[i] = outputs.calculate_distance(targets);
713  }
714 
715  return(performance_terms/sqrt((double)training_instances_number));
716 }
717 
718 
719 // Vector<double> calculate_terms(const Vector<double>&) const method
720 
724 
726 {
727  // Control sentence (if debug)
728 
729  #ifndef NDEBUG
730 
731  check();
732 
733  #endif
734 
735  #ifndef NDEBUG
736 
737  std::ostringstream buffer;
738 
739  const size_t size = network_parameters.size();
740 
741  const size_t parameters_number = neural_network_pointer->count_parameters_number();
742 
743  if(size != parameters_number)
744  {
745  buffer << "OpenNN Exception: MeanSquaredError class.\n"
746  << "double calculate_terms(const Vector<double>&) const method.\n"
747  << "Size (" << size << ") must be equal to number of multilayer perceptron parameters (" << parameters_number << ").\n";
748 
749  throw std::logic_error(buffer.str());
750  }
751 
752  #endif
753 
754  NeuralNetwork neural_network_copy(*neural_network_pointer);
755 
756  neural_network_copy.set_parameters(network_parameters);
757 
758  MeanSquaredError mean_squared_error_copy(*this);
759 
760  mean_squared_error_copy.set_neural_network_pointer(&neural_network_copy);
761 
762  return(mean_squared_error_copy.calculate_terms());
763 }
764 
765 
766 // Matrix<double> calculate_terms_Jacobian(void) const method
767 
770 
772 {
773  // Control sentence
774 
775  #ifndef NDEBUG
776 
777  check();
778 
779  #endif
780 
781  // Neural network stuff
782 
783  const MultilayerPerceptron* multilayer_perceptron_pointer = neural_network_pointer->get_multilayer_perceptron_pointer();
784 
785  const size_t inputs_number = multilayer_perceptron_pointer->get_inputs_number();
786  const size_t outputs_number = multilayer_perceptron_pointer->get_outputs_number();
787 
788  const size_t layers_number = multilayer_perceptron_pointer->get_layers_number();
789 
790  const size_t neural_parameters_number = multilayer_perceptron_pointer->count_parameters_number();
791 
792  Vector< Vector< Vector<double> > > first_order_forward_propagation(2);
793 
794  Vector<double> particular_solution;
795  Vector<double> homogeneous_solution;
796 
797  const bool has_conditions_layer = neural_network_pointer->has_conditions_layer();
798 
799  const ConditionsLayer* conditions_layer_pointer = has_conditions_layer ? neural_network_pointer->get_conditions_layer_pointer() : NULL;
800 
801  // Data set stuff
802 
803  const Instances& instances = data_set_pointer->get_instances();
804 
805  const size_t training_instances_number = instances.count_training_instances_number();
806 
807  const Vector<size_t> training_indices = instances.arrange_training_indices();
808 
809  size_t training_index;
810 
811  const Variables& variables = data_set_pointer->get_variables();
812 
813  const Vector<size_t> inputs_indices = variables.arrange_inputs_indices();
814  const Vector<size_t> targets_indices = variables.arrange_targets_indices();
815 
816  const MissingValues& missing_values = data_set_pointer->get_missing_values();
817 
818  Vector<double> inputs(inputs_number);
819  Vector<double> targets(outputs_number);
820 
821  // Performance functional
822 
823  Vector<double> term(outputs_number);
824  double term_norm;
825 
826  Vector<double> output_gradient(outputs_number);
827 
828  Vector< Vector<double> > layers_delta(layers_number);
829  Vector<double> point_gradient(neural_parameters_number);
830 
831  Matrix<double> terms_Jacobian(training_instances_number, neural_parameters_number);
832 
833  // Main loop
834 
835  int i = 0;
836 
837 #pragma omp parallel for private(i, training_index, inputs, targets, first_order_forward_propagation, \
838  term, term_norm, output_gradient, layers_delta, particular_solution, homogeneous_solution, point_gradient)
839 
840  for(i = 0; i < (int)training_instances_number; i++)
841  {
842  training_index = training_indices[i];
843 
844  if(missing_values.has_missing_values(training_index))
845  {
846  continue;
847  }
848 
849  inputs = data_set_pointer->get_instance(training_index, inputs_indices);
850 
851  targets = data_set_pointer->get_instance(training_index, targets_indices);
852 
853  first_order_forward_propagation = multilayer_perceptron_pointer->calculate_first_order_forward_propagation(inputs);
854 
855  const Vector< Vector<double> >& layers_activation = first_order_forward_propagation[0];
856  const Vector< Vector<double> >& layers_activation_derivative = first_order_forward_propagation[1];
857 
858  if(!has_conditions_layer)
859  {
860  const Vector<double>& outputs = first_order_forward_propagation[0][layers_number-1];
861 
862  term = (outputs-targets);
863  term_norm = term.calculate_norm();
864 
865  if(term_norm == 0.0)
866  {
867  output_gradient.set(outputs_number, 0.0);
868  }
869  else
870  {
871  output_gradient = term/term_norm;
872  }
873 
874  layers_delta = calculate_layers_delta(layers_activation_derivative, output_gradient);
875  }
876  else
877  {
878  particular_solution = conditions_layer_pointer->calculate_particular_solution(inputs);
879  homogeneous_solution = conditions_layer_pointer->calculate_homogeneous_solution(inputs);
880 
881  term = (particular_solution+homogeneous_solution*layers_activation[layers_number-1] - targets)/sqrt((double)training_instances_number);
882  term_norm = term.calculate_norm();
883 
884  if(term_norm == 0.0)
885  {
886  output_gradient.set(outputs_number, 0.0);
887  }
888  else
889  {
890  output_gradient = term/term_norm;
891  }
892 
893  layers_delta = calculate_layers_delta(layers_activation_derivative, homogeneous_solution, output_gradient);
894  }
895 
896  point_gradient = calculate_point_gradient(inputs, layers_activation, layers_delta);
897 
898  terms_Jacobian.set_row(i, point_gradient);
899  }
900 
901  return(terms_Jacobian/sqrt((double)training_instances_number));
902 }
903 
904 
905 // FirstOrderTerms calculate_first_order_terms(void) const method
906 
908 
910 
912 {
913  // Control sentence (if debug)
914 
915  #ifndef NDEBUG
916 
917  check();
918 
919  #endif
920 
921  FirstOrderTerms first_order_terms;
922 
923  first_order_terms.terms = calculate_terms();
924 
925  first_order_terms.Jacobian = calculate_terms_Jacobian();
926 
927  return(first_order_terms);
928 }
929 
930 
931 // std::string write_performance_term_type(void) const method
932 
934 
936 {
937  return("MEAN_SQUARED_ERROR");
938 }
939 
940 
941 // tinyxml2::XMLDocument* to_XML(void) const method
942 
945 
946 tinyxml2::XMLDocument* MeanSquaredError::to_XML(void) const
947 {
948  std::ostringstream buffer;
949 
950  tinyxml2::XMLDocument* document = new tinyxml2::XMLDocument;
951 
952  // Mean squared error
953 
954  tinyxml2::XMLElement* mean_squared_error_element = document->NewElement("MeanSquaredError");
955 
956  document->InsertFirstChild(mean_squared_error_element);
957 
958  // Display
959  {
960  tinyxml2::XMLElement* element = document->NewElement("Display");
961  mean_squared_error_element->LinkEndChild(element);
962 
963  buffer.str("");
964  buffer << display;
965 
966  tinyxml2::XMLText* text = document->NewText(buffer.str().c_str());
967  element->LinkEndChild(text);
968  }
969 
970  return(document);
971 }
972 
973 }
974 
975 // OpenNN: Open Neural Networks Library.
976 // Copyright (c) 2005-2015 Roberto Lopez.
977 //
978 // This library is free software; you can redistribute it and/or
979 // modify it under the terms of the GNU Lesser General Public
980 // License as published by the Free Software Foundation; either
981 // version 2.1 of the License, or any later version.
982 //
983 // This library is distributed in the hope that it will be useful,
984 // but WITHOUT ANY WARRANTY; without even the implied warranty of
985 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
986 // Lesser General Public License for more details.
987 
988 // You should have received a copy of the GNU Lesser General Public
989 // License along with this library; if not, write to the Free Software
990 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
virtual ~MeanSquaredError(void)
Destructor.
size_t count_parameters_number(void) const
Vector< double > calculate_terms(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
Vector< double > terms
Subterms performance vector.
Matrix< double > Hessian
Performance term Hessian matrix.
double performance
Performance term performance.
Vector< double > calculate_gradient(void) const
size_t count_training_instances_number(void) const
Returns the number of instances in the data set which will be used for training.
Definition: instances.cpp:387
size_t get_inputs_number(void) const
Returns the number of inputs to the multilayer perceptron.
Vector< double > calculate_point_gradient(const Vector< double > &, const Vector< Vector< double > > &, const Vector< Vector< double > > &) const
void set(void)
Sets the size of a vector to zero.
Definition: vector.h:656
double performance
Peformance term performance.
size_t get_layers_number(void) const
Returns the number of layers in the multilayer perceptron.
bool has_missing_values(void) const
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
tinyxml2::XMLDocument * to_XML(void) const
Vector< double > gradient
Performance term gradient vector.
Matrix< double > Jacobian
Subterms Jacobian matrix.
FirstOrderTerms calculate_first_order_terms(void) const
Returns a first order terms performance structure, which contains the values and the Jacobian of the ...
const MissingValues & get_missing_values(void) const
Returns a reference to the missing values object in the data set.
Definition: data_set.cpp:275
virtual void set_neural_network_pointer(NeuralNetwork *)
Vector< size_t > arrange_targets_indices(void) const
Returns the indices of the target variables.
Definition: variables.cpp:519
Vector< size_t > arrange_training_indices(void) const
Returns the indices of the instances which will be used for training.
Definition: instances.cpp:489
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
Vector< double > get_instance(const size_t &) const
Definition: data_set.cpp:684
bool has_conditions_layer(void) const
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
Matrix< double > calculate_Hessian(void) const
double calculate_generalization_performance(void) const
double calculate_sum_squared_error(const Vector< double > &) const
Definition: vector.h:2569
std::string write_performance_term_type(void) const
Returns a string with the name of the mean squared error performance type, "MEAN_SQUARED_ERROR".
Vector< double > gradient
Performance term gradient vector.
virtual Vector< double > calculate_homogeneous_solution(const Vector< double > &) const
Returns the homogeneous solution for applying boundary conditions.
double calculate_performance(void) const
Returns the mean squared error of a neural network on a data set.
NeuralNetwork * neural_network_pointer
Pointer to a multilayer perceptron object.
virtual Vector< double > calculate_particular_solution(const Vector< double > &) const
Returns the particular solution for applying boundary conditions.
bool display
Display messages to screen.
double calculate_distance(const Vector< double > &) const
Definition: vector.h:2557
ConditionsLayer * get_conditions_layer_pointer(void) const
Returns a pointer to the conditions layer composing this neural network.
size_t count_inputs_number(void) const
Returns the number of input variables of the data set.
Definition: variables.cpp:249
FirstOrderPerformance calculate_first_order_performance(void) const
Vector< Vector< Vector< double > > > calculate_first_order_forward_propagation(const Vector< double > &) const
DataSet * data_set_pointer
Pointer to a data set object.
size_t count_targets_number(void) const
Returns the number of target variables of the data set.
Definition: variables.cpp:271
SecondOrderPerformance calculate_second_order_performance(void) const
void set_row(const size_t &, const Vector< T > &)
Definition: matrix.h:1691
Matrix< double > calculate_terms_Jacobian(void) const
Vector< Vector< double > > calculate_layers_delta(const Vector< Vector< double > > &, const Vector< double > &) const
Vector< size_t > arrange_generalization_indices(void) const
Returns the indices of the instances which will be used for generalization.
Definition: instances.cpp:516
void set_parameters(const Vector< double > &)
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
size_t count_parameters_number(void) const
Returns the number of parameters (biases and synaptic weights) in the multilayer perceptron.