16 #include "cross_entropy_error.h"
116 if(
this != &other_cross_entropy_error)
159 std::ostringstream buffer;
165 buffer <<
"OpenNN Exception: CrossEntropyError class.\n"
166 <<
"void check(void) const method.\n"
167 <<
"Pointer to neural network is NULL.\n";
169 throw std::logic_error(buffer.str());
174 if(!multilayer_perceptron_pointer)
176 buffer <<
"OpenNN Exception: CrossEntropyError class.\n"
177 <<
"void check(void) const method.\n"
178 <<
"Pointer to multilayer perceptron is NULL.\n";
180 throw std::logic_error(buffer.str());
186 if(inputs_number == 0)
188 buffer <<
"OpenNN Exception: CrossEntropyError class.\n"
189 <<
"void check(void) const method.\n"
190 <<
"Number of inputs in multilayer perceptron object is zero.\n";
192 throw std::logic_error(buffer.str());
195 if(outputs_number == 0)
197 buffer <<
"OpenNN Exception: CrossEntropyError class.\n"
198 <<
"void check(void) const method.\n"
199 <<
"Number of outputs in multilayer perceptron object is zero.\n";
201 throw std::logic_error(buffer.str());
206 if(!probabilistic_layer_pointer)
208 buffer <<
"OpenNN Exception: CrossEntropyError class.\n"
209 <<
"void check(void) const method.\n"
210 <<
"Pointer to probabilistic layer is NULL.\n";
212 throw std::logic_error(buffer.str());
217 if(outputs_probabilizing_method != ProbabilisticLayer::Softmax)
219 buffer <<
"OpenNN Exception: CrossEntropyError class.\n"
220 <<
"void check(void) const method.\n"
221 <<
"Probabilistic method is not Softmax.\n";
223 throw std::logic_error(buffer.str());
230 buffer <<
"OpenNN Exception: CrossEntropyError class.\n"
231 <<
"void check(void) const method.\n"
232 <<
"Pointer to data set is NULL.\n";
234 throw std::logic_error(buffer.str());
244 if(inputs_number != data_set_inputs_number)
246 buffer <<
"OpenNN Exception: CrossEntropyError class.\n"
247 <<
"void check(void) const method.\n"
248 <<
"Number of inputs in neural network must be equal to number of inputs in data set.\n";
250 throw std::logic_error(buffer.str());
253 if(outputs_number != targets_number)
255 buffer <<
"OpenNN Exception: CrossEntropyError class.\n"
256 <<
"void check(void) const method.\n"
257 <<
"Number of outputs in neural network must be equal to number of targets in data set.\n";
259 throw std::logic_error(buffer.str());
270 std::ostringstream buffer;
272 buffer <<
"OpenNN Exception: CrossEntropyError class.\n"
273 <<
"double calculate_performance(void) method.\n"
274 <<
"This method is under development.\n";
276 throw std::logic_error(buffer.str());
395 size_t generalization_index;
410 double generalization_performance = 0.0;
414 #pragma omp parallel for private(i, generalization_index, inputs, outputs, targets) reduction(- : generalization_performance)
416 for(i = 0; i < (int)generalization_instances_number; i++)
418 generalization_index = generalization_indices[i];
439 for(
size_t j = 0; j < outputs_number; j++)
441 generalization_performance -= targets[j]*log(outputs[j]) + (1.0 - targets[j])*log(1.0 - outputs[j]);
445 return(generalization_performance);
492 std::ostringstream buffer;
496 if(target_data < 0.0)
498 buffer <<
"OpenNN Exception: CrossEntropyError class.\n"
499 <<
"Vector<double> calculate_gradient(void) const method.\n"
500 <<
"Target data must be equal or greater than zero.\n";
502 throw std::logic_error(buffer.str());
505 if(target_data > 1.0)
507 buffer <<
"OpenNN Exception: CrossEntropyError class.\n"
508 <<
"Vector<double> calculate_gradient(void) const method.\n"
509 <<
"Target data must be less or equal or than one.\n";
511 throw std::logic_error(buffer.str());
531 size_t training_index;
553 for(
size_t i = 0; i < training_instances_number; i++)
555 training_index = training_indices[i];
571 if(!has_conditions_layer)
573 const Vector<double>& outputs = layers_activation[layers_number-1];
575 for(
size_t j = 0; j < outputs_number; j++)
580 output_objective_gradient[j]
581 = -targets[j]/outputs[j] + (1.0 - targets[j])/(1.0 - outputs[j]);
592 const Vector<double>& outputs = particular_solution + homogeneous_solution*layers_activation[layers_number-1];
594 for(
size_t j = 0; j < outputs_number; j++)
599 output_objective_gradient[j]
600 = -targets[j]/outputs[j] + (1.0 - targets[j])*(1.0 - outputs[j]);
604 layers_delta =
calculate_layers_delta(layers_activation_derivative, homogeneous_solution, output_objective_gradient);
609 gradient += point_gradient;
624 return(objective_Hessian);
634 return(
"CROSS_ENTROPY_ERROR");
645 std::ostringstream buffer;
647 tinyxml2::XMLDocument* document =
new tinyxml2::XMLDocument;
651 tinyxml2::XMLElement* cross_entropy_error_element = document->NewElement(
"CrossEntropyError");
653 document->InsertFirstChild(cross_entropy_error_element);
658 tinyxml2::XMLElement* display_element = document->NewElement(
"Display");
659 cross_entropy_error_element->LinkEndChild(display_element);
664 tinyxml2::XMLText* display_text = document->NewText(buffer.str().c_str());
665 display_element->LinkEndChild(display_text);
679 const tinyxml2::XMLElement* root_element = document.FirstChildElement(
"CrossEntropyError");
683 std::ostringstream buffer;
685 buffer <<
"OpenNN Exception: CrossEntropyError class.\n"
686 <<
"void from_XML(const tinyxml2::XMLDocument&) method.\n"
687 <<
"Cross entropy error element is NULL.\n";
689 throw std::logic_error(buffer.str());
694 const tinyxml2::XMLElement* display_element = root_element->FirstChildElement(
"Display");
698 const std::string new_display_string = display_element->GetText();
704 catch(
const std::logic_error& e)
706 std::cout << e.what() << std::endl;
double calculate_minimum_generalization_performance(void)
const ProbabilisticMethod & get_probabilistic_method(void) const
const Variables & get_variables(void) const
Returns a constant reference to the variables object composing this data set object.
Matrix< double > arrange_target_data(void) const
size_t count_training_instances_number(void) const
Returns the number of instances in the data set which will be used for training.
Vector< double > calculate_gradient(void) const
size_t get_inputs_number(void) const
Returns the number of inputs to the multilayer perceptron.
size_t get_layers_number(void) const
Returns the number of layers in the multilayer perceptron.
void from_XML(const tinyxml2::XMLDocument &)
bool has_missing_values(void) const
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
double calculate_generalization_performance(void) const
ProbabilisticLayer * get_probabilistic_layer_pointer(void) const
Returns a pointer to the probabilistic layer composing this neural network.
const MissingValues & get_missing_values(void) const
Returns a reference to the missing values object in the data set.
bool operator==(const CrossEntropyError &) const
std::string write_performance_term_type(void) const
Returns a string with the name of the cross entropy error performance type, "CROSS_ENTROPY_ERROR".
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
MultilayerPerceptron * get_multilayer_perceptron_pointer(void) const
Returns a pointer to the multilayer perceptron composing this neural network.
virtual Vector< double > calculate_homogeneous_solution(const Vector< double > &) const
Returns the homogeneous solution for applying boundary conditions.
Matrix< double > calculate_Hessian(void) const
virtual Vector< double > calculate_particular_solution(const Vector< double > &) const
Returns the particular solution for applying boundary conditions.
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.
CrossEntropyError & operator=(const CrossEntropyError &)
virtual ~CrossEntropyError(void)
Destructor.
double calculate_performance(void) const
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.
tinyxml2::XMLDocument * to_XML(void) const
double calculate_minimum_performance(void)
Vector< size_t > arrange_generalization_indices(void) const
Returns the indices of the instances which will be used for generalization.
ProbabilisticMethod
Enumeration of available methods for interpreting variables as probabilities.
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.