9#include "growing_inputs.h"
104 if(new_maximum_inputs_number <= 1)
106 ostringstream buffer;
108 buffer <<
"OpenNN Exception: GrowingInputs class.\n"
109 <<
"void set_maximum_selection_failures(const Index&) method.\n"
110 <<
"Maximum selection failures must be greater than 0.\n";
112 throw logic_error(buffer.str());
128 if(new_minimum_inputs_number == 0)
130 ostringstream buffer;
132 buffer <<
"OpenNN Exception: GrowingInputs class.\n"
133 <<
"void set_minimum_inputs_number(const Index&) method.\n"
134 <<
"Minimum inputs number must be greater than 0.\n";
136 throw logic_error(buffer.str());
152 if(new_maximum_selection_failures <= 0)
154 ostringstream buffer;
156 buffer <<
"OpenNN Exception: GrowingInputs class.\n"
157 <<
"void set_maximum_selection_failures(const Index&) method.\n"
158 <<
"Maximum selection failures must be greater than 0.\n";
160 throw logic_error(buffer.str());
182 if(
display) cout <<
"Performing growing inputs selection..." << endl;
188 type previus_selection_error = numeric_limits< type>::max();
200 Tensor<string, 1> input_columns_names;
204 const Tensor<type, 1> total_correlations = correlations.abs().sum(rows_sum);
208 sort(correlations_rank_descending.data(),
209 correlations_rank_descending.data() + correlations_rank_descending.size(),
210 [&](Index i, Index j){return total_correlations[i] > total_correlations[j];});
220 Index selection_failures = 0;
226 time_t beginning_time, current_time;
227 type elapsed_time = type(0);
229 time(&beginning_time);
237 data_set_pointer->
set_column_use(correlations_rank_descending[i-1], DataSet::VariableUse::Input);
252 cout <<
"Epoch: " << epoch << endl;
253 cout <<
"Input columns number: " << input_columns_number << endl;
254 cout <<
"Inputs: " << endl;
258 for(Index i = 0; i < input_columns_number; i++) cout <<
" " << input_columns_names(i) << endl;
261 type minimum_training_error = numeric_limits<type>::max();
262 type minimum_selection_error = numeric_limits<type>::max();
272 cout <<
"Trial number: " << i+1 << endl;
273 cout <<
" Training error: " << training_results.get_training_error() << endl;
274 cout <<
" Selection error: " << training_results.get_selection_error() << endl;
277 if(training_results.get_selection_error() < minimum_selection_error)
279 minimum_training_error = training_results.get_training_error();
280 minimum_selection_error = training_results.get_selection_error();
302 if(previus_selection_error < training_results.get_selection_error()) selection_failures++;
304 previus_selection_error = training_results.get_selection_error();
306 inputs_selection_results.
training_error_history(input_columns_number) = training_results.get_training_error();
308 inputs_selection_results.
selection_error_history(input_columns_number) = training_results.get_selection_error();
312 elapsed_time =
static_cast<type
>(difftime(current_time,beginning_time));
320 if(
display) cout <<
"Epoch " << epoch << endl <<
"Maximum time reached: " <<
write_time(elapsed_time) << endl;
322 inputs_selection_results.
stopping_condition = InputsSelection::StoppingCondition::MaximumTime;
330 inputs_selection_results.
stopping_condition = InputsSelection::StoppingCondition::SelectionErrorGoal;
336 if(
display) cout <<
"\nMaximum number of epochs reached." << endl;
338 inputs_selection_results.
stopping_condition = InputsSelection::StoppingCondition::MaximumEpochs;
344 if(
display) cout <<
"\nMaximum selection failures ("<<selection_failures<<
") reached." << endl;
346 inputs_selection_results.
stopping_condition = InputsSelection::StoppingCondition::MaximumSelectionFailures;
348 else if(input_columns_number >=
maximum_inputs_number || input_columns_number >= original_input_columns_number)
352 if(
display) cout <<
"\nMaximum inputs (" << input_columns_number <<
") reached." << endl;
354 inputs_selection_results.
stopping_condition = InputsSelection::StoppingCondition::MaximumInputs;
361 inputs_selection_results.resize_history(input_columns_number);
371 data_set_pointer->set_input_target_columns(inputs_selection_results.optimal_input_columns_indices, target_columns_indices);
373 const Tensor<Scaler, 1> input_variables_scalers = data_set_pointer->get_input_variables_scalers();
388 if(
display) inputs_selection_results.print();
390 return inputs_selection_results;
398 ostringstream buffer;
400 Tensor<string, 1> labels(8);
401 Tensor<string, 1> values(8);
405 labels(0) =
"Trials number";
410 values(0) = buffer.str();
414 labels(1) =
"Selection error goal";
419 values(1) = buffer.str();
423 labels(2) =
"Maximum selection failures";
428 values(2) = buffer.str();
432 labels(3) =
"Maximum inputs number";
437 values(3) = buffer.str();
441 labels(4) =
"Minimum correlations";
446 values(4) = buffer.str();
450 labels(5) =
"Maximum correlation";
455 values(5) = buffer.str();
459 labels(6) =
"Maximum iterations number";
464 values(6) = buffer.str();
468 labels(7) =
"Maximum time";
473 values(7) = buffer.str();
475 const Index rows_number = labels.size();
476 const Index columns_number = 2;
478 Tensor<string, 2> string_matrix(rows_number, columns_number);
480 string_matrix.chip(0, 1) = labels;
481 string_matrix.chip(1, 1) = values;
483 return string_matrix;
492 ostringstream buffer;
494 file_stream.OpenElement(
"GrowingInputs");
498 file_stream.OpenElement(
"TrialsNumber");
503 file_stream.
PushText(buffer.str().c_str());
509 file_stream.OpenElement(
"SelectionErrorGoal");
514 file_stream.
PushText(buffer.str().c_str());
520 file_stream.OpenElement(
"MaximumSelectionFailures");
525 file_stream.
PushText(buffer.str().c_str());
531 file_stream.OpenElement(
"MinimumInputsNumber");
536 file_stream.
PushText(buffer.str().c_str());
542 file_stream.OpenElement(
"MaximumInputsNumber");
547 file_stream.
PushText(buffer.str().c_str());
553 file_stream.OpenElement(
"MinimumCorrelation");
558 file_stream.
PushText(buffer.str().c_str());
564 file_stream.OpenElement(
"MaximumCorrelation");
569 file_stream.
PushText(buffer.str().c_str());
575 file_stream.OpenElement(
"MaximumEpochsNumber");
580 file_stream.
PushText(buffer.str().c_str());
586 file_stream.OpenElement(
"MaximumTime");
591 file_stream.
PushText(buffer.str().c_str());
608 ostringstream buffer;
610 buffer <<
"OpenNN Exception: GrowingInputs class.\n"
611 <<
"void from_XML(const tinyxml2::XMLDocument&) method.\n"
612 <<
"GrowingInputs element is nullptr.\n";
614 throw logic_error(buffer.str());
623 const Index new_trials_number =
static_cast<Index
>(atoi(element->GetText()));
629 catch(
const logic_error& e)
631 cerr << e.what() << endl;
642 const string new_display = element->GetText();
648 catch(
const logic_error& e)
650 cerr << e.what() << endl;
661 const type new_selection_error_goal =
static_cast<type
>(atof(element->GetText()));
667 catch(
const logic_error& e)
669 cerr << e.what() << endl;
680 const Index new_maximum_epochs_number =
static_cast<Index
>(atoi(element->GetText()));
686 catch(
const logic_error& e)
688 cerr << e.what() << endl;
699 const type new_maximum_correlation =
static_cast<type
>(atof(element->GetText()));
705 catch(
const logic_error& e)
707 cerr << e.what() << endl;
718 const type new_minimum_correlation =
static_cast<type
>(atof(element->GetText()));
724 catch(
const logic_error& e)
726 cerr << e.what() << endl;
737 const type new_maximum_time =
static_cast<type
>(atof(element->GetText()));
743 catch(
const logic_error& e)
745 cerr << e.what() << endl;
757 const Index new_minimum_inputs_number =
static_cast<Index
>(atoi(element->GetText()));
763 catch(
const logic_error& e)
765 cerr << e.what() << endl;
777 const Index new_maximum_inputs_number =
static_cast<Index
>(atoi(element->GetText()));
783 catch(
const logic_error& e)
785 cerr << e.what() << endl;
792 const tinyxml2::XMLElement* element = root_element->FirstChildElement(
"MaximumSelectionFailures");
796 const Index new_maximum_selection_failures =
static_cast<Index
>(atoi(element->GetText()));
802 catch(
const logic_error& e)
804 cerr << e.what() << endl;
817 FILE * file = fopen(file_name.c_str(),
"w");
836 if(document.LoadFile(file_name.c_str()))
838 ostringstream buffer;
840 buffer <<
"OpenNN Exception: GrowingInputs class.\n"
841 <<
"void load(const string&) method.\n"
842 <<
"Cannot load XML file " << file_name <<
".\n";
844 throw logic_error(buffer.str());
This class represents the concept of data set for data modelling problems, such as approximation,...
Tensor< string, 1 > get_columns_names() const
Returns a string vector that contains the names of the columns.
Tensor< Descriptives, 1 > calculate_input_variables_descriptives() const
Tensor< Index, 1 > get_target_columns_indices() const
Returns a indices vector with the positions of the targets.
Tensor< Index, 1 > get_input_columns_indices() const
Returns a indices vector with the positions of the inputs.
Index get_input_variables_number() const
Tensor< Correlation, 2 > calculate_input_target_columns_correlations() const
Tensor< string, 1 > get_input_columns_names() const
Returns a string vector that contains the names of the columns whose uses are Input.
void set_input_columns_unused()
Sets all input columns in the data_set as unused columns.
Tensor< string, 1 > get_input_variables_names() const
void set_column_use(const Index &, const VariableUse &)
Index get_input_columns_number() const
Returns the number of columns whose uses are Input.
This abstract class represents the concept of loss index composed of an error term and a regularizati...
DataSet * get_data_set_pointer() const
Returns a pointer to the data set object associated to the error term.
ScalingLayer * get_scaling_layer_pointer() const
Returns a pointer to the scaling layers object composing this neural network object.
bool has_scaling_layer() const
const bool & get_display() const
Index get_inputs_number() const
Returns the number of inputs to the neural network.
void set_inputs_number(const Index &)
void set_parameters(Tensor< type, 1 > &)
void set_parameters_random()
void set_inputs_names(const Tensor< string, 1 > &)
Tensor< type, 1 > get_parameters() const
void set()
Sets the scaling layer to be empty.
This class represents the concept of training strategy for a neural network in OpenNN.
TrainingResults perform_training()
LossIndex * get_loss_index_pointer()
Returns a pointer to the LossIndex class.
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 optimization algorithm results.