9#include "unscaling_layer.h"
26 set(new_neurons_number);
34 set(new_descriptives);
83 Tensor<type, 1> minimums(neurons_number);
85 for(Index i = 0; i < neurons_number; i++)
101 Tensor<type, 1> maximums(neurons_number);
103 for(Index i = 0; i < neurons_number; i++)
127 ostringstream buffer;
129 buffer.precision(10);
131 for(Index i = 0; i < neurons_number; i++)
133 if(
scalers(i) == Scaler::NoScaling)
135 buffer << outputs_names(i) <<
" = " << inputs_names(i) <<
";\n";
137 else if(
scalers(i) == Scaler::MinimumMaximum)
141 else if(
scalers(i) == Scaler::MeanStandardDeviation)
143 buffer << outputs_names(i) <<
" = " <<
descriptives(i).minimum <<
"+0.5*(" << inputs_names(i) <<
"+1)*(" <<
descriptives(i).maximum <<
"-" <<
descriptives(i).minimum <<
");\n";
145 else if(
scalers(i) == Scaler::Logarithm)
147 buffer << outputs_names(i) <<
" = " <<
descriptives(i).minimum <<
"+0.5*(exp(" << inputs_names(i) <<
")+1)*(" <<
descriptives(i).maximum <<
"-" <<
descriptives(i).minimum <<
");\n";
151 ostringstream buffer;
153 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
154 <<
"string write_expression() const method.\n"
155 <<
"Unknown inputs scaling method.\n";
157 throw logic_error(buffer.str());
161 string expression = buffer.str();
163 replace(expression,
"+-",
"-");
164 replace(expression,
"--",
"+");
175 Tensor<string, 1> scaling_methods_strings(neurons_number);
177 for(Index i = 0; i < neurons_number; i++)
179 if(
scalers[i] == Scaler::NoScaling)
181 scaling_methods_strings[i] =
"NoScaling";
183 else if(
scalers[i] == Scaler::MinimumMaximum)
185 scaling_methods_strings[i] =
"MinimumMaximum";
187 else if(
scalers[i] == Scaler::MeanStandardDeviation)
189 scaling_methods_strings[i] =
"MeanStandardDeviation";
191 else if(
scalers[i] == Scaler::StandardDeviation)
193 scaling_methods_strings[i] =
"StandardDeviation";
195 else if(
scalers[i] == Scaler::Logarithm)
197 scaling_methods_strings[i] =
"Logarithm";
201 ostringstream buffer;
203 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
204 <<
"Tensor<string, 1> write_unscaling_method() const.\n"
205 <<
"Unknown unscaling method.\n";
207 throw logic_error(buffer.str());
211 return scaling_methods_strings;
222 Tensor<string, 1> scaling_methods_strings(neurons_number);
224 for(Index i = 0; i < neurons_number; i++)
226 if(
scalers[i] == Scaler::NoScaling)
228 scaling_methods_strings[i] =
"no unscaling";
230 else if(
scalers[i] == Scaler::MinimumMaximum)
232 scaling_methods_strings[i] =
"minimum and maximum";
234 else if(
scalers[i] == Scaler::MeanStandardDeviation)
236 scaling_methods_strings[i] =
"mean and standard deviation";
238 else if(
scalers[i] == Scaler::StandardDeviation)
240 scaling_methods_strings[i] =
"standard deviation";
242 else if(
scalers[i] == Scaler::Logarithm)
244 scaling_methods_strings[i] =
"logarithm";
248 ostringstream buffer;
250 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
251 <<
"Tensor<string, 1> write_unscaling_method() const.\n"
252 <<
"Unknown unscaling method.\n";
254 throw logic_error(buffer.str());
258 return scaling_methods_strings;
283void UnscalingLayer::set_inputs_number(
const Index& new_inputs_number)
289void UnscalingLayer::set_neurons_number(
const Index& new_neurons_number)
302 scalers.resize(new_neurons_number);
304 scalers.setConstant(Scaler::MinimumMaximum);
319 scalers.resize(new_descriptives.size());
321 scalers.setConstant(Scaler::MinimumMaximum);
327void UnscalingLayer::set(
const Tensor<Descriptives, 1>& new_descriptives,
const Tensor<Scaler, 1>& new_scalers)
342 from_XML(new_unscaling_layer_document);
401 const Index new_descriptives_size = new_descriptives.size();
403 if(new_descriptives_size != neurons_number)
405 ostringstream buffer;
407 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
408 <<
"void set_descriptives(const Tensor<Descriptives, 1>&) method.\n"
409 <<
"Size of descriptives (" << new_descriptives_size <<
") must be equal to number of unscaling neurons (" << neurons_number <<
").\n";
411 throw logic_error(buffer.str());
468 descriptives[i].set_standard_deviation(new_standard_deviation);
477 scalers = new_unscaling_method;
491 if(neurons_number == 0)
493 ostringstream buffer;
495 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
496 <<
"set_scalers(const string& new_scaling_methods_string) method.\n"
497 <<
"Neurons number (" << neurons_number <<
")must be greater than 0.\n";
499 throw logic_error(buffer.str());
504 if(new_scaling_methods_string ==
"NoScaling")
508 else if(new_scaling_methods_string ==
"MinimumMaximum")
512 else if(new_scaling_methods_string ==
"MeanStandardDeviation")
516 else if(new_scaling_methods_string ==
"StandardDeviation")
520 else if(new_scaling_methods_string ==
"Logarithm")
526 ostringstream buffer;
528 buffer <<
"OpenNN Exception: ScalingLayer class.\n"
529 <<
"set_scalers(const string& new_scaling_methods_string) method.\n";
531 throw logic_error(buffer.str());
546 if(neurons_number == 0)
548 ostringstream buffer;
550 buffer <<
"OpenNN Exception: ScalingLayer class.\n"
551 <<
"void set_scalers(const Tensor<string, 1>&) method.\n"
552 <<
"Neurons number (" << neurons_number <<
") must be greater than 0.\n";
554 throw logic_error(buffer.str());
559 Tensor<Scaler, 1> new_unscaling_methods(neurons_number);
561 for(Index i = 0; i < neurons_number; i++)
563 if(new_unscaling_methods_string(i) ==
"NoScaling")
565 new_unscaling_methods(i) = Scaler::NoScaling;
567 else if(new_unscaling_methods_string(i) ==
"MeanStandardDeviation")
569 new_unscaling_methods(i) = Scaler::MeanStandardDeviation;
571 else if(new_unscaling_methods_string(i) ==
"StandardDeviation")
573 new_unscaling_methods(i) = Scaler::StandardDeviation;
575 else if(new_unscaling_methods_string(i) ==
"MinimumMaximum")
577 new_unscaling_methods(i) = Scaler::MinimumMaximum;
579 else if(new_unscaling_methods_string(i) ==
"Logarithm")
581 new_unscaling_methods(i) = Scaler::Logarithm;
585 ostringstream buffer;
587 buffer <<
"OpenNN Exception: ScalingLayer class.\n"
588 <<
"void set_scalers(const Tensor<string, 1>&) method.\n"
589 <<
"Unknown scaling method: " << new_unscaling_methods_string(i) <<
".\n";
591 throw logic_error(buffer.str());
605 for(Index i = 0; i < neurons_number; i++)
607 scalers(i) = new_unscaling_method;
633 const Index size = outputs.size();
635 if(size != neurons_number)
637 ostringstream buffer;
639 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
640 <<
"void check_range(const Tensor<type, 1>&) const method.\n"
641 <<
"Size of outputs must be equal to number of unscaling neurons.\n";
643 throw logic_error(buffer.str());
652 for(Index i = 0; i < neurons_number; i++)
656 cout <<
"OpenNN Warning: UnscalingLayer class.\n"
657 <<
"void check_range(const Tensor<type, 1>&) const method.\n"
658 <<
"Output variable " << i <<
" is less than outputs.\n";
663 cout <<
"OpenNN Warning: UnscalingLayer class.\n"
664 <<
"void check_range(const Tensor<type, 1>&) const method.\n"
665 <<
"Output variable " << i <<
" is greater than maximum.\n";
678 if(neurons_number == 0)
694 Tensor<type, 2> outputs;
700 ostringstream buffer;
702 const Index columns_number = inputs.dimension(1);
704 if(columns_number != neurons_number)
706 buffer <<
"OpenNN Exception: ScalingLayer class.\n"
707 <<
"Tensor<type, 2> calculate_outputs(const Tensor<type, 2>&) const method.\n"
708 <<
"Size of inputs (" << columns_number <<
") must be equal to number of scaling neurons (" << neurons_number <<
").\n";
710 throw logic_error(buffer.str());
714 const Index points_number = inputs.dimension(0);
716 outputs.resize(points_number, neurons_number);
718 for(Index i = 0; i < points_number; i++)
720 for(Index j = 0; j < neurons_number; j++)
726 cout <<
"OpenNN Warning: ScalingLayer class.\n"
727 <<
"Tensor<type, 2> calculate_mean_standard_deviation_outputs(const Tensor<type, 2>&) const method.\n"
728 <<
"Standard deviation of variable " << i <<
" is zero.\n"
729 <<
"Those variables won't be scaled.\n";
732 outputs(j) = inputs(j);
736 if(
scalers(j) == Scaler::NoScaling)
738 outputs(i,j) = inputs(i,j);
741 else if(
scalers(j) == Scaler::MinimumMaximum)
747 outputs(i,j) = inputs(i,j)*slope + intercept;
750 else if(
scalers(j) == Scaler::MeanStandardDeviation)
756 outputs(i,j) = inputs(i,j)*slope + intercept;
758 else if(
scalers(j) == Scaler::StandardDeviation)
760 const type standard_deviation =
descriptives(j).standard_deviation;
762 outputs(i,j) = inputs(i,j)*standard_deviation;
764 else if(
scalers(j) == Scaler::Logarithm)
766 outputs(i,j) =
exp(inputs(i,j));
770 ostringstream buffer;
772 buffer <<
"OpenNN Exception: ScalingLayer class\n"
773 <<
"Tensor<type, 2> calculate_outputs(const Tensor<type, 2>&) const method.\n"
774 <<
"Unknown scaling method.\n";
776 throw logic_error(buffer.str());
791 ostringstream buffer;
797 file_stream.OpenElement(
"UnscalingLayer");
801 file_stream.OpenElement(
"UnscalingNeuronsNumber");
804 buffer << neurons_number;
806 file_stream.
PushText(buffer.str().c_str());
814 for(Index i = 0; i < neurons_number; i++)
816 file_stream.OpenElement(
"Descriptives");
822 file_stream.OpenElement(
"Minimum");
827 file_stream.
PushText(buffer.str().c_str());
833 file_stream.OpenElement(
"Maximum");
838 file_stream.
PushText(buffer.str().c_str());
844 file_stream.OpenElement(
"Mean");
849 file_stream.
PushText(buffer.str().c_str());
855 file_stream.OpenElement(
"StandardDeviation");
860 file_stream.
PushText(buffer.str().c_str());
866 file_stream.OpenElement(
"Scaler");
871 file_stream.
PushText(buffer.str().c_str());
893 ostringstream buffer;
899 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
900 <<
"void from_XML(const tinyxml2::XMLDocument&) method.\n"
901 <<
"Unscaling layer element is nullptr.\n";
903 throw logic_error(buffer.str());
908 const tinyxml2::XMLElement* neurons_number_element = root_element->FirstChildElement(
"UnscalingNeuronsNumber");
910 if(!neurons_number_element)
912 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
913 <<
"void from_XML(const tinyxml2::XMLDocument&) method.\n"
914 <<
"Unscaling neurons number element is nullptr.\n";
916 throw logic_error(buffer.str());
919 const Index neurons_number =
static_cast<Index
>(atoi(neurons_number_element->GetText()));
927 for(Index i = 0; i < neurons_number; i++)
930 start_element = descriptives_element;
932 if(!descriptives_element)
934 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
935 <<
"void from_XML(const tinyxml2::XMLElement*) method.\n"
936 <<
"Descriptives of unscaling neuron " << i+1 <<
" is nullptr.\n";
938 throw logic_error(buffer.str());
945 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
946 <<
"void from_XML(const tinyxml2::XMLElement*) method.\n"
947 <<
"Index " << index <<
" is not correct.\n";
949 throw logic_error(buffer.str());
954 const tinyxml2::XMLElement* minimum_element = descriptives_element->FirstChildElement(
"Minimum");
958 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
959 <<
"void from_XML(const tinyxml2::XMLElement*) method.\n"
960 <<
"Minimum element " << i+1 <<
" is nullptr.\n";
962 throw logic_error(buffer.str());
965 if(minimum_element->GetText())
967 descriptives(i).minimum =
static_cast<type
>(atof(minimum_element->GetText()));
972 const tinyxml2::XMLElement* maximum_element = descriptives_element->FirstChildElement(
"Maximum");
976 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
977 <<
"void from_XML(const tinyxml2::XMLElement*) method.\n"
978 <<
"Maximum element " << i+1 <<
" is nullptr.\n";
980 throw logic_error(buffer.str());
983 if(maximum_element->GetText())
985 descriptives(i).maximum =
static_cast<type
>(atof(maximum_element->GetText()));
994 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
995 <<
"void from_XML(const tinyxml2::XMLElement*) method.\n"
996 <<
"Mean element " << i+1 <<
" is nullptr.\n";
998 throw logic_error(buffer.str());
1001 if(mean_element->GetText())
1003 descriptives(i).mean =
static_cast<type
>(atof(mean_element->GetText()));
1008 const tinyxml2::XMLElement* standard_deviation_element = descriptives_element->FirstChildElement(
"StandardDeviation");
1010 if(!standard_deviation_element)
1012 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
1013 <<
"void from_XML(const tinyxml2::XMLElement*) method.\n"
1014 <<
"Standard deviation element " << i+1 <<
" is nullptr.\n";
1016 throw logic_error(buffer.str());
1019 if(standard_deviation_element->GetText())
1021 descriptives(i).standard_deviation =
static_cast<type
>(atof(standard_deviation_element->GetText()));
1026 const tinyxml2::XMLElement* unscaling_method_element = descriptives_element->FirstChildElement(
"Scaler");
1028 if(!unscaling_method_element)
1030 buffer <<
"OpenNN Exception: UnscalingLayer class.\n"
1031 <<
"void from_XML(const tinyxml2::XMLDocument&) method.\n"
1032 <<
"Unscaling method element " << i+1 <<
" is nullptr.\n";
1034 throw logic_error(buffer.str());
1037 const string new_method = unscaling_method_element->GetText();
1039 if(new_method ==
"NoScaling")
1041 scalers[i] = Scaler::NoScaling;
1043 else if(new_method ==
"MinimumMaximum")
1045 scalers[i] = Scaler::MinimumMaximum;
1047 else if(new_method ==
"MeanStandardDeviation")
1049 scalers[i] = Scaler::MeanStandardDeviation;
1051 else if(new_method ==
"StandardDeviation")
1053 scalers[i] = Scaler::StandardDeviation;
1055 else if(new_method ==
"Logarithm")
1057 scalers[i] = Scaler::Logarithm;
1067 string new_display_string = element->GetText();
1073 catch(
const logic_error& e)
1075 cerr << e.what() << endl;
1089 ostringstream buffer;
1091 buffer.precision(10);
1093 buffer <<
"vector<float> " <<
layer_name <<
"(const vector<float>& inputs)\n{" << endl;
1095 buffer <<
"\tvector<float> outputs(" << neurons_number <<
");\n" << endl;
1097 for(Index i = 0; i < neurons_number; i++)
1099 if(
scalers(i) == Scaler::NoScaling)
1101 buffer <<
"\toutputs[" << i <<
"] = inputs[" << i <<
"];" << endl;
1103 else if(
scalers(i) == Scaler::MinimumMaximum)
1107 buffer <<
"\toutputs[" << i <<
"] = " <<
descriptives(i).minimum <<
";\n";
1115 buffer <<
"\toutputs[" << i <<
"] = inputs[" << i <<
"]*"<<slope<<
"+"<<intercept<<
";\n";
1118 else if(
scalers(i) == Scaler::MeanStandardDeviation)
1120 const type standard_deviation =
descriptives(i).standard_deviation;
1124 buffer <<
"\toutputs[" << i <<
"] = inputs[" << i <<
"]*"<<standard_deviation<<
"+"<<mean<<
";\n";
1126 else if(
scalers(i) == Scaler::StandardDeviation)
1128 const type standard_deviation =
descriptives(i).standard_deviation;
1130 buffer <<
"\toutputs[" << i <<
"] = inputs[" << i <<
"]*"<<standard_deviation<<
";\n";
1132 else if(
scalers(i) == Scaler::Logarithm)
1134 buffer <<
"\toutputs[" << i <<
"] =exp( inputs[" << i <<
"]);";
1138 ostringstream buffer;
1140 buffer <<
"OpenNN Exception: ScalingLayer class.\n"
1141 <<
"string write_expression() const method.\n"
1142 <<
"Unknown inputs scaling method.\n";
1144 throw logic_error(buffer.str());
1148 buffer <<
"\n\treturn outputs;\n}" << endl;
1150 return buffer.str();
1162 ostringstream buffer;
1164 buffer.precision(10);
1166 buffer <<
"\tdef " <<
layer_name <<
"(self,inputs):\n" << endl;
1168 buffer <<
"\t\toutputs = [None] * "<<neurons_number<<
"\n" << endl;
1170 for(Index i = 0; i < neurons_number; i++)
1172 if(
scalers(i) == Scaler::NoScaling)
1174 buffer <<
"\t\toutputs[" << i <<
"] = inputs[" << i <<
"]" << endl;
1176 else if(
scalers(i) == Scaler::MinimumMaximum)
1180 buffer <<
"\toutputs[" << i <<
"] = " <<
descriptives(i).minimum <<
"\n";
1186 const type intercept
1189 buffer <<
"\t\toutputs[" << i <<
"] = inputs[" << i <<
"]*"<<slope<<
"+"<<intercept<<
"\n";
1192 else if(
scalers(i) == Scaler::MeanStandardDeviation)
1194 const type standard_deviation =
descriptives(i).standard_deviation;
1198 buffer <<
"\t\toutputs[" << i <<
"] = inputs[" << i <<
"]*"<<standard_deviation<<
"+"<<mean<<
"\n";
1200 else if(
scalers(i) == Scaler::StandardDeviation)
1202 const type standard_deviation =
descriptives(i).standard_deviation;
1204 buffer <<
"\t\toutputs[" << i <<
"] = inputs[" << i <<
"]*"<<standard_deviation<<
"\n";
1206 else if(
scalers(i) == Scaler::Logarithm)
1208 buffer <<
"\t\toutputs[" << i <<
"] = np.exp( inputs[" << i <<
"])\n";
1212 ostringstream buffer;
1214 buffer <<
"OpenNN Exception: ScalingLayer class.\n"
1215 <<
"string write_expression() const method.\n"
1216 <<
"Unknown inputs scaling method.\n";
1218 throw logic_error(buffer.str());
1222 buffer <<
"\n\t\treturn outputs\n" << endl;
1224 return buffer.str();
This abstract class represents the concept of layer of neurons in OpenNN.
string layer_name
Layer name.
Type layer_type
Layer type.
This class represents a layer of unscaling neurons.
string write_expression_c() const
void set_maximum(const Index &, const type &)
void set_descriptives(const Tensor< Descriptives, 1 > &)
void set_item_descriptives(const Index &, const Descriptives &)
void set_minimum(const Index &, const type &)
const bool & get_display() const
void set_standard_deviation(const Index &, const type &)
Index get_inputs_number() const
Returns the number of inputs.
string write_expression(const Tensor< string, 1 > &, const Tensor< string, 1 > &) const
Returns a string with the expression of the inputs scaling process.
void from_XML(const tinyxml2::XMLDocument &)
virtual void set_default()
const Tensor< Scaler, 1 > get_unscaling_method() const
string write_expression_python() const
Tensor< Scaler, 1 > scalers
Unscaling method for the output variables.
void check_range(const Tensor< type, 1 > &) const
bool display
Display warning messages to screen.
bool is_empty() const
Returns true if the number of unscaling neurons is zero, and false otherwise.
void set()
Sets the unscaling layer to be empty.
Tensor< type, 1 > get_minimums() const
Tensor< type, 1 > get_maximums() const
virtual ~UnscalingLayer()
Destructor.
Tensor< string, 1 > write_unscaling_method_text() const
void set_scalers(const Tensor< Scaler, 1 > &)
Tensor< type, 2 > calculate_outputs(const Tensor< type, 2 > &)
Index get_neurons_number() const
Returns the number of unscaling neurons in this layer.
Tensor< Descriptives, 1 > get_descriptives() const
UnscalingLayer()
Default constructor.
type min_range
min and max range for unscaling
Tensor< Descriptives, 1 > descriptives
Descriptives of output variables.
void set_display(const bool &)
void write_XML(tinyxml2::XMLPrinter &) const
void set_mean(const Index &, const type &)
void set_min_max_range(const type min, const type max)
Tensor< string, 1 > write_unscaling_methods() const
Returns a vector of strings with the name of the method used for each unscaling neuron.
XMLError QueryUnsignedAttribute(const char *name, unsigned int *value) const
See QueryIntAttribute()
const XMLElement * NextSiblingElement(const char *name=nullptr) const
Get the next(right) sibling element of this node, with an optionally supplied name.
void PushText(const char *text, bool cdata=false)
Add a text node.
void PushAttribute(const char *name, const char *value)
If streaming, add an attribute to an open element.
virtual void CloseElement(bool compactMode=false)
If streaming, close the Element.
HALF_CONSTEXPR half abs(half arg)
This structure contains the simplest Descriptives for a set, variable, etc. It includes :