16 #include "unscaling_layer.h"
35 set(new_unscaling_neurons_number);
51 set(unscaling_layer_document);
59 set(other_unscaling_layer);
81 if(
this != &other_unscaling_layer)
158 for(
size_t i = 0; i < unscaling_neurons_number; i++)
163 return(statistics_matrix);
178 for(
size_t i = 0; i < unscaling_neurons_number; i++)
198 for(
size_t i = 0; i < unscaling_neurons_number; i++)
227 return(
"NoUnscaling");
231 return(
"MinimumMaximum");
235 return(
"MeanStandardDeviation");
239 std::ostringstream buffer;
241 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
242 <<
"std::string write_unscaling_method(void) const method.\n"
243 <<
"Unknown unscaling method.\n";
245 throw std::logic_error(buffer.str());
259 return(
"no unscaling");
263 return(
"mean and standard deviation");
267 return(
"minimum and maximum");
271 std::ostringstream buffer;
273 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
274 <<
"std::string write_unscaling_method_text(void) const method.\n"
275 <<
"Unknown unscaling method.\n";
277 throw std::logic_error(buffer.str());
342 from_XML(new_unscaling_layer_document);
395 const size_t new_statistics_size = new_statistics.size();
397 if(new_statistics_size != unscaling_neurons_number)
399 std::ostringstream buffer;
401 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
402 <<
"void set_statistics(const Vector< Statistics<double> >&) method.\n"
403 <<
"Size of statistics must be equal to number of unscaling neurons.\n";
405 throw std::logic_error(buffer.str());
472 statistics[i].set_standard_deviation(new_standard_deviation);
495 if(new_unscaling_method ==
"NoUnscaling")
499 else if(new_unscaling_method ==
"MeanStandardDeviation")
503 else if(new_unscaling_method ==
"MinimumMaximum")
509 std::ostringstream buffer;
511 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
512 <<
"void set_unscaling_method(const std::string&) method.\n"
513 <<
"Unknown unscaling method: " << new_unscaling_method <<
".\n";
515 throw std::logic_error(buffer.str());
546 if(index >= unscaling_neurons_number)
548 std::ostringstream buffer;
550 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
551 <<
"void prune_unscaling_neuron(const size_t&) method.\n"
552 <<
"Index of unscaling neuron is equal or greater than number of unscaling neurons.\n";
554 throw std::logic_error(buffer.str());
577 const size_t size = outputs.size();
579 if(size != unscaling_neurons_number)
581 std::ostringstream buffer;
583 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
584 <<
"void check_range(const Vector<double>&) const method.\n"
585 <<
"Size of outputs must be equal to number of unscaling neurons.\n";
587 throw std::logic_error(buffer.str());
596 for(
size_t i = 0; i < unscaling_neurons_number; i++)
600 std::cout <<
"OpenNN Warning: UnscalingLayer class.\n"
601 <<
"void check_range(const Vector<double>&) const method.\n"
602 <<
"Output variable " << i <<
" is less than outputs.\n";
607 std::cout <<
"OpenNN Warning: UnscalingLayer class.\n"
608 <<
"void check_range(const Vector<double>&) const method.\n"
609 <<
"Output variable " << i <<
" is greater than maximum.\n";
624 if(unscaling_neurons_number == 0)
646 for(
size_t i = 0; i < unscaling_neurons_number; i++)
669 std::ostringstream buffer;
671 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
672 <<
"void initialize_random(void) method.\n"
673 <<
"Unknown unscaling method.\n";
675 throw std::logic_error(buffer.str());
695 const size_t size = inputs.size();
697 if(size != unscaling_neurons_number)
699 std::ostringstream buffer;
701 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
702 <<
"Vector<double> calculate_outputs(const Vector<double>&) const method.\n"
703 <<
"Size must be equal to number of unscaling neurons.\n";
705 throw std::logic_error(buffer.str());
720 case MeanStandardDeviation:
734 std::ostringstream buffer;
736 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
737 <<
"Vector<double> calculate_outputs(const Vector<double>&) const method.\n"
738 <<
"Unknown unscaling method.\n";
740 throw std::logic_error(buffer.str());
781 case MeanStandardDeviation:
789 std::ostringstream buffer;
791 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
792 <<
"Vector<double> calculate_derivatives(const Vector<double>&) const.\n"
793 <<
"Unknown scaling and unscaling method.\n";
795 throw std::logic_error(buffer.str());
818 case MeanStandardDeviation:
826 std::ostringstream buffer;
828 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
829 <<
"Vector<double> calculate_second_derivatives(const Vector<double>&) const.\n"
830 <<
"Unknown scaling and unscaling method.\n";
832 throw std::logic_error(buffer.str());
851 for(
size_t i = 0; i < unscaling_neurons_number; i++)
857 std::cout <<
"OpenNN Warning: UnscalingLayer class.\n"
858 <<
"Vector<double> calculate_minimum_maximum_outputs(Vector<double>&) const method.\n"
859 <<
"Minimum and maximum values of output variable " << i <<
" are equal.\n"
860 <<
"Those outputs won't be unscaled.\n";
863 outputs[i] = inputs[i];
886 for(
size_t i = 0; i < unscaling_neurons_number; i++)
892 std::cout <<
"OpenNN Warning: UnscalingLayer class.\n"
893 <<
"Vector<double> calculate_minimum_maximum_derivatives(const Vector<double>&) const.\n"
894 <<
"Minimum and maximum values of output variable " << i <<
" are equal.\n"
895 <<
"Those derivatives won't be unscaled.\n";
919 const Vector<double> unscaled_second_derivative(unscaling_neurons_number, 0.0);
921 return(unscaled_second_derivative);
936 for(
size_t i = 0; i < unscaling_neurons_number; i++)
942 std::cout <<
"OpenNN Warning: UnscalingLayer class.\n"
943 <<
"Vector<double> calculate_mean_standard_deviation_outputs(const Vector<double>&) const method.\n"
944 <<
"Standard deviation of output variable " << i <<
" is zero.\n"
945 <<
"Those outputs won't be unscaled.\n";
948 outputs[i] = inputs[i];
969 Vector<double> unscaled_derivative(unscaling_neurons_number, 0.0);
971 for(
size_t i = 0; i < unscaling_neurons_number; i++)
977 std::cout <<
"OpenNN Warning: UnscalingLayer class.\n"
978 <<
"Vector<double> calculate_mean_standard_deviation_derivatives(const Vector<double>&) const.\n"
979 <<
"Standard deviation of output variable " << i <<
" is zero.\n"
980 <<
"Those derivatives won't be unscaled.\n";
983 unscaled_derivative[i] = 1.0;
987 unscaled_derivative[i] =
statistics[i].standard_deviation;
991 return(unscaled_derivative);
1004 const Vector<double> unscaled_second_derivative(unscaling_neurons_number, 0.0);
1006 return(unscaled_second_derivative);
1018 Matrix<double> Jacobian(unscaling_neurons_number, unscaling_neurons_number, 0.0);
1036 for(
size_t i = 0; i < unscaling_neurons_number; i++)
1038 Hessian_form[i].
set(unscaling_neurons_number, unscaling_neurons_number, 0.0);
1040 Hessian_form[i](i,i) = second_derivative[i];
1042 return(Hessian_form);
1052 std::ostringstream buffer;
1056 buffer <<
"Unscaling layer\n";
1058 for(
size_t i = 0; i < unscaling_neurons_number; i++)
1060 buffer <<
"Statistics " << i+1 <<
":\n"
1061 <<
"Minimum: " <<
statistics[i].minimum <<
"\n"
1062 <<
"Maximum: " <<
statistics[i].maximum <<
"\n"
1064 <<
"Standard deviation: " <<
statistics[i].standard_deviation <<
"\n";
1068 <<
"Display: " <<
display <<
"\n";
1070 return(buffer.str());
1081 std::ostringstream buffer;
1083 tinyxml2::XMLDocument* document =
new tinyxml2::XMLDocument;
1085 tinyxml2::XMLElement* unscaling_layer_element = document->NewElement(
"UnscalingLayer");
1087 document->InsertFirstChild(unscaling_layer_element);
1089 tinyxml2::XMLElement* element = NULL;
1090 tinyxml2::XMLText* text = NULL;
1096 element = document->NewElement(
"UnscalingNeuronsNumber");
1097 unscaling_layer_element->LinkEndChild(element);
1100 buffer << unscaling_neurons_number;
1102 text = document->NewText(buffer.str().c_str());
1103 element->LinkEndChild(text);
1106 for(
size_t i = 0; i < unscaling_neurons_number; i++)
1108 tinyxml2::XMLElement* statistics_element = document->NewElement(
"Statistics");
1109 statistics_element->SetAttribute(
"Index", (
unsigned)i+1);
1111 unscaling_layer_element->LinkEndChild(statistics_element);
1115 tinyxml2::XMLElement* minimum_element = document->NewElement(
"Minimum");
1116 statistics_element->LinkEndChild(minimum_element);
1121 tinyxml2::XMLText* minimum_text = document->NewText(buffer.str().c_str());
1122 minimum_element->LinkEndChild(minimum_text);
1126 tinyxml2::XMLElement* maximum_element = document->NewElement(
"Maximum");
1127 statistics_element->LinkEndChild(maximum_element);
1132 tinyxml2::XMLText* maximum_text = document->NewText(buffer.str().c_str());
1133 maximum_element->LinkEndChild(maximum_text);
1137 tinyxml2::XMLElement* mean_element = document->NewElement(
"Mean");
1138 statistics_element->LinkEndChild(mean_element);
1143 tinyxml2::XMLText* mean_text = document->NewText(buffer.str().c_str());
1144 mean_element->LinkEndChild(mean_text);
1148 tinyxml2::XMLElement* standard_deviation_element = document->NewElement(
"StandardDeviation");
1149 statistics_element->LinkEndChild(standard_deviation_element);
1154 tinyxml2::XMLText* standard_deviation_text = document->NewText(buffer.str().c_str());
1155 standard_deviation_element->LinkEndChild(standard_deviation_text);
1160 element = document->NewElement(
"UnscalingMethod");
1161 unscaling_layer_element->LinkEndChild(element);
1164 element->LinkEndChild(text);
1169 element = document->NewElement(
"Display");
1170 unscaling_layer_element->LinkEndChild(element);
1175 text = document->NewText(buffer.str().c_str());
1176 element->LinkEndChild(text);
1190 std::ostringstream buffer;
1192 const tinyxml2::XMLElement* root_element = document.FirstChildElement(
"UnscalingLayer");
1196 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
1197 <<
"void from_XML(const tinyxml2::XMLDocument&) method.\n"
1198 <<
"Unscaling layer element is NULL.\n";
1200 throw std::logic_error(buffer.str());
1205 const tinyxml2::XMLElement* unscaling_neurons_number_element = root_element->FirstChildElement(
"UnscalingNeuronsNumber");
1207 if(!unscaling_neurons_number_element)
1209 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
1210 <<
"void from_XML(const tinyxml2::XMLDocument&) method.\n"
1211 <<
"Unscaling neurons number element is NULL.\n";
1213 throw std::logic_error(buffer.str());
1216 const size_t unscaling_neurons_number = atoi(unscaling_neurons_number_element->GetText());
1218 set(unscaling_neurons_number);
1222 const tinyxml2::XMLElement* start_element = unscaling_neurons_number_element;
1224 for(
size_t i = 0; i < unscaling_neurons_number; i++)
1226 const tinyxml2::XMLElement* statistics_element = start_element->NextSiblingElement(
"Statistics");
1227 start_element = statistics_element;
1229 if(!statistics_element)
1231 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
1232 <<
"void from_XML(const tinyxml2::XMLElement*) method.\n"
1233 <<
"Statistics of unscaling neuron " << i+1 <<
" is NULL.\n";
1235 throw std::logic_error(buffer.str());
1238 statistics_element->QueryUnsignedAttribute(
"Index", &index);
1242 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
1243 <<
"void from_XML(const tinyxml2::XMLElement*) method.\n"
1244 <<
"Index " << index <<
" is not correct.\n";
1246 throw std::logic_error(buffer.str());
1251 const tinyxml2::XMLElement* minimum_element = statistics_element->FirstChildElement(
"Minimum");
1253 if(!minimum_element)
1255 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
1256 <<
"void from_XML(const tinyxml2::XMLElement*) method.\n"
1257 <<
"Minimum element " << i+1 <<
" is NULL.\n";
1259 throw std::logic_error(buffer.str());
1262 if(minimum_element->GetText())
1264 statistics[i].minimum = atof(minimum_element->GetText());
1269 const tinyxml2::XMLElement* maximum_element = statistics_element->FirstChildElement(
"Maximum");
1271 if(!maximum_element)
1273 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
1274 <<
"void from_XML(const tinyxml2::XMLElement*) method.\n"
1275 <<
"Maximum element " << i+1 <<
" is NULL.\n";
1277 throw std::logic_error(buffer.str());
1280 if(maximum_element->GetText())
1282 statistics[i].maximum = atof(maximum_element->GetText());
1287 const tinyxml2::XMLElement* mean_element = statistics_element->FirstChildElement(
"Mean");
1291 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
1292 <<
"void from_XML(const tinyxml2::XMLElement*) method.\n"
1293 <<
"Mean element " << i+1 <<
" is NULL.\n";
1295 throw std::logic_error(buffer.str());
1298 if(mean_element->GetText())
1300 statistics[i].mean = atof(mean_element->GetText());
1305 const tinyxml2::XMLElement* standard_deviation_element = statistics_element->FirstChildElement(
"StandardDeviation");
1307 if(!standard_deviation_element)
1309 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
1310 <<
"void from_XML(const tinyxml2::XMLElement*) method.\n"
1311 <<
"Standard deviation element " << i+1 <<
" is NULL.\n";
1313 throw std::logic_error(buffer.str());
1316 if(standard_deviation_element->GetText())
1318 statistics[i].standard_deviation = atof(standard_deviation_element->GetText());
1324 const tinyxml2::XMLElement* unscaling_method_element = root_element->FirstChildElement(
"UnscalingMethod");
1326 if(unscaling_method_element)
1328 const std::string new_method = unscaling_method_element->GetText();
1334 catch(
const std::logic_error& e)
1336 std::cout << e.what() << std::endl;
1342 const tinyxml2::XMLElement* element = root_element->FirstChildElement(
"Display");
1346 std::string new_display_string = element->GetText();
1352 catch(
const std::logic_error& e)
1354 std::cout << e.what() << std::endl;
1370 std::ostringstream buffer;
1372 for(
size_t i = 0; i < unscaling_neurons_number; i++)
1374 buffer << outputs_name[i] <<
"=" << inputs_name[i] <<
";\n";
1377 return(buffer.str());
1391 std::ostringstream buffer;
1393 for(
size_t i = 0; i < unscaling_neurons_number; i++)
1395 buffer << outputs_name[i] <<
"=0.5*(" << inputs_name[i] <<
"+1.0)*(" <<
statistics[i].maximum <<
"-" <<
statistics[i].minimum <<
")+" <<
statistics[i].minimum <<
";\n";
1398 return(buffer.str());
1412 std::ostringstream buffer;
1414 for(
size_t i = 0; i < unscaling_neurons_number; i++)
1416 buffer << outputs_name[i] <<
"=" <<
statistics[i].mean <<
"+" <<
statistics[i].standard_deviation <<
"*" << inputs_name[i] <<
";\n";
1419 return(buffer.str());
1439 case MinimumMaximum:
1445 case MeanStandardDeviation:
1453 std::ostringstream buffer;
1455 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
1456 <<
"std::string write_expression(const Vector<std::string>&, const Vector<std::string>&) const method.\n"
1457 <<
"Unknown unscaling method.\n";
1459 throw std::logic_error(buffer.str());
void set_item_statistics(const size_t &, const Statistics< double > &)
Vector< double > calculate_mean_standard_deviation_outputs(const Vector< double > &) const
Matrix< double > arrange_Jacobian(const Vector< double > &) const
Arranges a "Jacobian" matrix from the vector of derivatives.
bool is_empty(void) const
Returns true if the number of unscaling neurons is zero, and false otherwise.
void set_unscaling_method(const UnscalingMethod &)
void initialize_random(void)
std::string write_unscaling_method_text(void) const
bool display
Display warning messages to screen.
Vector< double > calculate_mean_standard_deviation_derivatives(const Vector< double > &) const
Vector< double > arrange_minimums(void) const
void set_minimum(const size_t &, const double &)
virtual ~UnscalingLayer(void)
Destructor.
std::string to_string(void) const
Returns a string representation of the current unscaling layer object.
void set(void)
Sets the size of a vector to zero.
void set_maximum(const size_t &, const double &)
void prune_unscaling_neuron(const size_t &)
const bool & get_display(void) const
virtual void set_default(void)
void set_diagonal(const T &)
Matrix< double > arrange_statistics(void) const
std::string write_unscaling_method(void) const
const UnscalingMethod & get_unscaling_method(void) const
void check_range(const Vector< double > &) const
Vector< double > calculate_second_derivatives(const Vector< double > &) const
Vector< double > calculate_derivatives(const Vector< double > &) const
Vector< double > calculate_mean_standard_deviation_second_derivatives(const Vector< double > &) const
UnscalingMethod unscaling_method
Unscaling method for the output variables.
UnscalingLayer(void)
Default constructor.
void set_mean(const size_t &, const double &)
UnscalingLayer & operator=(const UnscalingLayer &)
void set_statistics(const Vector< Statistics< double > > &)
Vector< Matrix< double > > arrange_Hessian_form(const Vector< double > &) const
Arranges a "Hessian form" vector of matrices from the vector of second derivatives.
std::string write_none_expression(const Vector< std::string > &, const Vector< std::string > &) const
void from_XML(const tinyxml2::XMLDocument &)
std::string write_expression(const Vector< std::string > &, const Vector< std::string > &) const
std::string write_minimum_maximum_expression(const Vector< std::string > &, const Vector< std::string > &) const
size_t get_unscaling_neurons_number(void) const
Returns the number of unscaling neurons in this layer.
Vector< double > calculate_outputs(const Vector< double > &) const
bool operator==(const UnscalingLayer &) const
Vector< Statistics< double > > get_statistics(void) const
Vector< double > calculate_minimum_maximum_second_derivatives(const Vector< double > &) const
Vector< double > calculate_minimum_maximum_outputs(const Vector< double > &) const
void set(void)
Sets the unscaling layer to be empty.
void set_standard_deviation(const size_t &, const double &)
void set_display(const bool &)
tinyxml2::XMLDocument * to_XML(void) const
Vector< double > arrange_maximums(void) const
Vector< Statistics< double > > statistics
Statistics of output variables.
void set_row(const size_t &, const Vector< T > &)
std::string write_mean_stadard_deviation_expression(const Vector< std::string > &, const Vector< std::string > &) const
UnscalingMethod
Enumeration of available methods for input variables, output variables and independent parameters sca...
Vector< double > calculate_minimum_maximum_derivatives(const Vector< double > &) const