9#include "pruning_inputs.h"
100 if(new_minimum_inputs_number <= 0)
102 ostringstream buffer;
104 buffer <<
"OpenNN Exception: PruningInputs class.\n"
105 <<
"void set_minimum_inputs_number(const Index&) method.\n"
106 <<
"Minimum inputs number must be greater than 0.\n";
108 throw logic_error(buffer.str());
124 if(new_maximum_inputs_number <= 0)
126 ostringstream buffer;
128 buffer <<
"OpenNN Exception: PruningInputs class.\n"
129 <<
"void set_maximum_inputs_number(const Index&) method.\n"
130 <<
"Maximum inputs number must be greater than 0.\n";
132 throw logic_error(buffer.str());
148 if(new_maximum_selection_failures <= 0)
150 ostringstream buffer;
152 buffer <<
"OpenNN Exception: PruningInputs class.\n"
153 <<
"void set_maximum_selection_failures(const Index&) method.\n"
154 <<
"Maximum selection failures must be greater than 0.\n";
156 throw logic_error(buffer.str());
178 if(
display) cout <<
"Performing pruning inputs selection..." << endl;
184 type previus_selection_error = numeric_limits<type>::max();
192 Tensor<string, 1> input_columns_names;
196 const Tensor<type, 1> total_correlations = correlations.abs().sum(rows_sum);
200 sort(correlations_rank_ascending.data(),
201 correlations_rank_ascending.data() + correlations_rank_ascending.size(),
202 [&](Index i, Index j){return total_correlations[i] < total_correlations[j];});
212 Index selection_failures = 0;
218 time_t beginning_time, current_time;
219 type elapsed_time = type(0);
221 time(&beginning_time);
227 if(epoch > 0) data_set_pointer->
set_column_use(correlations_rank_ascending[epoch], DataSet::VariableUse::UnusedVariable);
237 cout <<
"Epoch: " << epoch << endl;
238 cout <<
"Input columns number: " << input_columns_number << endl;
239 cout <<
"Inputs: " << endl;
243 for(Index i = 0; i < input_columns_number; i++) cout <<
" " << input_columns_names(i) << endl;
246 type minimum_training_error = numeric_limits<type>::max();
247 type minimum_selection_error = numeric_limits<type>::max();
255 if(training_results.get_selection_error() < minimum_selection_error)
257 minimum_training_error = training_results.get_training_error();
258 minimum_selection_error = training_results.get_selection_error();
281 cout <<
"Trial number: " << trial+1 << endl;
282 cout <<
" Training error: " << training_results.get_training_error() << endl;
283 cout <<
" Selection error: " << training_results.get_selection_error() << endl;
297 elapsed_time =
static_cast<type
>( difftime(current_time,beginning_time));
305 if(
display) cout <<
"Epoch " << epoch << endl <<
"Maximum time reached: " <<
write_time(elapsed_time) << endl;
307 inputs_selection_results.
stopping_condition = InputsSelection::StoppingCondition::MaximumTime;
315 inputs_selection_results.
stopping_condition = InputsSelection::StoppingCondition::SelectionErrorGoal;
321 if(
display) cout <<
"Epoch " << epoch << endl <<
"Maximum number of epochs reached: " << epoch << endl;
323 inputs_selection_results.
stopping_condition = InputsSelection::StoppingCondition::MaximumEpochs;
329 if(
display) cout <<
"Epoch " << epoch << endl <<
"Maximum selection failures reached: " << selection_failures << endl;
331 inputs_selection_results.
stopping_condition = InputsSelection::StoppingCondition::MaximumSelectionFailures;
339 inputs_selection_results.
stopping_condition = InputsSelection::StoppingCondition::MinimumInputs;
348 inputs_selection_results.resize_history(epoch+1);
356 data_set_pointer->set_input_target_columns(inputs_selection_results.optimal_input_columns_indices, target_columns_indices);
358 const Tensor<Scaler, 1> input_variables_scalers = data_set_pointer->get_input_variables_scalers();
373 if(
display) inputs_selection_results.print();
375 return inputs_selection_results;
383 ostringstream buffer;
385 Tensor<string, 1> labels(8);
386 Tensor<string, 1> values(8);
390 labels(0) =
"Trials number";
395 values(0) = buffer.str();
399 labels(1) =
"Selection loss goal";
404 values(1) = buffer.str();
408 labels(2) =
"Maximum selection failures";
413 values(2) = buffer.str();
417 labels(3) =
"Minimum inputs number";
422 values(3) = buffer.str();
426 labels(4) =
"Minimum correlation";
431 values(4) = buffer.str();
435 labels(5) =
"Maximum correlation";
440 values(5) = buffer.str();
444 labels(6) =
"Maximum iterations number";
449 values(6) = buffer.str();
453 labels(7) =
"Maximum time";
458 values(7) = buffer.str();
460 const Index rows_number = labels.size();
461 const Index columns_number = 2;
463 Tensor<string, 2> string_matrix(rows_number, columns_number);
465 string_matrix.chip(0, 1) = labels;
466 string_matrix.chip(1, 1) = values;
468 return string_matrix;
477 ostringstream buffer;
479 file_stream.OpenElement(
"PruningInputs");
483 file_stream.OpenElement(
"TrialsNumber");
488 file_stream.
PushText(buffer.str().c_str());
494 file_stream.OpenElement(
"SelectionErrorGoal");
499 file_stream.
PushText(buffer.str().c_str());
505 file_stream.OpenElement(
"MaximumSelectionFailures");
510 file_stream.
PushText(buffer.str().c_str());
516 file_stream.OpenElement(
"MinimumInputsNumber");
521 file_stream.
PushText(buffer.str().c_str());
527 file_stream.OpenElement(
"MaximumInputsNumber");
532 file_stream.
PushText(buffer.str().c_str());
538 file_stream.OpenElement(
"MinimumCorrelation");
543 file_stream.
PushText(buffer.str().c_str());
549 file_stream.OpenElement(
"MaximumCorrelation");
554 file_stream.
PushText(buffer.str().c_str());
560 file_stream.OpenElement(
"MaximumEpochsNumber");
565 file_stream.
PushText(buffer.str().c_str());
571 file_stream.OpenElement(
"MaximumTime");
576 file_stream.
PushText(buffer.str().c_str());
593 ostringstream buffer;
595 buffer <<
"OpenNN Exception: PruningInputs class.\n"
596 <<
"void from_XML(const tinyxml2::XMLDocument&) method.\n"
597 <<
"PruningInputs element is nullptr.\n";
599 throw logic_error(buffer.str());
608 const Index new_trials_number =
static_cast<Index
>(atoi(element->GetText()));
614 catch(
const logic_error& e)
616 cerr << e.what() << endl;
627 const string new_display = element->GetText();
633 catch(
const logic_error& e)
635 cerr << e.what() << endl;
646 const type new_selection_error_goal =
static_cast<type
>(atof(element->GetText()));
652 catch(
const logic_error& e)
654 cerr << e.what() << endl;
665 const Index new_maximum_epochs_number =
static_cast<Index
>(atoi(element->GetText()));
671 catch(
const logic_error& e)
673 cerr << e.what() << endl;
684 const type new_maximum_correlation =
static_cast<type
>(atof(element->GetText()));
690 catch(
const logic_error& e)
692 cerr << e.what() << endl;
703 const type new_minimum_correlation =
static_cast<type
>(atof(element->GetText()));
709 catch(
const logic_error& e)
711 cerr << e.what() << endl;
722 const type new_maximum_time =
static_cast<type
>( atoi(element->GetText()));
728 catch(
const logic_error& e)
730 cerr << e.what() << endl;
741 const Index new_minimum_inputs_number =
static_cast<Index
>(atoi(element->GetText()));
747 catch(
const logic_error& e)
749 cerr << e.what() << endl;
760 const Index new_maximum_inputs_number =
static_cast<Index
>(atoi(element->GetText()));
766 catch(
const logic_error& e)
768 cerr << e.what() << endl;
775 const tinyxml2::XMLElement* element = root_element->FirstChildElement(
"MaximumSelectionFailures");
779 const Index new_maximum_selection_failures =
static_cast<Index
>(atoi(element->GetText()));
785 catch(
const logic_error& e)
787 cerr << e.what() << endl;
799 FILE * file = fopen(file_name.c_str(),
"w");
818 if(document.LoadFile(file_name.c_str()))
820 ostringstream buffer;
822 buffer <<
"OpenNN Exception: PruningInputs class.\n"
823 <<
"void load(const string&) method.\n"
824 <<
"Cannot load XML file " << file_name <<
".\n";
826 throw logic_error(buffer.str());
This class represents the concept of a data set for data modelling problems, such as approximation,...
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.
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.
This class represents the concept of neural network in the OpenNN library.
ScalingLayer * get_scaling_layer_pointer() const
Returns a pointer to the scaling layers object composing this neural network object.
bool has_scaling_layer() 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.