OpenNN  2.2
Open Neural Networks Library
independent_parameters_error.cpp
1 /****************************************************************************************************************/
2 /* */
3 /* OpenNN: Open Neural Networks Library */
4 /* www.artelnics.com/opennn */
5 /* */
6 /* I N D E P E N D E N T P A R A M E T E R S 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 "independent_parameters_error.h"
17 
18 
19 namespace OpenNN
20 {
21 
22 // DEFAULT CONSTRUCTOR
23 
27 
29  : PerformanceTerm()
30 {
32 
33  set_default();
34 }
35 
36 
37 // NEURAL NETWORK CONSTRUCTOR
38 
43 
45 : PerformanceTerm(new_neural_network_pointer)
46 {
48 
49  set_default();
50 }
51 
52 
53 // XML CONSTRUCTOR
54 
59 
60 IndependentParametersError::IndependentParametersError(const tinyxml2::XMLDocument& independent_parameters_error_document)
61  : PerformanceTerm(independent_parameters_error_document)
62 {
64 
65  set_default();
66 }
67 
68 
69 // DESTRUCTOR
70 
72 
74 {
75 }
76 
77 
78 // ASSIGNMENT OPERATOR
79 
80 // FinalSolutionsError& operator = (const FinalSolutionsError&) method
81 
83 
85 {
86  if(this != &other_independent_parameters_error)
87  {
88  *neural_network_pointer = *other_independent_parameters_error.neural_network_pointer;
89  *data_set_pointer = *other_independent_parameters_error.data_set_pointer;
90  *mathematical_model_pointer = *other_independent_parameters_error.mathematical_model_pointer;
91  *numerical_differentiation_pointer = *other_independent_parameters_error.numerical_differentiation_pointer;
92  display = other_independent_parameters_error.display;
93 
94  target_independent_parameters = other_independent_parameters_error.target_independent_parameters;
96  }
97 
98  return(*this);
99 }
100 
101 
102 // EQUAL TO OPERATOR
103 
104 // bool operator == (const FinalSolutionsError&) const method
105 
107 
108 bool IndependentParametersError::operator == (const IndependentParametersError& other_independent_parameters_error) const
109 {
110  if(*neural_network_pointer == *other_independent_parameters_error.neural_network_pointer
111  && *mathematical_model_pointer == *other_independent_parameters_error.mathematical_model_pointer
112  && *numerical_differentiation_pointer == *other_independent_parameters_error.numerical_differentiation_pointer
113  && display == other_independent_parameters_error.display
114  && target_independent_parameters == other_independent_parameters_error.target_independent_parameters
115  && independent_parameters_errors_weights == other_independent_parameters_error.independent_parameters_errors_weights)
116  {
117  return(true);
118  }
119  else
120  {
121  return(false);
122  }
123 }
124 
125 
126 // METHODS
127 
128 
129 // const Vector<double>& get_target_independent_parameters(void) const method
130 
132 
134 {
136 }
137 
138 
139 // const double& get_target_independent_parameter(const size_t&) const method
140 
143 
145 {
147 }
148 
149 
150 // const Vector<double>& get_independent_parameters_errors_weights(void) const method
151 
153 
155 {
157 }
158 
159 
160 // const double& get_independent_parameter_error_weight(const size_t&) const method
161 
164 
166 {
168 }
169 
170 
171 // void set_target_independent_parameters(const Vector<double>&) method
172 
175 
177 {
178  target_independent_parameters = new_target_independent_parameters;
179 }
180 
181 
182 // void set_target_independent_parameter(const size_t&, const double&) method
183 
187 
188 void IndependentParametersError::set_target_independent_parameter(const size_t& i, const double& new_target_independent_parameter)
189 {
190  target_independent_parameters[i] = new_target_independent_parameter;
191 }
192 
193 
194 // void set_independent_parameters_errors_weights(const Vector<double>&) method
195 
198 
199 void IndependentParametersError::set_independent_parameters_errors_weights(const Vector<double>& new_independent_parameters_errors_weights)
200 {
201  independent_parameters_errors_weights = new_independent_parameters_errors_weights;
202 }
203 
204 
205 // void set_independent_parameter_error_weight(const size_t&, const double&) method
206 
210 
211 void IndependentParametersError::set_independent_parameter_error_weight(const size_t& i, const double& new_independent_parameter_error_weight)
212 {
213  independent_parameters_errors_weights[i] = new_independent_parameter_error_weight;
214 }
215 
216 
217 // void set_default(void) method
218 
225 
227 {
229  {
231  {
232  const IndependentParameters* independent_parameters_pointer = neural_network_pointer->get_independent_parameters_pointer();
233 
234  const size_t independent_parameters_number = independent_parameters_pointer->get_parameters_number();
235 
236  target_independent_parameters.set(independent_parameters_number, 0.0);
237 
238  independent_parameters_errors_weights.set(independent_parameters_number, 1.0);
239  }
240  }
241  else
242  {
245  }
246 
247  display = true;
248 }
249 
250 
251 // void check(void) const method
252 
256 
258 {
259  std::ostringstream buffer;
260 
261  // Neural network stuff
262 
264  {
265  buffer << "OpenNN Exception: IndependentParametersError class.\n"
266  << "void check(void) const method.\n"
267  << "Pointer to neural network is NULL.\n";
268 
269  throw std::logic_error(buffer.str());
270  }
271 
272  const IndependentParameters* independent_parameters_pointer = neural_network_pointer->get_independent_parameters_pointer();
273 
274  if(!independent_parameters_pointer)
275  {
276  buffer << "OpenNN Exception: IndependentParametersError class.\n"
277  << "void check(void) const method.\n"
278  << "Pointer to independent parameters is NULL.\n";
279 
280  throw std::logic_error(buffer.str());
281  }
282 
283  const size_t independent_parameters_number = independent_parameters_pointer->get_parameters_number();
284 
285  if(independent_parameters_number == 0)
286  {
287  buffer << "OpenNN Exception: IndependentParametersError class.\n"
288  << "void check(void) const method.\n"
289  << "Number of independent parameters is zero.\n";
290 
291  throw std::logic_error(buffer.str());
292  }
293 
294  // Mathematical model stuff
295 
297  {
298  buffer << "OpenNN Exception: IndependentParametersError class.\n"
299  << "void check(void) const method.\n"
300  << "Pointer to mathematical model is NULL.\n";
301 
302  throw std::logic_error(buffer.str());
303  }
304 
305  // Independent parameters error stuff
306 
307  const size_t target_independent_parameters_size = target_independent_parameters.size();
308 
309  if(target_independent_parameters_size != independent_parameters_number)
310  {
311  buffer << "OpenNN Exception: IndependentParametersError class." << std::endl
312  << "double calculate_performance(void) const method." << std::endl
313  << "Size of target independent parameters must be equal to number of independent parameters." << std::endl;
314 
315  throw std::logic_error(buffer.str());
316  }
317 
318  const size_t independent_parameters_errors_weights_size = independent_parameters_errors_weights.size();
319 
320  if(independent_parameters_errors_weights_size != independent_parameters_number)
321  {
322  buffer << "OpenNN Exception: IndependentParametersError class." << std::endl
323  << "double calculate_performance(void) const method." << std::endl
324  << "Size of independent parameters errors weights must be equal to number of independent parameters." << std::endl;
325 
326  throw std::logic_error(buffer.str());
327  }
328 
329 }
330 
331 
332 // double calculate_performance(void) const method
333 
335 
337 {
338  // Neural network stuff
339 
340  #ifndef NDEBUG
341 
342  check();
343 
344  #endif
345 
346  // Neural network stuff
347 
348  const IndependentParameters* independent_parameters_pointer = neural_network_pointer->get_independent_parameters_pointer();
349 
350  const Vector<double> independent_parameters = independent_parameters_pointer->get_parameters();
351 
352  const Vector<double> independent_parameters_error = independent_parameters - target_independent_parameters;
353 
354  return((independent_parameters_errors_weights*independent_parameters_error*independent_parameters_error).calculate_sum());
355 }
356 
357 
358 // double calculate_performance(const Vector<double>&) const method
359 
361 
363 {
364  // Neural network stuff
365 
366  #ifndef NDEBUG
367 
368  check();
369 
370  #endif
371 
372 
373 #ifndef NDEBUG
374 
375  const size_t size = parameters.size();
376 
377  const size_t parameters_number = neural_network_pointer->count_parameters_number();
378 
379  if(size != parameters_number)
380  {
381  std::ostringstream buffer;
382 
383  buffer << "OpenNN Exception: IndependentParametersError class." << std::endl
384  << "double calculate_performance(const Vector<double>&) const method." << std::endl
385  << "Size of parameters (" << size << ") must be equal to number of parameters (" << parameters_number << ")." << std::endl;
386 
387  throw std::logic_error(buffer.str());
388  }
389 
390  #endif
391 
392  NeuralNetwork neural_network_copy(*neural_network_pointer);
393 
394  neural_network_copy.set_parameters(parameters);
395 
396  IndependentParametersError independent_parameters_error_copy(*this);
397 
398  independent_parameters_error_copy.set_neural_network_pointer(&neural_network_copy);
399 
400  return(independent_parameters_error_copy.calculate_performance());
401 }
402 
403 
404 // Vector<double> calculate_gradient(void) const method
405 
407 {
408  // Neural network stuff
409 
410  #ifndef NDEBUG
411 
412  check();
413 
414  #endif
415 
416  const size_t parameters_number = neural_network_pointer->count_parameters_number();
417 
418  const IndependentParameters* independent_parameters_pointer = neural_network_pointer->get_independent_parameters_pointer();
419 
420  const size_t independent_parameters_number = independent_parameters_pointer->get_parameters_number();
421 
422  const size_t neural_parameters_number = parameters_number - independent_parameters_number;
423 
424  Vector<double> multilayer_perceptron_gradient(neural_parameters_number, 0.0);
425 
426  const Vector<double> independent_parameters = independent_parameters_pointer->get_parameters();
427 
428  const Vector<double> independent_parameters_gradient = (independent_parameters - target_independent_parameters)*2.0;
429 
430  return(multilayer_perceptron_gradient.assemble(independent_parameters_gradient));
431 }
432 
433 
434 // Matrix<double> calculate_Hessian(void) const method
435 
437 {
438  // Neural network stuff
439 
440  #ifndef NDEBUG
441 
442  check();
443 
444  #endif
445 
446  const size_t parameters_number = neural_network_pointer->count_parameters_number();
447 
448  const IndependentParameters* independent_parameters_pointer = neural_network_pointer->get_independent_parameters_pointer();
449 
450  const size_t independent_parameters_number = independent_parameters_pointer->get_parameters_number();
451 
452  const size_t neural_parameters_number = parameters_number - independent_parameters_number;
453 
454  Matrix<double> Hessian(parameters_number, parameters_number, 0.0);
455 
456  for(size_t i = neural_parameters_number; i < parameters_number; i++)
457  {
458  for(size_t j = neural_parameters_number; j < parameters_number; j++)
459  {
460  Hessian(i,j) = 2.0;
461  }
462  }
463 
464  return(Hessian);
465 }
466 
467 
468 // std::string write_performance_term_type(void) const method
469 
471 
473 {
474  return("INDEPENDENT_PARAMETERS_ERROR");
475 }
476 
477 
478 // std::string write_information(void) const method
479 
481 {
482  std::ostringstream buffer;
483 
484  buffer << "Independent parameters error: " << calculate_performance() << "\n";
485 
486  return(buffer.str());
487 }
488 
489 
490 // tinyxml2::XMLDocument* to_XML(void) method method
491 
494 
495 tinyxml2::XMLDocument* IndependentParametersError::to_XML(void) const
496 {
497  std::ostringstream buffer;
498 
499  tinyxml2::XMLDocument* document = new tinyxml2::XMLDocument;
500 
501  // Independent parameters error
502 
503  tinyxml2::XMLElement* independent_parameters_error_element = document->NewElement("IndependentParametersError");
504 
505  document->InsertFirstChild(independent_parameters_error_element);
506 
507  // Numerical differentiation
508 
509 // if(numerical_differentiation_pointer)
510 // {
511 // tinyxml2::XMLElement* element = numerical_differentiation_pointer->to_XML()->FirstChildElement();
512 // independent_parameters_error_element->LinkEndChild(element);
513 // }
514 
515  // Target independent parameters
516 
517  {
518  tinyxml2::XMLElement* element = document->NewElement("TargetIndependentParamters");
519  independent_parameters_error_element->LinkEndChild(element);
520 
521  buffer.str("");
523 
524  tinyxml2::XMLText* text = document->NewText(buffer.str().c_str());
525  element->LinkEndChild(text);
526  }
527 
528  // Independent parameters errors weights
529 
530  {
531  tinyxml2::XMLElement* element = document->NewElement("IndependentParametersErrorsWeights");
532  independent_parameters_error_element->LinkEndChild(element);
533 
534  buffer.str("");
536 
537  tinyxml2::XMLText* text = document->NewText(buffer.str().c_str());
538  element->LinkEndChild(text);
539  }
540 
541  // Display
542 
543  {
544  tinyxml2::XMLElement* display_element = document->NewElement("Display");
545  independent_parameters_error_element->LinkEndChild(display_element);
546 
547  buffer.str("");
548  buffer << display;
549 
550  tinyxml2::XMLText* display_text = document->NewText(buffer.str().c_str());
551  display_element->LinkEndChild(display_text);
552  }
553 
554  return(document);
555 }
556 
557 
558 // void from_XML(const tinyxml2::XMLDocument&) method
559 
560 // This method loads a sum squared error object from a XML file.
561 // @param element Name of XML sum squared error file.
562 
563 void IndependentParametersError::from_XML(const tinyxml2::XMLDocument& document)
564 {
565  const tinyxml2::XMLElement* root_element = document.FirstChildElement("IndependentParametersError");
566 
567  if(!root_element)
568  {
569  std::ostringstream buffer;
570 
571  buffer << "OpenNN Exception: IndependentParametersError class.\n"
572  << "void from_XML(const tinyxml2::XMLDocument&) method.\n"
573  << "Independent parameters error element is NULL.\n";
574 
575  throw std::logic_error(buffer.str());
576  }
577 
578  // Display
579  {
580  const tinyxml2::XMLElement* display_element = root_element->FirstChildElement("Display");
581 
582  if(display_element)
583  {
584  const std::string new_display_string = display_element->GetText();
585 
586  try
587  {
588  set_display(new_display_string != "0");
589  }
590  catch(const std::logic_error& e)
591  {
592  std::cout << e.what() << std::endl;
593  }
594  }
595  }
596 }
597 
598 }
599 
600 
601 // OpenNN: Open Neural Networks Library.
602 // Copyright (c) 2005-2015 Roberto Lopez.
603 //
604 // This library is free software; you can redistribute it and/or
605 // modify it under the terms of the GNU Lesser General Public
606 // License as published by the Free Software Foundation; either
607 // version 2.1 of the License, or any later version.
608 //
609 // This library is distributed in the hope that it will be useful,
610 // but WITHOUT ANY WARRANTY; without even the implied warranty of
611 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
612 // Lesser General Public License for more details.
613 // You should have received a copy of the GNU Lesser General Public
614 // License along with this library; if not, write to the Free Software
615 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
bool has_independent_parameters(void) const
size_t count_parameters_number(void) const
const Vector< double > & get_independent_parameters_errors_weights(void) const
Returns the weight for each error between the actual independent parameters and their target values...
Matrix< double > calculate_Hessian(void) const
Returns the performance term Hessian.
size_t get_parameters_number(void) const
MathematicalModel * mathematical_model_pointer
Pointer to a mathematical model object.
const Vector< double > & get_parameters(void) const
Returns the values of the independent parameters.
void set(void)
Sets the size of a vector to zero.
Definition: vector.h:656
std::string write_performance_term_type(void) const
Returns a string with the name of the independent parameters error performance type, "INDEPENDENT_PARAMETERS_ERROR".
void set_target_independent_parameter(const size_t &, const double &)
void set_display(const bool &)
const Vector< double > & get_target_independent_parameters(void) const
Returns the desired values for the independent parameter.
Vector< T > assemble(const Vector< T > &) const
Definition: vector.h:5110
const double & get_independent_parameter_error_weight(const size_t &) const
void set_independent_parameter_error_weight(const size_t &, const double &)
virtual void set_neural_network_pointer(NeuralNetwork *)
double calculate_performance(void) const
Returns the dot product between the independent parameters vector and its targets vector...
void construct_numerical_differentiation(void)
This method constructs the numerical differentiation object which composes the performance term class...
Vector< double > independent_parameters_errors_weights
Weight for each error between the actual independent parameters and their target values.
void set_target_independent_parameters(const Vector< double > &)
void set_independent_parameters_errors_weights(const Vector< double > &)
Vector< double > target_independent_parameters
Desired independent parameter values.
NeuralNetwork * neural_network_pointer
Pointer to a multilayer perceptron object.
NumericalDifferentiation * numerical_differentiation_pointer
Numerical differentiation object.
Vector< double > calculate_gradient(void) const
Returns the performance term gradient.
tinyxml2::XMLDocument * to_XML(void) const
bool display
Display messages to screen.
IndependentParameters * get_independent_parameters_pointer(void) const
Returns a pointer to the independent parameters object composing this neural network.
DataSet * data_set_pointer
Pointer to a data set object.
const double & get_target_independent_parameter(const size_t &) const
bool operator==(const IndependentParametersError &) const
Equal to operator.
IndependentParametersError & operator=(const IndependentParametersError &)
Assignment operator.
void from_XML(const tinyxml2::XMLDocument &)
void set_parameters(const Vector< double > &)