OpenNN  2.2
Open Neural Networks Library
performance_functional.h
1 /****************************************************************************************************************/
2 /* */
3 /* OpenNN: Open Neural Networks Library */
4 /* www.artelnics.com/opennn */
5 /* */
6 /* P E R F O R M A N C E F U N C T I O N A L C L A S S H E A D E R */
7 /* */
8 /* Roberto Lopez */
9 /* Artelnics - Making intelligent use of data */
11 /* */
12 /****************************************************************************************************************/
13 
14 #ifndef __PERFORMANCEFUNCTIONAL_H__
15 #define __PERFORMANCEFUNCTIONAL_H__
16 
17 // System includes
18 
19 #include <string>
20 #include <sstream>
21 #include <iostream>
22 #include <cmath>
23 
24 // OpenNN includes
25 
26 #include "vector.h"
27 #include "matrix.h"
28 #include "numerical_differentiation.h"
29 
30 #include "data_set.h"
31 #include "mathematical_model.h"
32 
33 #include "neural_network.h"
34 #include "performance_term.h"
35 
36 #include "sum_squared_error.h"
37 #include "mean_squared_error.h"
38 #include "root_mean_squared_error.h"
39 #include "normalized_squared_error.h"
40 #include "minkowski_error.h"
41 #include "cross_entropy_error.h"
42 #include "outputs_integrals.h"
43 #include "solutions_error.h"
44 #include "final_solutions_error.h"
45 #include "independent_parameters_error.h"
46 #include "inverse_sum_squared_error.h"
47 
48 #include "neural_parameters_norm.h"
49 
50 // TinyXml includes
51 
52 #include "../tinyxml2/tinyxml2.h"
53 
54 namespace OpenNN
55 {
56 
61 
63 {
64 
65 public:
66 
67  // DEFAULT CONSTRUCTOR
68 
69  explicit PerformanceFunctional(void);
70 
71  // OBJECTIVE FUNCTIONAL CONSTRUCTOR
72 
74 
75  // NEURAL NETWORK CONSTRUCTOR
76 
78 
79  // NEURAL NETWORK AND DATA SET CONSTRUCTOR
80 
82 
83  // NEURAL NETWORK AND MATHEMATICAL MODEL CONSTRUCTOR
84 
86 
87  // NEURAL NETWORK, MATHEMATICAL MODEL AND DATA SET CONSTRUCTOR
88 
90 
91  // FILE CONSTRUCTOR
92 
93  explicit PerformanceFunctional(const std::string&);
94 
95  // XML CONSTRUCTOR
96 
97  explicit PerformanceFunctional(const tinyxml2::XMLDocument&);
98 
99 
100  // COPY CONSTRUCTOR
101 
103 
104  // DESTRUCTOR
105 
106  virtual ~PerformanceFunctional(void);
107 
108  // STRUCTURES
109 
112 
114  {
116 
117  double performance;
118  };
119 
122 
124  {
126 
127  double performance;
128 
130 
132  };
133 
136 
138  {
140 
141  double performance;
142 
144 
146 
148 
150  };
151 
152 
153  // ENUMERATIONS
154 
156 
158  {
159  NO_OBJECTIVE,
160  SUM_SQUARED_ERROR_OBJECTIVE,
161  MEAN_SQUARED_ERROR_OBJECTIVE,
162  ROOT_MEAN_SQUARED_ERROR_OBJECTIVE,
163  NORMALIZED_SQUARED_ERROR_OBJECTIVE,
164  MINKOWSKI_ERROR_OBJECTIVE,
165  CROSS_ENTROPY_ERROR_OBJECTIVE,
166  OUTPUTS_INTEGRALS_OBJECTIVE,
167  SOLUTIONS_ERROR_OBJECTIVE,
168  FINAL_SOLUTIONS_ERROR_OBJECTIVE,
169  INDEPENDENT_PARAMETERS_ERROR_OBJECTIVE,
170  INVERSE_SUM_SQUARED_ERROR_OBJECTIVE,
171  USER_OBJECTIVE
172  };
173 
175 
177  {
178  NO_REGULARIZATION,
179  NEURAL_PARAMETERS_NORM_REGULARIZATION,
180  OUTPUTS_INTEGRALS_REGULARIZATION,
181  USER_REGULARIZATION
182  };
183 
185 
187  {
188  NO_CONSTRAINTS,
189  OUTPUTS_INTEGRALS_CONSTRAINTS,
190  SOLUTIONS_ERROR_CONSTRAINTS,
191  FINAL_SOLUTIONS_ERROR_CONSTRAINTS,
192  INDEPENDENT_PARAMETERS_ERROR_CONSTRAINTS,
193  USER_CONSTRAINTS
194  };
195 
196 
197  // METHODS
198 
199  // Check methods
200 
201  void check_neural_network(void) const;
202 
203  void check_performance_terms(void) const;
204 
205  // Get methods
206 
208 
210  {
211  #ifndef NDEBUG
212 
214  {
215  std::ostringstream buffer;
216 
217  buffer << "OpenNN Exception: PerformanceFunctional class.\n"
218  << "NeuralNetwork* get_neural_network_pointer(void) const method.\n"
219  << "Neural network pointer is NULL.\n";
220 
221  throw std::logic_error(buffer.str());
222  }
223 
224  #endif
225 
226  return(neural_network_pointer);
227  }
228 
230 
232  {
233  #ifndef NDEBUG
234 
236  {
237  std::ostringstream buffer;
238 
239  buffer << "OpenNN Exception: PerformanceFunctional class.\n"
240  << "MathematicalModel* get_mathematical_model_pointer(void) const method.\n"
241  << "MathematicalModel pointer is NULL.\n";
242 
243  throw std::logic_error(buffer.str());
244  }
245 
246  #endif
247 
249  }
250 
252 
253  inline DataSet* get_data_set_pointer(void) const
254  {
255  #ifndef NDEBUG
256 
257  if(!data_set_pointer)
258  {
259  std::ostringstream buffer;
260 
261  buffer << "OpenNN Exception: PerformanceFunctional class.\n"
262  << "DataSet* get_data_set_pointer(void) const method.\n"
263  << "DataSet pointer is NULL.\n";
264 
265  throw std::logic_error(buffer.str());
266  }
267 
268  #endif
269 
270  return(data_set_pointer);
271  }
272 
273 
274  bool has_neural_network(void) const;
275  bool has_mathematical_model(void) const;
276  bool has_data_set(void) const;
277 
278  bool has_generalization(void) const;
279 
280  bool is_sum_squared_terms(void) const;
281 
282 
283  // Objective terms
284 
297 
298  // Regularization terms
299 
303 
304  // Constraints terms
305 
311 
312  // Functional type methods
313 
314  const ObjectiveType& get_objective_type(void) const;
315  const RegularizationType& get_regularization_type(void) const;
316  const ConstraintsType& get_constraints_type(void) const;
317 
318  std::string write_objective_type(void) const;
319  std::string write_regularization_type(void) const;
320  std::string write_constraints_type(void) const;
321 
322  std::string write_objective_type_text(void) const;
323  std::string write_regularization_type_text(void) const;
324  std::string write_constraints_type_text(void) const;
325 
326  // Serialization methods
327 
328  const bool& get_display(void) const;
329 
330  // Set methods
331 
333 
336 
340 
341  void set_default(void);
342 
343  // Functionals methods
344 
345  void set_objective_type(const ObjectiveType&);
348 
349  void set_objective_type(const std::string&);
350  void set_regularization_type(const std::string&);
351  void set_constraints_type(const std::string&);
352 
353  void destruct_objective(void);
354  void destruct_regularization(void);
355  void destruct_constraints(void);
356 
357  void destruct_all_terms(void);
358 
359  void set_display(const bool&);
360 
361  // Performance functional methods
362 
363  double calculate_objective(void) const;
364  double calculate_regularization(void) const;
365  double calculate_constraints(void) const;
366 
367  double calculate_objective(const Vector<double>&) const;
368  double calculate_regularization(const Vector<double>&) const;
369  double calculate_constraints(const Vector<double>&) const;
370 
374 
378 
382 
386 
390 
394 
395  double calculate_performance(void) const;
397  Matrix<double> calculate_Hessian(void) const;
398 
399  double calculate_performance(const Vector<double>&) const;
402 
403  virtual Matrix<double> calculate_inverse_Hessian(void) const;
404 
406 
407  Vector<double> calculate_terms(void) const;
409 
410  virtual ZeroOrderperformance calculate_zero_order_performance(void) const;
411  virtual FirstOrderperformance calculate_first_order_performance(void) const;
412  virtual SecondOrderperformance calculate_second_order_performance(void) const;
413 
414  double calculate_generalization_objective(void) const;
415  double calculate_generalization_regularization(void) const;
416  double calculate_generalization_constraints(void) const;
417 
418  virtual double calculate_generalization_performance(void) const;
419 
420  // Taylor approximation methods
421 
425 
426  // Directional performance
427 
428  double calculate_performance(const Vector<double>&, const double&) const;
429  double calculate_performance_derivative(const Vector<double>&, const double&) const;
430  double calculate_performance_second_derivative(const Vector<double>&, const double&) const;
431 
432  // Serialization methods
433 
434  virtual tinyxml2::XMLDocument* to_XML(void) const;
435  virtual void from_XML(const tinyxml2::XMLDocument&);
436 
437  virtual std::string to_string(void) const;
438  virtual void save(const std::string&) const;
439  virtual void load(const std::string&);
440 
441  virtual std::string write_information(void);
442 
443  void print(void) const;
444 
445 
446 private:
447 
449 
451 
453 
455 
457 
459 
461 
463 
465 
467 
469 
471 
472  // Objective terms
473 
475 
477 
479 
481 
483 
485 
487 
489 
491 
493 
495 
497 
499 
501 
503 
505 
507 
509 
511 
513 
515 
517 
519 
521 
522  // Regularization terms
523 
525 
527 
529 
531 
533 
535 
536  // Constraints terms
537 
539 
541 
543 
545 
547 
549 
551 
553 
555 
557 
559 
560  bool display;
561 };
562 
563 }
564 
565 #endif
566 
567 // OpenNN: Open Neural Networks Library.
568 // Copyright (c) 2005-2015 Roberto Lopez.
569 //
570 // This library is free software; you can redistribute it and/or
571 // modify it under the terms of the GNU Lesser General Public
572 // License as published by the Free Software Foundation; either
573 // version 2.1 of the License, or any later version.
574 //
575 // This library is distributed in the hope that it will be useful,
576 // but WITHOUT ANY WARRANTY; without even the implied warranty of
577 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
578 // Lesser General Public License for more details.
579 
580 // You should have received a copy of the GNU Lesser General Public
581 // License along with this library; if not, write to the Free Software
582 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
SumSquaredError * get_sum_squared_error_objective_pointer(void) const
Vector< double > calculate_regularization_terms(void) const
std::string write_objective_type_text(void) const
Returns a string in text format with the type of objective term used in the performance functional ex...
virtual double calculate_generalization_performance(void) const
std::string write_objective_type(void) const
Returns a string with the type of objective term used in the performance functional expression...
virtual Vector< double > calculate_vector_dot_Hessian(const Vector< double > &) const
double calculate_generalization_objective(void) const
Returns the evaluation of the objective term on the generalization instances of the associated data s...
IndependentParametersError * independent_parameters_error_objective_pointer
Pointer to the independent parameters error object wich can be used as objective term.
Matrix< double > Hessian
Performance function Hessian matrix.
PerformanceTerm * get_user_constraints_pointer(void) const
OutputsIntegrals * outputs_integrals_constraints_pointer
Pointer to the outputs integrals object wich can be used as constraints term.
MinkowskiError * get_Minkowski_error_objective_pointer(void) const
virtual void save(const std::string &) const
virtual SecondOrderperformance calculate_second_order_performance(void) const
Returns a second order performance structure, which contains the value, the gradient and the Hessian ...
void set_user_objective_pointer(PerformanceTerm *)
Vector< double > calculate_gradient(void) const
Returns the performance function gradient, as the sum of the objective and the regularization gradien...
void destruct_all_terms(void)
This method destructs the objective, regularization and constraints terms.
double performance
Performance function performance.
NeuralParametersNorm * get_neural_parameters_norm_regularization_pointer(void) const
void print(void) const
Print the members of this object to the standard output.
const bool & get_display(void) const
const RegularizationType & get_regularization_type(void) const
Returns the type of regularization term used in the performance functional expression.
bool display
Display messages to screen.
double calculate_generalization_regularization(void) const
Returns the evaluation of the regularization term on the generalization instances of the associated d...
IndependentParametersError * independent_parameters_error_constraints_pointer
Pointer to the independent parameters error object wich can be used as constraints term...
OutputsIntegrals * outputs_integrals_objective_pointer
Pointer to the outputs integrals object wich can be used as objective term.
double calculate_performance_second_derivative(const Vector< double > &, const double &) const
MathematicalModel * mathematical_model_pointer
Pointer to a mathematical model object.
std::string write_constraints_type_text(void) const
Returns a string in text format with the type of constraints term used in the performance functional ...
RegularizationType regularization_type
Type of regularization term.
MinkowskiError * Minkowski_error_objective_pointer
Pointer to the Mikowski error object wich can be used as objective term.
virtual Matrix< double > calculate_inverse_Hessian(void) const
ObjectiveType
Enumeration of available objective types in OpenNN.
NeuralNetwork * get_neural_network_pointer(void) const
Returns a pointer to the neural network associated to the performance functional. ...
Vector< double > calculate_terms(void) const
PerformanceTerm * get_user_objective_pointer(void) const
PerformanceTerm * user_objective_pointer
Pointer to the user performance term object wich can be used as objective.
IndependentParametersError * get_independent_parameters_error_constraints_pointer(void) const
FinalSolutionsError * final_solutions_error_objective_pointer
Pointer to the final solutions error object wich can be used as objective term.
NormalizedSquaredError * normalized_squared_error_objective_pointer
Pointer to the normalized squared error object wich can be used as objective term.
void set_regularization_type(const RegularizationType &)
Matrix< double > calculate_constraints_terms_Jacobian(void) const
virtual tinyxml2::XMLDocument * to_XML(void) const
ObjectiveType objective_type
Type of objective term.
Matrix< double > calculate_objective_terms_Jacobian(void) const
NeuralParametersNorm * neural_parameters_norm_regularization_pointer
Pointer to the neural parameters norm object wich can be used as regularization term.
void set_objective_type(const ObjectiveType &)
Matrix< double > calculate_terms_Jacobian(void) const
ConstraintsType
Enumeration of available constraints types in OpenNN.
Matrix< double > calculate_regularization_terms_Jacobian(void) const
double performance
Performance function performance.
FinalSolutionsError * final_solutions_error_constraints_pointer
Pointer to the final solutions error object wich can be used as constraints term. ...
FinalSolutionsError * get_final_solutions_error_objective_pointer(void) const
void set_neural_network_pointer(NeuralNetwork *)
OutputsIntegrals * get_outputs_integrals_regularization_pointer(void) const
PerformanceTerm * user_constraints_pointer
Pointer to a user performance term to represent the contraint.
PerformanceTerm * user_regularization_pointer
Pointer to a user performance term to be used for regularization.
Vector< double > calculate_regularization_gradient(void) const
virtual std::string to_string(void) const
Writes to a string the members of the performance functional object in text format.
virtual std::string write_information(void)
double calculate_generalization_constraints(void) const
Returns the evaluation of the constraints term on the generalization instances of the associated data...
MathematicalModel * get_mathematical_model_pointer(void) const
Returns a pointer to the mathematical model associated to the performance functional.
SolutionsError * solutions_error_constraints_pointer
Pointer to the solutions error object wich can be used as constraints term.
const ConstraintsType & get_constraints_type(void) const
Returns the type of constraints term used in the performance functional expression.
RootMeanSquaredError * get_root_mean_squared_error_objective_pointer(void) const
virtual ZeroOrderperformance calculate_zero_order_performance(void) const
Returns a zero order performance structure, which just contains the performance value of the performa...
NormalizedSquaredError * get_normalized_squared_error_objective_pointer(void) const
RootMeanSquaredError * root_mean_squared_error_objective_pointer
Pointer to the root mean squared error object wich can be used as objective term. ...
DataSet * get_data_set_pointer(void) const
Returns a pointer to the data set associated to the performance functional.
Vector< double > gradient
Performance function gradient vector.
double calculate_performance_derivative(const Vector< double > &, const double &) const
std::string write_regularization_type(void) const
Returns a string with the type of regularization term used in the performance functional expression...
std::string write_constraints_type(void) const
Returns a string with the type of constraints term used in the performance functional expression...
double performance
Performance function performance.
Vector< double > calculate_objective_terms(void) const
double calculate_second_order_Taylor_approximation(const Vector< double > &) const
SolutionsError * get_solutions_error_objective_pointer(void) const
SumSquaredError * sum_squared_error_objective_pointer
Pointer to the sum squared error object wich can be used as objective term.
Vector< double > calculate_objective_gradient(void) const
void set_user_regularization_pointer(PerformanceTerm *)
FinalSolutionsError * get_final_solutions_error_constraints_pointer(void) const
InverseSumSquaredError * get_inverse_sum_squared_error_objective_pointer(void) const
Vector< double > calculate_constraints_gradient(void) const
Vector< double > gradient
Performance function gradient vector.
NeuralNetwork * neural_network_pointer
Pointer to a neural network object.
void set_mathematical_model_pointer(MathematicalModel *)
OutputsIntegrals * get_outputs_integrals_objective_pointer(void) const
void check_neural_network(void) const
Throws an exception if no neural network is associated to the performance functional.
virtual void load(const std::string &)
OutputsIntegrals * get_outputs_integrals_constraints_pointer(void) const
Matrix< double > calculate_regularization_Hessian(void) const
Vector< double > calculate_constraints_terms(void) const
IndependentParametersError * get_independent_parameters_error_objective_pointer(void) const
ConstraintsType constraints_type
Type of constraints term.
SolutionsError * get_solutions_error_constraints_pointer(void) const
double calculate_zero_order_Taylor_approximation(const Vector< double > &) const
const ObjectiveType & get_objective_type(void) const
Returns the type of objective term used in the performance functional expression. ...
MeanSquaredError * get_mean_squared_error_objective_pointer(void) const
Matrix< double > calculate_Hessian(void) const
CrossEntropyError * cross_entropy_error_objective_pointer
Pointer to the cross entropy error object wich can be used as objective term.
virtual FirstOrderperformance calculate_first_order_performance(void) const
Returns a first order performance structure, which contains the value and the gradient of the perform...
PerformanceTerm * get_user_regularization_pointer(void) const
Matrix< double > calculate_constraints_Hessian(void) const
InverseSumSquaredError * inverse_sum_squared_error_objective_pointer
Pointer to the inverse sum squared error object wich can be used as objective term.
CrossEntropyError * get_cross_entropy_error_objective_pointer(void) const
DataSet * data_set_pointer
Pointer to a data set object.
double calculate_first_order_Taylor_approximation(const Vector< double > &) const
void set_constraints_type(const ConstraintsType &)
void set_user_constraints_pointer(PerformanceTerm *)
MeanSquaredError * mean_squared_error_objective_pointer
Pointer to the mean squared error object wich can be used as objective term.
void set_default(void)
Sets the members of the performance functional object to their default values.
OutputsIntegrals * outputs_integrals_regularization_pointer
Pointer to the sum outputs integrals object wich can be used as regularization term.
virtual void from_XML(const tinyxml2::XMLDocument &)
RegularizationType
Enumeration of available regularization types in OpenNN.
Matrix< double > calculate_objective_Hessian(void) const
SolutionsError * solutions_error_objective_pointer
Pointer to the solutions error object wich can be used as objective term.
std::string write_regularization_type_text(void) const
Returns a string in text format with the type of regularization term used in the performance function...