16 #include "scaling_layer.h"
41 set(new_scaling_neurons_number);
64 set(new_scaling_layer);
86 if(
this != &other_scaling_layer)
166 for(
size_t i = 0; i < scaling_neurons_number; i++)
171 return(statistics_matrix);
185 for(
size_t i = 0; i < scaling_neurons_number; i++)
204 for(
size_t i = 0; i < scaling_neurons_number; i++)
206 standard_deviations[i] =
statistics[i].standard_deviation;
209 return(standard_deviations);
231 return(
"MeanStandardDeviation");
235 return(
"MinimumMaximum");
243 std::ostringstream buffer;
245 buffer <<
"OpenNN Exception: ScalingLayer class.\n"
246 <<
"std::string write_scaling_method(void) const method.\n"
247 <<
"Unknown scaling method.\n";
249 throw std::logic_error(buffer.str());
263 return(
"no scaling");
267 return(
"mean and standard deviation");
271 return(
"minimum and maximum");
275 std::ostringstream buffer;
277 buffer <<
"OpenNN Exception: ScalingLayer class.\n"
278 <<
"std::string write_scaling_method_text(void) const method.\n"
279 <<
"Unknown scaling method.\n";
281 throw std::logic_error(buffer.str());
346 from_XML(new_scaling_layer_document);
402 const size_t new_statistics_size = new_statistics.size();
406 if(new_statistics_size != scaling_neurons_number)
408 std::ostringstream buffer;
410 buffer <<
"OpenNN Exception: ScalingLayer class.\n"
411 <<
"void set_statistics(const Vector< Statistics<double> >&) method.\n"
412 <<
"Size of statistics is not equal to number of scaling neurons.\n";
414 throw std::logic_error(buffer.str());
481 statistics[i].set_standard_deviation(new_standard_deviation);
504 if(new_scaling_method ==
"NoScaling")
508 else if(new_scaling_method ==
"MeanStandardDeviation")
512 else if(new_scaling_method ==
"MinimumMaximum")
518 std::ostringstream buffer;
520 buffer <<
"OpenNN Exception: ScalingLayer class.\n"
521 <<
"void set_scaling_method(const std::string&) method.\n"
522 <<
"Unknown scaling method: " << new_scaling_method <<
".\n";
524 throw std::logic_error(buffer.str());
555 if(index >= scaling_neurons_number)
557 std::ostringstream buffer;
559 buffer <<
"OpenNN Exception: ScalingLayer class.\n"
560 <<
"void prune_scaling_neuron(const size_t&) method.\n"
561 <<
"Index of scaling neuron is equal or greater than number of scaling neurons.\n";
563 throw std::logic_error(buffer.str());
580 if(inputs_number == 0)
607 const size_t size = inputs.size();
609 if(size != inputs_number)
611 std::ostringstream buffer;
613 buffer <<
"OpenNN Exception: ScalingLayer class.\n"
614 <<
"void check_range(const Vector<double>&) const method.\n"
615 <<
"Size of inputs must be equal to number of inputs.\n";
617 throw std::logic_error(buffer.str());
626 for(
size_t i = 0; i < inputs_number; i++)
630 std::cout <<
"OpenNN Warning: ScalingLayer class.\n"
631 <<
"void check_range(const Vector<double>&) const method.\n"
632 <<
"Input value " << i <<
" is less than corresponding minimum.\n";
637 std::cout <<
"OpenNN Warning: ScalingLayer class.\n"
638 <<
"void check_range(const Vector<double>&) const method.\n"
639 <<
"Input value " << i <<
" is greater than corresponding maximum.\n";
657 for(
size_t i = 0; i < scaling_neurons_number; i++)
680 std::ostringstream buffer;
682 buffer <<
"OpenNN Exception: ScalingLayer class.\n"
683 <<
"void initialize_random(void) method.\n"
684 <<
"Unknown scaling method.\n";
686 throw std::logic_error(buffer.str());
704 std::ostringstream buffer;
708 const size_t size = inputs.size();
710 if(size != inputs_number)
712 buffer <<
"OpenNN Exception: ScalingLayer class.\n"
713 <<
"Vector<double> calculate_outputs(const Vector<double>&) const method.\n"
714 <<
"Size of inputs must be equal to number of scaling neurons.\n";
716 throw std::logic_error(buffer.str());
729 case MeanStandardDeviation:
743 std::ostringstream buffer;
745 buffer <<
"OpenNN Exception: ScalingLayer class\n"
746 <<
"Vector<double> calculate_outputs(const Vector<double>&) const method.\n"
747 <<
"Unknown scaling and unscaling method.\n";
749 throw std::logic_error(buffer.str());
775 case MeanStandardDeviation:
783 std::ostringstream buffer;
785 buffer <<
"OpenNN Exception: ScalingLayer class.\n"
786 <<
"Vector<double> calculate_derivatives(const Vector<double>&) const method.\n"
787 <<
"Unknown scaling and unscaling method.\n";
789 throw std::logic_error(buffer.str());
811 case MeanStandardDeviation:
819 std::ostringstream buffer;
821 buffer <<
"OpenNN Exception: ScalingLayer class.\n"
822 <<
"Vector<double> calculate_second_derivatives(const Vector<double>&) const method.\n"
823 <<
"Unknown scaling and unscaling method.\n";
825 throw std::logic_error(buffer.str());
844 for(
size_t i = 0; i < scaling_neurons_number; i++)
850 std::cout <<
"OpenNN Warning: ScalingLayer class\n"
851 <<
"Vector<double> calculate_minimum_maximum_outputs(Vector<double>&) const method.\n"
852 <<
"Minimum and maximum values of variable " << i <<
" are equal.\n"
853 <<
"Those inputs won't be scaled.\n";
856 outputs[i] = inputs[i];
879 for(
size_t i = 0; i < scaling_neurons_number; i++)
885 std::cout <<
"OpenNN Warning: ScalingLayer class.\n"
886 <<
"Vector<double> calculate_minimum_maximum_derivatives(const Vector<double>&) const method.\n"
887 <<
"Minimum and maximum values of variable " << i <<
" are equal.\n"
888 <<
"That inputs is not scaled.\n";
891 scaled_derivative[i] = 1.0;
899 return(scaled_derivative);
912 const Vector<double> scaled_second_derivative(scaling_neurons_number, 0.0);
914 return(scaled_second_derivative);
929 for(
size_t i = 0; i < scaling_neurons_number; i++)
935 std::cout <<
"OpenNN Warning: ScalingLayer class.\n"
936 <<
"Vector<double> calculate_mean_standard_deviation_outputs(const Vector<double>&) const method.\n"
937 <<
"Standard deviation of variable " << i <<
" is zero.\n"
938 <<
"Those variables won't be scaled.\n";
941 outputs[i] = inputs[i];
964 for(
size_t i = 0; i < scaling_neurons_number; i++)
970 std::cout <<
"OpenNN Warning: ScalingLayer class.\n"
971 <<
"Vector<double> calculate_mean_standard_deviation_derivatives(const Vector<double>&) const method.\n"
972 <<
"Standard deviation of input variable " << i <<
" is zero.\n"
973 <<
"That inputs is not be scaled.\n";
980 derivative[i] = 1.0/
statistics[i].standard_deviation;
997 const Vector<double> second_derivative(scaling_neurons_number, 0.0);
999 return(second_derivative);
1011 Matrix<double> Jacobian(scaling_neurons_number, scaling_neurons_number, 0.0);
1029 for(
size_t i = 0; i < scaling_neurons_number; i++)
1031 Hessian_form[i].
set(scaling_neurons_number, scaling_neurons_number, 0.0);
1033 Hessian_form[i](i,i) = second_derivative[i];
1036 return(Hessian_form);
1050 std::ostringstream buffer;
1052 for(
size_t i = 0; i < inputs_number; i++)
1054 buffer << outputs_name[i] <<
"=" << inputs_name[i] <<
";\n";
1057 return(buffer.str());
1072 std::ostringstream buffer;
1074 for(
size_t i = 0; i < inputs_number; i++)
1076 buffer << outputs_name[i] <<
"=2*(" << inputs_name[i] <<
"-" <<
statistics[i].minimum <<
")/(" <<
statistics[i].maximum <<
"-" <<
statistics[i].minimum <<
")-1;\n";
1079 return(buffer.str());
1093 std::ostringstream buffer;
1095 for(
size_t i = 0; i < inputs_number; i++)
1097 buffer << outputs_name[i] <<
"=(" << inputs_name[i] <<
"-" <<
statistics[i].mean <<
")/" <<
statistics[i].standard_deviation <<
";\n";
1100 return(buffer.str());
1118 case MinimumMaximum:
1124 case MeanStandardDeviation:
1132 std::ostringstream buffer;
1134 buffer <<
"OpenNN Exception: ScalingLayer class.\n"
1135 <<
"std::string write_expression(void) const method.\n"
1136 <<
"Unknown inputs scaling method.\n";
1138 throw std::logic_error(buffer.str());
1151 std::ostringstream buffer;
1155 buffer <<
"Scaling layer\n";
1157 for(
size_t i = 0; i < scaling_neurons_number; i++)
1159 buffer <<
"Statistics " << i+1 <<
"\n"
1160 <<
"Minimum: " <<
statistics[i].minimum <<
"\n"
1161 <<
"Maximum: " <<
statistics[i].maximum <<
"\n"
1163 <<
"Standard deviation: " <<
statistics[i].standard_deviation <<
"\n";
1167 <<
"Display: " <<
display <<
"\n";
1169 return(buffer.str());
1180 tinyxml2::XMLDocument* document =
new tinyxml2::XMLDocument;
1182 std::ostringstream buffer;
1184 tinyxml2::XMLElement* scaling_layer_element = document->NewElement(
"ScalingLayer");
1186 document->InsertFirstChild(scaling_layer_element);
1190 tinyxml2::XMLElement* size_element = document->NewElement(
"ScalingNeuronsNumber");
1191 scaling_layer_element->LinkEndChild(size_element);
1196 buffer << scaling_neurons_number;
1198 tinyxml2::XMLText* size_text = document->NewText(buffer.str().c_str());
1199 size_element->LinkEndChild(size_text);
1201 for(
size_t i = 0; i < scaling_neurons_number; i++)
1203 tinyxml2::XMLElement* statistics_element = document->NewElement(
"Statistics");
1204 statistics_element->SetAttribute(
"Index", (
unsigned)i+1);
1206 scaling_layer_element->LinkEndChild(statistics_element);
1210 tinyxml2::XMLElement* minimum_element = document->NewElement(
"Minimum");
1211 statistics_element->LinkEndChild(minimum_element);
1216 tinyxml2::XMLText* minimum_text = document->NewText(buffer.str().c_str());
1217 minimum_element->LinkEndChild(minimum_text);
1221 tinyxml2::XMLElement* maximum_element = document->NewElement(
"Maximum");
1222 statistics_element->LinkEndChild(maximum_element);
1227 tinyxml2::XMLText* maximum_text = document->NewText(buffer.str().c_str());
1228 maximum_element->LinkEndChild(maximum_text);
1232 tinyxml2::XMLElement* mean_element = document->NewElement(
"Mean");
1233 statistics_element->LinkEndChild(mean_element);
1238 tinyxml2::XMLText* mean_text = document->NewText(buffer.str().c_str());
1239 mean_element->LinkEndChild(mean_text);
1243 tinyxml2::XMLElement* standard_deviation_element = document->NewElement(
"StandardDeviation");
1244 statistics_element->LinkEndChild(standard_deviation_element);
1249 tinyxml2::XMLText* standard_deviation_text = document->NewText(buffer.str().c_str());
1250 standard_deviation_element->LinkEndChild(standard_deviation_text);
1255 tinyxml2::XMLElement* method_element = document->NewElement(
"ScalingMethod");
1256 scaling_layer_element->LinkEndChild(method_element);
1259 method_element->LinkEndChild(method_text);
1263 tinyxml2::XMLElement* display_element = document->NewElement(
"Display");
1264 scaling_layer_element->LinkEndChild(display_element);
1269 tinyxml2::XMLText* display_text = document->NewText(buffer.str().c_str());
1270 display_element->LinkEndChild(display_text);
1283 std::ostringstream buffer;
1285 const tinyxml2::XMLElement* scaling_layer_element = document.FirstChildElement(
"ScalingLayer");
1287 if(!scaling_layer_element)
1289 buffer <<
"OpenNN Exception: ScalingLayer class.\n"
1290 <<
"void from_XML(const tinyxml2::XMLDocument&) method.\n"
1291 <<
"Scaling layer element is NULL.\n";
1293 throw std::logic_error(buffer.str());
1298 const tinyxml2::XMLElement* scaling_neurons_number_element = scaling_layer_element->FirstChildElement(
"ScalingNeuronsNumber");
1300 if(!scaling_neurons_number_element)
1302 buffer <<
"OpenNN Exception: ScalingLayer class.\n"
1303 <<
"void from_XML(const tinyxml2::XMLDocument&) method.\n"
1304 <<
"Scaling neurons number element is NULL.\n";
1306 throw std::logic_error(buffer.str());
1309 const size_t scaling_neurons_number = atoi(scaling_neurons_number_element->GetText());
1311 set(scaling_neurons_number);
1315 const tinyxml2::XMLElement* start_element = scaling_neurons_number_element;
1317 for(
size_t i = 0; i < scaling_neurons_number; i++)
1319 const tinyxml2::XMLElement* statistics_element = start_element->NextSiblingElement(
"Statistics");
1320 start_element = statistics_element;
1322 if(!statistics_element)
1324 buffer <<
"OpenNN Exception: ScalingLayer class.\n"
1325 <<
"void from_XML(const tinyxml2::XMLDocument&) method.\n"
1326 <<
"Statistics of scaling neuron " << i+1 <<
" is NULL.\n";
1328 throw std::logic_error(buffer.str());
1331 statistics_element->QueryUnsignedAttribute(
"Index", &index);
1335 buffer <<
"OpenNN Exception: ScalingLayer class.\n"
1336 <<
"void from_XML(const tinyxml2::XMLDocument&) method.\n"
1337 <<
"Index " << index <<
" is not correct.\n";
1339 throw std::logic_error(buffer.str());
1344 const tinyxml2::XMLElement* minimum_element = statistics_element->FirstChildElement(
"Minimum");
1346 if(!minimum_element)
1348 buffer <<
"OpenNN Exception: ScalingLayer class.\n"
1349 <<
"void from_XML(const tinyxml2::XMLDocument&) method.\n"
1350 <<
"Minimum element " << i+1 <<
" is NULL.\n";
1352 throw std::logic_error(buffer.str());
1355 if(minimum_element->GetText())
1357 statistics[i].minimum = atof(minimum_element->GetText());
1362 const tinyxml2::XMLElement* maximum_element = statistics_element->FirstChildElement(
"Maximum");
1364 if(!maximum_element)
1366 buffer <<
"OpenNN Exception: ScalingLayer class.\n"
1367 <<
"void from_XML(const tinyxml2::XMLDocument&) method.\n"
1368 <<
"Maximum element " << i+1 <<
" is NULL.\n";
1370 throw std::logic_error(buffer.str());
1373 if(maximum_element->GetText())
1375 statistics[i].maximum = atof(maximum_element->GetText());
1380 const tinyxml2::XMLElement* mean_element = statistics_element->FirstChildElement(
"Mean");
1384 buffer <<
"OpenNN Exception: ScalingLayer class.\n"
1385 <<
"void from_XML(const tinyxml2::XMLDocument&) method.\n"
1386 <<
"Mean element " << i+1 <<
" is NULL.\n";
1388 throw std::logic_error(buffer.str());
1391 if(mean_element->GetText())
1393 statistics[i].mean = atof(mean_element->GetText());
1398 const tinyxml2::XMLElement* standard_deviation_element = statistics_element->FirstChildElement(
"StandardDeviation");
1400 if(!standard_deviation_element)
1402 buffer <<
"OpenNN Exception: ScalingLayer class.\n"
1403 <<
"void from_XML(const tinyxml2::XMLDocument&) method.\n"
1404 <<
"Standard deviation element " << i+1 <<
" is NULL.\n";
1406 throw std::logic_error(buffer.str());
1409 if(standard_deviation_element->GetText())
1411 statistics[i].standard_deviation = atof(standard_deviation_element->GetText());
1417 const tinyxml2::XMLElement* scaling_method_element = scaling_layer_element->FirstChildElement(
"ScalingMethod");
1419 if(scaling_method_element)
1421 std::string new_method = scaling_method_element->GetText();
1427 catch(
const std::logic_error& e)
1429 std::cout << e.what() << std::endl;
1436 const tinyxml2::XMLElement* display_element = scaling_layer_element->FirstChildElement(
"Display");
1440 std::string new_display_string = display_element->GetText();
1446 catch(
const std::logic_error& e)
1448 std::cout << e.what() << std::endl;
Vector< double > calculate_outputs(const Vector< double > &) const
void prune_scaling_neuron(const size_t &)
void set_item_statistics(const size_t &, const Statistics< double > &)
std::string write_scaling_method(void) const
Returns a string with the name of the method used for scaling.
size_t get_scaling_neurons_number(void) const
Returns the number of unscaling neurons in this layer.
Vector< double > calculate_mean_standard_deviation_derivatives(const Vector< double > &) const
const ScalingMethod & get_scaling_method(void) const
Returns the method used for scaling.
std::string write_scaling_method_text(void) const
Vector< Statistics< double > > statistics
Statistics of input variables.
void check_range(const Vector< double > &) const
ScalingMethod scaling_method
Method for scaling the input variables.
virtual void from_XML(const tinyxml2::XMLDocument &)
void set(void)
Sets the size of a vector to zero.
Vector< Statistics< double > > get_statistics(void) const
void set(void)
Sets the scaling layer to be empty.
Vector< double > calculate_minimum_maximum_derivatives(const Vector< double > &) const
void set_diagonal(const T &)
void set_mean(const size_t &, const double &)
void set_statistics(const Vector< Statistics< double > > &)
bool operator==(const ScalingLayer &) const
void set_display(const bool &)
ScalingMethod
Enumeration of available methods for scaling the input variables.
void set_scaling_method(const ScalingMethod &)
Vector< double > arrange_means(void) const
Returns a single matrix with the means of all scaling neurons.
Vector< double > calculate_minimum_maximum_outputs(const Vector< double > &) const
Vector< double > calculate_minimum_maximum_second_derivatives(const Vector< double > &) const
tinyxml2::XMLDocument * to_XML(void) const
const bool & get_display(void) const
void set_maximum(const size_t &, const double &)
std::string write_mean_standard_deviation_expression(const Vector< std::string > &, const Vector< std::string > &) const
Vector< double > arrange_standard_deviations(void) const
Returns a single matrix with the standard deviations of all scaling neurons.
virtual void set_default(void)
Vector< Matrix< double > > arrange_Hessian_form(const Vector< double > &) const
Arranges a "Hessian form" vector of matrices from the vector of second derivatives.
bool display
Display warning messages to screen.
std::string to_string(void) const
Returns a string representation of the current scaling layer object.
virtual ~ScalingLayer(void)
Destructor.
void initialize_random(void)
Matrix< double > arrange_Jacobian(const Vector< double > &) const
Arranges a "Jacobian" matrix from the vector of derivatives.
Vector< double > calculate_derivatives(const Vector< double > &) const
std::string write_no_scaling_expression(const Vector< std::string > &, const Vector< std::string > &) const
Vector< double > calculate_mean_standard_deviation_outputs(const Vector< double > &) const
std::string write_expression(const Vector< std::string > &, const Vector< std::string > &) const
Returns a string with the expression of the inputs scaling process.
Matrix< double > arrange_statistics(void) const
std::string write_minimum_maximum_expression(const Vector< std::string > &, const Vector< std::string > &) const
Vector< double > calculate_second_derivatives(const Vector< double > &) const
Vector< double > calculate_mean_standard_deviation_second_derivatives(const Vector< double > &) const
void set_standard_deviation(const size_t &, const double &)
void set_minimum(const size_t &, const double &)
void set_row(const size_t &, const Vector< T > &)
ScalingLayer & operator=(const ScalingLayer &)
bool is_empty(void) const
Returns true if the number of scaling neurons is zero, and false otherwise.