9#include "model_selection.h"
27 set(new_training_strategy_pointer);
50 buffer <<
"OpenNN Exception: ModelSelection class.\n"
51 <<
"TrainingStrategy* get_training_strategy_pointer() const method.\n"
52 <<
"Training strategy pointer is nullptr.\n";
54 throw logic_error(buffer.str());
177 if(new_neurons_selection_method ==
"GROWING_NEURONS")
183 ostringstream buffer;
185 buffer <<
"OpenNN Exception: ModelSelection class.\n"
186 <<
"void set_neurons_selection_method(const string&) method.\n"
187 <<
"Unknown neurons selection type: " << new_neurons_selection_method <<
".\n";
189 throw logic_error(buffer.str());
208 if(new_inputs_selection_method ==
"GROWING_INPUTS")
212 else if(new_inputs_selection_method ==
"PRUNING_INPUTS")
216 else if(new_inputs_selection_method ==
"GENETIC_ALGORITHM")
222 ostringstream buffer;
224 buffer <<
"OpenNN Exception: ModelSelection class.\n"
225 <<
"void set_inputs_selection_method(const string&) method.\n"
226 <<
"Unknown inputs selection type: " << new_inputs_selection_method <<
".\n";
228 throw logic_error(buffer.str());
259 ostringstream buffer;
263 buffer <<
"OpenNN Exception: ModelSelection class.\n"
264 <<
"void check() const method.\n"
265 <<
"Pointer to training strategy is nullptr.\n";
267 throw logic_error(buffer.str());
274 if(!loss_index_pointer)
276 buffer <<
"OpenNN Exception: ModelSelection class.\n"
277 <<
"void check() const method.\n"
278 <<
"Pointer to loss index is nullptr.\n";
280 throw logic_error(buffer.str());
287 if(!neural_network_pointer)
289 buffer <<
"OpenNN Exception: ModelSelection class.\n"
290 <<
"void check() const method.\n"
291 <<
"Pointer to neural network is nullptr.\n";
293 throw logic_error(buffer.str());
296 if(neural_network_pointer->
is_empty())
298 buffer <<
"OpenNN Exception: ModelSelection class.\n"
299 <<
"void check() const method.\n"
300 <<
"Multilayer Perceptron is empty.\n";
302 throw logic_error(buffer.str());
309 if(!data_set_pointer)
311 buffer <<
"OpenNN Exception: ModelSelection class.\n"
312 <<
"void check() const method.\n"
313 <<
"Pointer to data set is nullptr.\n";
315 throw logic_error(buffer.str());
320 if(selection_samples_number == 0)
322 buffer <<
"OpenNN Exception: ModelSelection class.\n"
323 <<
"void check() const method.\n"
324 <<
"Number of selection samples is zero.\n";
326 throw logic_error(buffer.str());
339 case NeuronsSelectionMethod::GROWING_NEURONS:
354 case InputsSelectionMethod::GROWING_INPUTS:
357 case InputsSelectionMethod::PRUNING_INPUTS:
360 case InputsSelectionMethod::GENETIC_ALGORITHM:
375 file_stream.OpenElement(
"ModelSelection");
379 file_stream.OpenElement(
"NeuronsSelection");
381 file_stream.OpenElement(
"NeuronsSelectionMethod");
382 file_stream.
PushText(write_neurons_selection_method().c_str());
391 file_stream.OpenElement(
"InputsSelection");
393 file_stream.OpenElement(
"InputsSelectionMethod");
394 file_stream.
PushText(write_inputs_selection_method().c_str());
418 ostringstream buffer;
420 buffer <<
"OpenNN Exception: ModelSelection class.\n"
421 <<
"void from_XML(const tinyxml2::XMLDocument&) method.\n"
422 <<
"Model Selection element is nullptr.\n";
424 throw logic_error(buffer.str());
429 const tinyxml2::XMLElement* neurons_selection_element = root_element->FirstChildElement(
"NeuronsSelection");
431 if(neurons_selection_element)
435 const tinyxml2::XMLElement* neurons_selection_method_element = neurons_selection_element->FirstChildElement(
"NeuronsSelectionMethod");
441 const tinyxml2::XMLElement* growing_neurons_element = neurons_selection_element->FirstChildElement(
"GrowingNeurons");
443 if(growing_neurons_element)
447 tinyxml2::XMLElement* growing_neurons_element_copy = growing_neurons_document.NewElement(
"GrowingNeurons");
452 growing_neurons_element_copy->InsertEndChild(copy );
455 growing_neurons_document.InsertEndChild(growing_neurons_element_copy);
463 const tinyxml2::XMLElement* inputs_selection_element = root_element->FirstChildElement(
"InputsSelection");
465 if(inputs_selection_element)
467 const tinyxml2::XMLElement* inputs_selection_method_element = inputs_selection_element->FirstChildElement(
"InputsSelectionMethod");
473 const tinyxml2::XMLElement* growing_inputs_element = inputs_selection_element->FirstChildElement(
"GrowingInputs");
475 if(growing_inputs_element)
479 tinyxml2::XMLElement* growing_inputs_element_copy = growing_inputs_document.NewElement(
"GrowingInputs");
484 growing_inputs_element_copy->InsertEndChild(copy );
487 growing_inputs_document.InsertEndChild(growing_inputs_element_copy);
495 const tinyxml2::XMLElement* pruning_inputs_element = inputs_selection_element->FirstChildElement(
"PruningInputs");
497 if(pruning_inputs_element)
501 tinyxml2::XMLElement* pruning_inputs_element_copy = pruning_inputs_document.NewElement(
"PruningInputs");
506 pruning_inputs_element_copy->InsertEndChild(copy );
509 pruning_inputs_document.InsertEndChild(pruning_inputs_element_copy);
516 const tinyxml2::XMLElement* genetic_algorithm_element = inputs_selection_element->FirstChildElement(
"GeneticAlgorithm");
518 if(genetic_algorithm_element)
522 tinyxml2::XMLElement* genetic_algorithm_element_copy = genetic_algorithm_document.NewElement(
"GeneticAlgorithm");
527 genetic_algorithm_element_copy->InsertEndChild(copy );
530 genetic_algorithm_document.InsertEndChild(genetic_algorithm_element_copy);
539string ModelSelection::write_neurons_selection_method()
const
543 case NeuronsSelectionMethod::GROWING_NEURONS:
544 return "GROWING_NEURONS";
551string ModelSelection::write_inputs_selection_method()
const
555 case InputsSelectionMethod::GROWING_INPUTS:
556 return "GROWING_INPUTS";
558 case InputsSelectionMethod::PRUNING_INPUTS:
559 return "PRUNING_INPUTS";
561 case InputsSelectionMethod::GENETIC_ALGORITHM:
562 return "GENETIC_ALGORITHM";
582 FILE * file = fopen(file_name.c_str(),
"w");
599 if(document.LoadFile(file_name.c_str()))
601 ostringstream buffer;
603 buffer <<
"OpenNN Exception: ModelSelection class.\n"
604 <<
"void load(const string&) method.\n"
605 <<
"Cannot load XML file " << file_name <<
".\n";
607 throw logic_error(buffer.str());
This class represents the concept of data set for data modelling problems, such as approximation,...
Index get_selection_samples_number() const
Returns the number of samples in the data set which will be used for selection.
void from_XML(const tinyxml2::XMLDocument &)
InputsSelectionResults perform_inputs_selection()
Select the inputs with best generalization properties using the genetic algorithm.
void write_XML(tinyxml2::XMLPrinter &) const
This concrete class represents an growing neurons algorithm for the NeuronsSelection as part of the M...
void from_XML(const tinyxml2::XMLDocument &)
NeuronsSelectionResults perform_neurons_selection()
Perform neurons selection with the growing neurons method.
void write_XML(tinyxml2::XMLPrinter &) const
This abstract class represents the concept of loss index composed of an error term and a regularizati...
NeuralNetwork * get_neural_network_pointer() const
Returns a pointer to the neural network object associated to the error term.
DataSet * get_data_set_pointer() const
Returns a pointer to the data set object associated to the error term.
const InputsSelectionMethod & get_inputs_selection_method() const
Returns the type of algorithm for the inputs selection.
GrowingInputs * get_growing_inputs_pointer()
Returns a pointer to the growing inputs selection algorithm.
PruningInputs pruning_inputs
Pruning inputs object to be used for inputs selection.
TrainingStrategy * training_strategy_pointer
Pointer to a training strategy object.
NeuronsSelectionMethod
Enumeration of all the available neurons selection algorithms.
void from_XML(const tinyxml2::XMLDocument &)
void set_default()
Sets the members of the model selection object to their default values.
GrowingInputs growing_inputs
Growing inputs object to be used for inputs selection.
ModelSelection()
Default constructor.
void check() const
Checks that the different pointers needed for performing the model selection are not nullptr.
void load(const string &)
NeuronsSelectionResults perform_neurons_selection()
void set_inputs_selection_method(const InputsSelectionMethod &)
GrowingNeurons * get_growing_neurons_pointer()
Returns a pointer to the growing neurons selection algorithm.
bool display
Display messages to screen.
GeneticAlgorithm genetic_algorithm
Genetic algorithm object to be used for inputs selection.
InputsSelectionResults perform_inputs_selection()
const NeuronsSelectionMethod & get_neurons_selection_method() const
Returns the type of algorithm for the neurons selection.
void save(const string &) const
virtual ~ModelSelection()
Destructor.
GrowingNeurons growing_neurons
Growing neurons object to be used for neurons selection.
TrainingStrategy * get_training_strategy_pointer() const
Returns a pointer to the training strategy object.
NeuronsSelectionMethod neurons_selection_method
Type of neurons selection algorithm.
void set(TrainingStrategy *)
InputsSelectionMethod
Enumeration of all the available inputs selection algorithms.
void print() const
Prints to the screen the XML representation of this model selection object.
void set_display(const bool &)
PruningInputs * get_pruning_inputs_pointer()
Returns a pointer to the pruning inputs selection algorithm.
void write_XML(tinyxml2::XMLPrinter &) const
bool has_training_strategy() const
GeneticAlgorithm * get_genetic_algorithm_pointer()
Returns a pointer to the genetic inputs selection algorithm.
InputsSelectionMethod inputs_selection_method
Type of inputs selection algorithm.
void set_neurons_selection_method(const NeuronsSelectionMethod &)
void set_training_strategy_pointer(TrainingStrategy *)
void set_display(const bool &)
This class represents the concept of training strategy for a neural network in OpenNN.
LossIndex * get_loss_index_pointer()
Returns a pointer to the LossIndex class.
const XMLNode * NextSibling() const
Get the next(right) sibling node of this node.
const XMLNode * FirstChild() const
Get the first child node, or null if none exists.
void PushText(const char *text, bool cdata=false)
Add a text node.
virtual void CloseElement(bool compactMode=false)
If streaming, close the Element.
This structure contains the results from the neurons selection.