9#include "growing_neurons.h"
82 if(new_neurons_increment <= 0)
86 buffer <<
"OpenNN Exception: GrowingNeurons class.\n"
87 <<
"void set_neurons_increment(const Index&) method.\n"
88 <<
"New_step(" << new_neurons_increment <<
") must be greater than 0.\n";
90 throw logic_error(buffer.str());
106 if(new_maximum_selection_failures <= 0)
108 ostringstream buffer;
110 buffer <<
"OpenNN Exception: GrowingNeurons class.\n"
111 <<
"void set_maximum_selection_failures(const Index&) method.\n"
112 <<
"Maximum selection failures must be greater than 0.\n";
114 throw logic_error(buffer.str());
131 ostringstream buffer;
133 buffer <<
"OpenNN Exception: growing_neurons class.\n"
134 <<
"TrainingStrategy* training_strategy_pointer const method.\n"
135 <<
"training_strategy_pointer is nullptr.\n";
137 throw logic_error(buffer.str());
144 if(
display) cout <<
"Performing growing neurons selection..." << endl;
150 const Index trainable_layers_number = neural_network->get_trainable_layers_number();
154 Index neurons_number;
158 type previous_selection_error = numeric_limits<type>::max();
162 Index selection_failures = 0;
166 time_t beginning_time, current_time;
168 type elapsed_time = type(0);
174 time(&beginning_time);
180 if(
display == 0) cout << endl <<
"Neurons selection epoch: " << epoch << endl;
186 trainable_layers_pointers(trainable_layers_number-2)->set_neurons_number(neurons_number);
188 trainable_layers_pointers(trainable_layers_number-1)->set_inputs_number(neurons_number);
194 if(
display) cout <<
"Neurons number: " << neurons_number << endl;
196 type minimum_training_error = numeric_limits<type>::max();
197 type minimum_selection_error = numeric_limits<type>::max();
207 cout <<
"Trial: " << trial+1 << endl;
208 cout <<
"Training error: " << training_results.get_training_error() << endl;
209 cout <<
"Selection error: " << training_results.get_selection_error() << endl;
212 if(training_results.get_selection_error() < minimum_selection_error)
214 minimum_training_error = training_results.get_training_error();
215 minimum_selection_error = training_results.get_selection_error();
233 cout <<
"Neurons number: " << neurons_number << endl;
234 cout <<
"Training error: " << training_results.get_training_error() << endl;
235 cout <<
"Selection error: " << training_results.get_selection_error() << endl;
237 cout <<
"Elapsed time: " <<
write_time(elapsed_time) << endl;
246 elapsed_time =
static_cast<type
>(difftime(current_time, beginning_time));
254 if(
display) cout <<
"Epoch " << epoch << endl <<
"Maximum time reached: " <<
write_time(elapsed_time) << endl;
256 neurons_selection_results.
stopping_condition = GrowingNeurons::StoppingCondition::MaximumTime;
263 if(
display) cout <<
"Epoch " << epoch << endl <<
"Selection error goal reached: " << training_results.get_selection_error() << endl;
265 neurons_selection_results.
stopping_condition = GrowingNeurons::StoppingCondition::SelectionErrorGoal;
272 if(
display) cout <<
"Epoch " << epoch << endl <<
"Maximum number of epochs reached: " << epoch << endl;
274 neurons_selection_results.
stopping_condition = GrowingNeurons::StoppingCondition::MaximumEpochs;
281 if(
display) cout <<
"Epoch " << epoch << endl <<
"Maximum selection failures reached: " << selection_failures << endl;
283 neurons_selection_results.
stopping_condition = GrowingNeurons::StoppingCondition::MaximumSelectionFailures;
290 if(
display) cout <<
"Epoch " << epoch << endl <<
"Maximum number of neurons reached: " << neurons_number << endl;
292 neurons_selection_results.
stopping_condition = GrowingNeurons::StoppingCondition::MaximumNeurons;
297 neurons_selection_results.resize_history(epoch+1);
307 trainable_layers_pointers[trainable_layers_number-1]->set_inputs_number(neurons_selection_results.
optimal_neurons_number);
308 trainable_layers_pointers[trainable_layers_number-2]->set_neurons_number(neurons_selection_results.
optimal_neurons_number);
312 if(
display) neurons_selection_results.print();
314 return neurons_selection_results;
322 ostringstream buffer;
324 Tensor<string, 1> labels(8);
325 Tensor<string, 1> values(8);
329 labels(0) =
"Minimum neurons";
334 values(0) = buffer.str();
338 labels(1) =
"Maximum neurons";
343 values(1) = buffer.str();
352 values(2) = buffer.str();
356 labels(3) =
"Trials number";
361 values(3) = buffer.str();
365 labels(4) =
"Selection loss goal";
370 values(4) = buffer.str();
374 labels(5) =
"Maximum selection failures";
379 values(5) = buffer.str();
383 labels(6) =
"Maximum iterations number";
388 values(6) = buffer.str();
392 labels(7) =
"Maximum time";
397 values(7) = buffer.str();
399 const Index rows_number = labels.size();
400 const Index columns_number = 2;
402 Tensor<string, 2> string_matrix(rows_number, columns_number);
404 string_matrix.chip(0, 1) = labels;
405 string_matrix.chip(1, 1) = values;
407 return string_matrix;
417 ostringstream buffer;
419 file_stream.OpenElement(
"GrowingNeurons");
423 file_stream.OpenElement(
"MinimumNeurons");
428 file_stream.
PushText(buffer.str().c_str());
434 file_stream.OpenElement(
"MaximumNeurons");
439 file_stream.
PushText(buffer.str().c_str());
445 file_stream.OpenElement(
"Step");
450 file_stream.
PushText(buffer.str().c_str());
456 file_stream.OpenElement(
"TrialsNumber");
461 file_stream.
PushText(buffer.str().c_str());
467 file_stream.OpenElement(
"SelectionErrorGoal");
472 file_stream.
PushText(buffer.str().c_str());
478 file_stream.OpenElement(
"MaximumSelectionFailures");
483 file_stream.
PushText(buffer.str().c_str());
489 file_stream.OpenElement(
"MaximumTime");
494 file_stream.
PushText(buffer.str().c_str());
511 ostringstream buffer;
513 buffer <<
"OpenNN Exception: GrowingNeurons class.\n"
514 <<
"void from_XML(const tinyxml2::XMLDocument&) method.\n"
515 <<
"GrowingNeurons element is nullptr.\n";
517 throw logic_error(buffer.str());
526 const Index new_minimum_neurons =
static_cast<Index
>(atoi(element->GetText()));
532 catch(
const logic_error& e)
534 cerr << e.what() << endl;
545 const Index new_maximum_neurons =
static_cast<Index
>(atoi(element->GetText()));
551 catch(
const logic_error& e)
553 cerr << e.what() << endl;
564 const Index new_step =
static_cast<Index
>(atoi(element->GetText()));
570 catch(
const logic_error& e)
572 cerr << e.what() << endl;
583 const Index new_trials_number =
static_cast<Index
>(atoi(element->GetText()));
589 catch(
const logic_error& e)
591 cerr << e.what() << endl;
602 const type new_selection_error_goal =
static_cast<type
>(atof(element->GetText()));
608 catch(
const logic_error& e)
610 cerr << e.what() << endl;
617 const tinyxml2::XMLElement* element = root_element->FirstChildElement(
"MaximumSelectionFailures");
621 const Index new_maximum_selection_failures =
static_cast<Index
>(atoi(element->GetText()));
627 catch(
const logic_error& e)
629 cerr << e.what() << endl;
640 const type new_maximum_time = type(atoi(element->GetText()));
646 catch(
const logic_error& e)
648 cerr << e.what() << endl;
660 FILE * file = fopen(file_name.c_str(),
"w");
679 if(document.LoadFile(file_name.c_str()))
681 ostringstream buffer;
683 buffer <<
"OpenNN Exception: GrowingNeurons class.\n"
684 <<
"void load(const string&) method.\n"
685 <<
"Cannot load XML file " << file_name <<
".\n";
687 throw logic_error(buffer.str());
void set_maximum_selection_failures(const Index &)
virtual ~GrowingNeurons()
Destructor.
void from_XML(const tinyxml2::XMLDocument &)
void set_default()
Sets the members of the model selection object to their default values:
void load(const string &)
GrowingNeurons()
Default constructor.
NeuronsSelectionResults perform_neurons_selection()
Perform neurons selection with the growing neurons method.
Tensor< string, 2 > to_string_matrix() const
Writes as matrix of strings the most representative atributes.
Index neurons_increment
Number of neurons added at each iteration.
void save(const string &) const
void set_neurons_increment(const Index &)
const Index & get_step() const
Returns the number of the hidden perceptrons pointed in each iteration of the growing neurons algorit...
void write_XML(tinyxml2::XMLPrinter &) const
const Index & get_maximum_selection_failures() const
Returns the maximum number of selection failures in the model neurons selection algorithm.
Index maximum_selection_failures
Maximum number of epochs at which the selection error increases.
Tensor< Layer *, 1 > get_trainable_layers_pointers() const
Returns a pointer to the trainable layers object composing this neural network object.
void set_parameters(Tensor< type, 1 > &)
void set_parameters_random()
Tensor< type, 1 > get_parameters() const
This abstract class represents the concept of neurons selection algorithm for a ModelSelection[1].
Index minimum_neurons
Minimum number of hidden neurons.
TrainingStrategy * training_strategy_pointer
Pointer to a training strategy object.
void set_selection_error_goal(const type &)
Index maximum_neurons
Maximum number of hidden neurons.
Index trials_number
Number of trials for each neural network.
bool display
Display messages to screen.
type selection_error_goal
Goal value for the selection error. It is used as a stopping criterion.
void set_maximum_time(const type &)
const string write_time(const type &) const
Writes the time from seconds in format HH:mm:ss.
type maximum_time
Maximum selection algorithm time. It is used as a stopping criterion.
Index maximum_epochs_number
Maximum number of epochs to perform neurons selection. It is used as a stopping criterion.
void set_trials_number(const Index &)
This class represents the concept of training strategy for a neural network in OpenNN.
TrainingResults perform_training()
NeuralNetwork * get_neural_network_pointer() const
Returns a pointer to the NeuralNetwork class.
void set_display(const bool &)
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.
type optimum_training_error
Value of loss for the neural network with minimum selection error.
Tensor< type, 1 > selection_error_history
Selection loss of the different neural networks.
Index optimal_neurons_number
Neurons of the neural network with minimum selection error.
Tensor< type, 1 > optimal_parameters
Vector of parameters for the neural network with minimum selection error.
type optimum_selection_error
Value of minimum selection error.
Tensor< type, 1 > training_error_history
Performance of the different neural networks.
NeuronsSelection::StoppingCondition stopping_condition
Stopping condition of the algorithm.
string elapsed_time
Elapsed time during the loss of the algortihm.
Tensor< Index, 1 > neurons_number_history
Neurons of the diferent neural networks.
This structure contains the optimization algorithm results.