OpenNN  2.2
Open Neural Networks Library
performance_term.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 T E R M 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 __PERFORMANCETERM_H__
15 #define __PERFORMANCETERM_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 
35 // TinyXml includes
36 
37 #include "../tinyxml2/tinyxml2.h"
38 
39 namespace OpenNN
40 {
44 
46 {
47 
48 public:
49 
50  // DEFAULT CONSTRUCTOR
51 
52  explicit PerformanceTerm(void);
53 
54  // NEURAL NETWORK CONSTRUCTOR
55 
56  explicit PerformanceTerm(NeuralNetwork*);
57 
58  // DATA SET CONSTRUCTOR
59 
60  explicit PerformanceTerm(DataSet*);
61 
62  // MATHEMATICAL MODEL CONSTRUCTOR
63 
65 
66  // NEURAL NETWORK AND DATA SET CONSTRUCTOR
67 
69 
70  // NEURAL NETWORK AND MATHEMATICAL MODEL CONSTRUCTOR
71 
73 
74  // NEURAL NETWORK, MATHEMATICAL MODEL AND DATA SET CONSTRUCTOR
75 
77 
78  // XML CONSTRUCTOR
79 
80  explicit PerformanceTerm(const tinyxml2::XMLDocument&);
81 
82  // COPY CONSTRUCTOR
83 
85 
86  // DESTRUCTOR
87 
88  virtual ~PerformanceTerm(void);
89 
90  // ASSIGNMENT OPERATOR
91 
93 
94  // EQUAL TO OPERATOR
95 
96  virtual bool operator == (const PerformanceTerm&) const;
97 
98  // STRUCTURES
99 
102 
104  {
106 
107  double performance;
108  };
109 
110 
113 
115  {
117 
118  double performance;
119 
121 
123  };
124 
125 
128 
130  {
132 
133  double performance;
134 
136 
138 
140 
142  };
143 
144 
148 
150  {
152 
154  };
155 
158 
160  {
162 
164 
166 
168  };
169 
170 
171  // METHODS
172 
173  // Get methods
174 
176 
178  {
179  #ifndef NDEBUG
180 
182  {
183  std::ostringstream buffer;
184 
185  buffer << "OpenNN Exception: PerformanceTerm class.\n"
186  << "NeuralNetwork* get_neural_network_pointer(void) const method.\n"
187  << "Neural network pointer is NULL.\n";
188 
189  throw std::logic_error(buffer.str());
190  }
191 
192  #endif
193 
194  return(neural_network_pointer);
195  }
196 
197 
199 
201  {
202  #ifndef NDEBUG
203 
205  {
206  std::ostringstream buffer;
207 
208  buffer << "OpenNN Exception: PerformanceTerm class.\n"
209  << "MathematicalModel* get_mathematical_model_pointer(void) const method.\n"
210  << "MathematicalModel pointer is NULL.\n";
211 
212  throw std::logic_error(buffer.str());
213  }
214 
215  #endif
216 
217 
219  }
220 
221 
223 
224  inline DataSet* get_data_set_pointer(void) const
225  {
226  #ifndef NDEBUG
227 
228  if(!data_set_pointer)
229  {
230  std::ostringstream buffer;
231 
232  buffer << "OpenNN Exception: PerformanceTerm class.\n"
233  << "DataSet* get_data_set_pointer(void) const method.\n"
234  << "DataSet pointer is NULL.\n";
235 
236  throw std::logic_error(buffer.str());
237  }
238 
239  #endif
240 
241  return(data_set_pointer);
242  }
243 
244 
246 
248  {
249  #ifndef NDEBUG
250 
252  {
253  std::ostringstream buffer;
254 
255  buffer << "OpenNN Exception: PerformanceTerm class.\n"
256  << "NumericalDifferentiation* get_numerical_differentiation_pointer(void) const method.\n"
257  << "Numerical differentiation pointer is NULL.\n";
258 
259  throw std::logic_error(buffer.str());
260  }
261 
262  #endif
263 
265  }
266 
267  const bool& get_display(void) const;
268 
269  bool has_neural_network(void) const;
270  bool has_mathematical_model(void) const;
271  bool has_data_set(void) const;
272  bool has_numerical_differentiation(void) const;
273 
274 
275  // Set methods
276 
277  virtual void set(void);
278  virtual void set(NeuralNetwork*);
279  virtual void set(DataSet*);
280  virtual void set(MathematicalModel*);
281  virtual void set(NeuralNetwork*, DataSet*);
282  virtual void set(NeuralNetwork*, MathematicalModel*);
283  virtual void set(NeuralNetwork*, MathematicalModel*, DataSet*);
284 
285  void set(const PerformanceTerm&);
286 
288 
290  virtual void set_data_set_pointer(DataSet*);
291 
293 
294  virtual void set_default(void);
295 
296  void set_display(const bool&);
297 
298  // Pointer methods
299 
302 
303  // Checking methods
304 
305  virtual void check(void) const;
306 
307  // Layers delta methods
308 
311 
312  // Interlayers Delta methods
313 
315 
316  // Point objective function methods
317 
320 
322 
323  // Objective methods
324 
326 
327  virtual double calculate_performance(void) const = 0;
328 
330 
331  virtual double calculate_performance(const Vector<double>&) const = 0;
332 
334 
335  virtual double calculate_generalization_performance(void) const
336  {
337  return(0.0);
338  }
339 
341 
342  virtual Vector<double> calculate_gradient(void) const;
343 
344  virtual Vector<double> calculate_gradient(const Vector<double>&) const;
345 
347 
348  virtual Matrix<double> calculate_Hessian(void) const;
349 
350  virtual Matrix<double> calculate_Hessian(const Vector<double>&) const;
351 
352  virtual Vector<double> calculate_terms(void) const;
353  virtual Vector<double> calculate_terms(const Vector<double>&) const;
354 
355  virtual Matrix<double> calculate_terms_Jacobian(void) const;
356 
358 
359  virtual std::string write_performance_term_type(void) const;
360 
361  virtual std::string write_information(void) const;
362 
363  // Serialization methods
364 
365  virtual std::string to_string(void) const;
366 
367  virtual tinyxml2::XMLDocument* to_XML(void) const;
368  virtual void from_XML(const tinyxml2::XMLDocument&);
369 
370  size_t calculate_Kronecker_delta(const size_t&, const size_t&) const;
371 
372 protected:
373 
375 
377 
379 
381 
383 
385 
387 
389 
391 
392  bool display;
393 };
394 
395 }
396 
397 #endif
398 
399 // OpenNN: Open Neural Networks Library.
400 // Copyright (c) 2005-2015 Roberto Lopez.
401 //
402 // This library is free software; you can redistribute it and/or
403 // modify it under the terms of the GNU Lesser General Public
404 // License as published by the Free Software Foundation; either
405 // version 2.1 of the License, or any later version.
406 //
407 // This library is distributed in the hope that it will be useful,
408 // but WITHOUT ANY WARRANTY; without even the implied warranty of
409 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
410 // Lesser General Public License for more details.
411 
412 // You should have received a copy of the GNU Lesser General Public
413 // License along with this library; if not, write to the Free Software
414 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
void set_numerical_differentiation_pointer(NumericalDifferentiation *)
size_t calculate_Kronecker_delta(const size_t &, const size_t &) const
virtual tinyxml2::XMLDocument * to_XML(void) const
bool has_data_set(void) const
MathematicalModel * get_mathemtaical_model_pointer(void) const
Returns a pointer to the mathematical model object associated to the performance term.
Vector< double > terms
Subterms performance vector.
virtual std::string write_performance_term_type(void) const
Returns a string with the default type of performance term, "USER_PERFORMANCE_TERM".
Matrix< double > Hessian
Performance term Hessian matrix.
MathematicalModel * mathematical_model_pointer
Pointer to a mathematical model object.
Matrix< Matrix< double > > calculate_interlayers_Delta(const Vector< Vector< double > > &, const Vector< Vector< double > > &, const Matrix< Matrix< double > > &, const Vector< double > &, const Matrix< double > &, const Vector< Vector< double > > &) const
NeuralNetwork * get_neural_network_pointer(void) const
Returns a pointer to the neural network object associated to the performance term.
double performance
Performance term performance.
virtual Vector< double > calculate_gradient(void) const
Returns the performance term gradient.
Vector< double > calculate_point_gradient(const Vector< double > &, const Vector< Vector< double > > &, const Vector< Vector< double > > &) const
virtual void set_data_set_pointer(DataSet *)
Sets a new data set on which the performance term is to be measured.
virtual PerformanceTerm::FirstOrderTerms calculate_first_order_terms(void) const
Returns the performance of all the subterms composing the performance term.
double performance
Peformance term performance.
virtual void check(void) const
Vector< double > gradient
Performance term gradient vector.
DataSet * get_data_set_pointer(void) const
Returns a pointer to the data set object associated to the performance term.
Matrix< double > Jacobian
Subterms Jacobian matrix.
virtual std::string to_string(void) const
Returns the default string representation of a performance term.
virtual double calculate_performance(void) const =0
Returns the performance value of the performance term.
void set_display(const bool &)
bool has_neural_network(void) const
virtual bool operator==(const PerformanceTerm &) const
virtual void set_neural_network_pointer(NeuralNetwork *)
virtual double calculate_generalization_performance(void) const
Returns an performance of the performance term for generalization purposes.
virtual Matrix< double > calculate_terms_Jacobian(void) const
Returns the Jacobian matrix of the subterms composing the performance term.
virtual Vector< double > calculate_terms(void) const
Returns the performance of all the subterms composing the performance term.
virtual Matrix< double > calculate_Hessian(void) const
Returns the performance term Hessian.
void construct_numerical_differentiation(void)
This method constructs the numerical differentiation object which composes the performance term class...
NumericalDifferentiation * get_numerical_differentiation_pointer(void) const
Returns a pointer to the numerical differentiation object used in this performance term object...
Vector< double > gradient
Performance term gradient vector.
virtual void set_mathematical_model_pointer(MathematicalModel *)
Sets a new mathematical model on which the performance term is to be measured.
NeuralNetwork * neural_network_pointer
Pointer to a multilayer perceptron object.
NumericalDifferentiation * numerical_differentiation_pointer
Numerical differentiation object.
virtual std::string write_information(void) const
Matrix< double > calculate_point_Hessian(const Vector< Vector< double > > &, const Vector< Vector< Vector< double > > > &, const Matrix< Matrix< double > > &, const Vector< Vector< double > > &, const Matrix< Matrix< double > > &) const
bool display
Display messages to screen.
virtual void from_XML(const tinyxml2::XMLDocument &)
DataSet * data_set_pointer
Pointer to a data set object.
virtual void set_default(void)
double performance
Performance term evaluation.
void delete_numerical_differentiation_pointer(void)
This method deletes the numerical differentiation object which composes the performance term class...
Vector< double > terms
Subterms performance vector.
bool has_mathematical_model(void) const
const bool & get_display(void) const
Vector< Vector< double > > calculate_layers_delta(const Vector< Vector< double > > &, const Vector< double > &) const
bool has_numerical_differentiation(void) const
virtual PerformanceTerm & operator=(const PerformanceTerm &)