16 #include "sum_squared_error.h"
120 std::ostringstream buffer;
126 buffer <<
"OpenNN Exception: SumSquaredError class.\n"
127 <<
"void check(void) const method.\n"
128 <<
"Pointer to neural network is NULL.\n";
130 throw std::logic_error(buffer.str());
135 if(!multilayer_perceptron_pointer)
137 buffer <<
"OpenNN Exception: SumSquaredError class.\n"
138 <<
"void check(void) const method.\n"
139 <<
"Pointer to multilayer perceptron is NULL.\n";
141 throw std::logic_error(buffer.str());
151 buffer <<
"OpenNN Exception: SumSquaredError class.\n"
152 <<
"void check(void) const method.\n"
153 <<
"Pointer to data set is NULL.\n";
155 throw std::logic_error(buffer.str());
165 if(data_set_inputs_number != inputs_number)
167 buffer <<
"OpenNN Exception: SumSquaredError class.\n"
168 <<
"void check(void) const method.\n"
169 <<
"Number of inputs in neural network must be equal to number of inputs in data set.\n";
171 throw std::logic_error(buffer.str());
174 if(outputs_number != targets_number)
176 buffer <<
"OpenNN Exception: SumSquaredError class.\n"
177 <<
"void check(void) const method.\n"
178 <<
"Number of outputs in neural network must be equal to number of targets in data set.\n";
180 throw std::logic_error(buffer.str());
212 size_t training_index;
228 double sum_squared_error = 0.0;
230 #pragma omp parallel for private(i, training_index, inputs, outputs, targets) reduction(+ : sum_squared_error)
232 for(i = 0; i < (int)training_instances_number; i++)
234 training_index = training_indices[i];
258 return(sum_squared_error);
282 std::ostringstream buffer;
284 const size_t size = parameters.size();
288 if(size != parameters_number)
290 buffer <<
"OpenNN Exception: SumSquaredError class." << std::endl
291 <<
"double calculate_performance(const Vector<double>&) const method." << std::endl
292 <<
"Size (" << size <<
") must be equal to number of parameters (" << parameters_number <<
")." << std::endl;
294 throw std::logic_error(buffer.str());
314 size_t training_index;
331 double sum_squared_error = 0.0;
333 #pragma omp parallel for private(i, training_index, inputs, outputs, targets) reduction(+ : sum_squared_error)
335 for(i = 0; i < (int)training_instances_number; i++)
337 training_index = training_indices[i];
361 return(sum_squared_error);
391 size_t generalization_index;
406 double generalization_performance = 0.0;
410 #pragma omp parallel for private(i, generalization_index, inputs, outputs, targets) reduction(+ : generalization_performance)
412 for(i = 0; i < (int)generalization_instances_number; i++)
414 generalization_index = generalization_indices[i];
438 return(generalization_performance);
485 size_t training_index;
512 #pragma omp parallel for private(i, training_index, inputs, targets, first_order_forward_propagation, layers_inputs, layers_combination_parameters_Jacobian,\
513 output_gradient, layers_delta, particular_solution, homogeneous_solution, point_gradient)
515 for(i = 0; i < (int)training_instances_number; i++)
517 training_index = training_indices[i];
537 if(!has_conditions_layer)
539 output_gradient = (layers_activation[layers_number-1]-targets)*2.0;
548 output_gradient = (particular_solution+homogeneous_solution*layers_activation[layers_number-1] - targets)*2.0;
550 layers_delta =
calculate_layers_delta(layers_activation_derivative, homogeneous_solution, output_gradient);
556 gradient += point_gradient;
609 size_t training_index;
629 Matrix<double> Hessian(parameters_number, parameters_number, 0.0);
631 for(
size_t i = 0; i < training_instances_number; i++)
633 training_index = training_indices[i];
652 layers_inputs[0] = inputs;
654 for(
size_t j = 1; j < layers_number; j++)
656 layers_inputs[j] = layers_activation[j-1];
663 if(!has_conditions_layer)
665 output_gradient = (layers_activation[layers_number-1] - targets)*2.0;
669 interlayers_Delta =
calculate_interlayers_Delta(layers_activation_derivative, layers_activation_second_derivative, interlayers_combination_combination_Jacobian, output_gradient, output_Hessian, layers_delta);
676 output_gradient = (particular_solution+homogeneous_solution*layers_activation[layers_number-1] - targets)*2.0;
678 layers_delta =
calculate_layers_delta(layers_activation_derivative, homogeneous_solution, output_gradient);
681 Hessian +=
calculate_point_Hessian(layers_activation_derivative, perceptrons_combination_parameters_gradient, interlayers_combination_combination_Jacobian, layers_delta, interlayers_Delta);
717 size_t training_index;
736 #pragma omp parallel for private(i, training_index, inputs, outputs, targets)
738 for(i = 0; i < (int)training_instances_number; i++)
740 training_index = training_indices[i];
764 return(performance_terms);
786 const size_t size = parameters.size();
790 if(size != parameters_number)
792 std::ostringstream buffer;
794 buffer <<
"OpenNN Exception: SumSquaredError class." << std::endl
795 <<
"double calculate_terms(const Vector<double>&) const method." << std::endl
796 <<
"Size (" << size <<
") must be equal to number of neural network parameters (" << parameters_number <<
")." << std::endl;
798 throw std::logic_error(buffer.str());
857 size_t training_index;
879 Matrix<double> terms_Jacobian(training_instances_number, neural_parameters_number);
885 #pragma omp parallel for private(i, training_index, inputs, targets, first_order_forward_propagation, layers_inputs, \
886 layers_combination_parameters_Jacobian, term, term_norm, output_gradient, layers_delta, particular_solution, homogeneous_solution, point_gradient)
888 for(i = 0; i < (int)training_instances_number; i++)
890 training_index = training_indices[i];
903 layers_inputs = multilayer_perceptron_pointer->
arrange_layers_input(inputs, first_order_forward_propagation);
909 if(!has_conditions_layer)
913 term = first_order_forward_propagation[0][layers_number-1] - targets;
918 output_gradient.
set(outputs_number, 0.0);
922 output_gradient = term/term_norm;
935 term = (particular_solution+homogeneous_solution*first_order_forward_propagation[0][layers_number-1] - targets);
940 output_gradient.
set(outputs_number, 0.0);
944 output_gradient = term/term_norm;
947 layers_delta =
calculate_layers_delta(first_order_forward_propagation[1], homogeneous_solution, output_gradient);
952 terms_Jacobian.
set_row(i, point_gradient);
955 return(terms_Jacobian);
971 return(first_order_terms);
1004 size_t training_index;
1023 #pragma omp parallel for private(i, training_index, inputs, outputs, targets)
1025 for(i = 0; i < (int)training_instances_number; i++)
1027 training_index = training_indices[i];
1051 return(squared_errors);
1085 return(
"SUM_SQUARED_ERROR");
1095 std::ostringstream buffer;
1097 tinyxml2::XMLDocument* document =
new tinyxml2::XMLDocument;
1101 tinyxml2::XMLElement* root_element = document->NewElement(
"SumSquaredError");
1103 document->InsertFirstChild(root_element);
1108 tinyxml2::XMLElement* display_element = document->NewElement(
"Display");
1109 root_element->LinkEndChild(display_element);
1114 tinyxml2::XMLText* display_text = document->NewText(buffer.str().c_str());
1115 display_element->LinkEndChild(display_text);
1129 const tinyxml2::XMLElement* root_element = document.FirstChildElement(
"SumSquaredError");
1133 std::ostringstream buffer;
1135 buffer <<
"OpenNN Exception: SumSquaredError class.\n"
1136 <<
"void from_XML(const tinyxml2::XMLDocument&) method.\n"
1137 <<
"Sum squared error element is NULL.\n";
1139 throw std::logic_error(buffer.str());
1144 const tinyxml2::XMLElement* element = root_element->FirstChildElement(
"Display");
1148 const std::string new_display_string = element->GetText();
1154 catch(
const std::logic_error& e)
1156 std::cout << e.what() << std::endl;
size_t count_parameters_number(void) const
double calculate_performance(void) const
Returns the performance value of a neural network according to the sum squared error on a data set...
double calculate_generalization_performance(void) const
Returns the sum squared error of the neural network measured on the generalization instances of the d...
const Variables & get_variables(void) const
Returns a constant reference to the variables object composing this data set object.
tinyxml2::XMLDocument * to_XML(void) const
Returns a representation of the sum squared error object, in XML format.
size_t count_training_instances_number(void) const
Returns the number of instances in the data set which will be used for training.
size_t get_inputs_number(void) const
Returns the number of inputs to the multilayer perceptron.
virtual ~SumSquaredError(void)
Destructor.
Matrix< Matrix< double > > calculate_interlayers_combination_combination_Jacobian(const Vector< double > &) const
void set(void)
Sets the size of a vector to zero.
size_t get_layers_number(void) const
Returns the number of layers in the multilayer perceptron.
bool has_missing_values(void) const
void initialize_diagonal(const size_t &, const T &)
Vector< size_t > arrange_layers_perceptrons_numbers(void) const
Returns a vector with the size of each layer.
size_t get_outputs_number(void) const
Returns the number of outputs neurons in the multilayer perceptron.
Vector< double > calculate_outputs(const Vector< double > &) const
Vector< double > calculate_gradient(void) const
const MissingValues & get_missing_values(void) const
Returns a reference to the missing values object in the data set.
Vector< Vector< Vector< double > > > calculate_perceptrons_combination_parameters_gradient(const Vector< Vector< double > > &) const
Vector< size_t > arrange_targets_indices(void) const
Returns the indices of the target variables.
Vector< size_t > arrange_training_indices(void) const
Returns the indices of the instances which will be used for training.
size_t count_generalization_instances_number(void) const
Returns the number of instances in the data set which will be used for generalization.
Vector< double > get_instance(const size_t &) const
bool has_conditions_layer(void) const
Matrix< double > calculate_Hessian(void) const
Matrix< double > calculate_terms_Jacobian(void) const
MultilayerPerceptron * get_multilayer_perceptron_pointer(void) const
Returns a pointer to the multilayer perceptron composing this neural network.
double calculate_norm(void) const
Returns the vector norm.
double calculate_sum_squared_error(const Vector< double > &) const
virtual Vector< double > calculate_homogeneous_solution(const Vector< double > &) const
Returns the homogeneous solution for applying boundary conditions.
Vector< Matrix< double > > calculate_layers_combination_parameters_Jacobian(const Vector< Vector< double > > &) const
virtual Vector< double > calculate_particular_solution(const Vector< double > &) const
Returns the particular solution for applying boundary conditions.
void from_XML(const tinyxml2::XMLDocument &)
double calculate_distance(const Vector< double > &) const
ConditionsLayer * get_conditions_layer_pointer(void) const
Returns a pointer to the conditions layer composing this neural network.
size_t count_inputs_number(void) const
Returns the number of input variables of the data set.
Vector< Vector< Vector< double > > > calculate_first_order_forward_propagation(const Vector< double > &) const
size_t count_targets_number(void) const
Returns the number of target variables of the data set.
Vector< double > calculate_terms(void) const
Calculates the squared error terms for each instance, and returns it in a vector of size the number t...
PerformanceTerm::FirstOrderTerms calculate_first_order_terms(void) const
std::string write_performance_term_type(void) const
Returns a string with the name of the sum squared error performance type, "SUM_SQUARED_ERROR".
void set_row(const size_t &, const Vector< T > &)
Vector< Vector< Vector< double > > > calculate_second_order_forward_propagation(const Vector< double > &) const
Vector< size_t > arrange_generalization_indices(void) const
Returns the indices of the instances which will be used for generalization.
Vector< double > calculate_squared_errors(void) const
Returns the squared errors of the training instances.
Vector< Vector< double > > arrange_layers_input(const Vector< double > &, const Vector< Vector< double > > &) const
void set_parameters(const Vector< double > &)
Vector< size_t > arrange_inputs_indices(void) const
Returns the indices of the input variables.
const Instances & get_instances(void) const
Returns a constant reference to the instances object composing this data set object.
size_t count_parameters_number(void) const
Returns the number of parameters (biases and synaptic weights) in the multilayer perceptron.