inputs_selection.h
1// OpenNN: Open Neural Networks Library
2// www.opennn.net
3//
4// I N P U T S S E L E C T I O N C L A S S H E A D E R
5//
6// Artificial Intelligence Techniques SL
7// artelnics@artelnics.com
8
9#ifndef INPUTSSELECTIONALGORITHM_H
10#define INPUTSSELECTIONALGORITHM_H
11
12// System includes
13
14#include <iostream>
15#include <fstream>
16#include <string>
17#include <sstream>
18#include <cmath>
19#include <ctime>
20#include <limits>
21
22// OpenNN includes
23
24#include "training_strategy.h"
25#include "config.h"
26
27namespace OpenNN
28{
29
30struct InputsSelectionResults;
31
33
39
41{
42public:
43
44 // Constructors
45
46 explicit InputsSelection();
47
49
50 // Destructor
51
52 virtual ~InputsSelection();
53
54 // Enumerations
55
57
58 enum class StoppingCondition{MaximumTime, SelectionErrorGoal, MaximumInputs, MinimumInputs, MaximumEpochs,
59 MaximumSelectionFailures, CorrelationGoal};
60
61 // Get methods
62
64
65 bool has_training_strategy() const;
66
67 const Index& get_trials_number() const;
68
69 const bool& get_display() const;
70
71 const type& get_selection_error_goal() const;
72 const Index& get_maximum_iterations_number() const;
73 const type& get_maximum_time() const;
74 const type& get_maximum_correlation() const;
75 const type& get_minimum_correlation() const;
76 const type& get_tolerance() const;
77
78 // Set methods
79
80 void set(TrainingStrategy*);
81
82 void set_default();
83
84 void set_trials_number(const Index&);
85
86 void set_display(const bool&);
87
88 void set_selection_error_goal(const type&);
89 void set_maximum_epochs_number(const Index&);
90 void set_maximum_time(const type&);
91 void set_maximum_correlation(const type&);
92 void set_minimum_correlation(const type&);
93
94 // Performances calculation methods
95
96 string write_stopping_condition(const TrainingResults&) const;
97
98 // inputs selection methods
99
100 void check() const;
101
102 // Utilities
103
104 Index get_input_index(const Tensor<DataSet::VariableUse, 1>&, const Index&);
105
107
109
111
112 const string write_time(const type&) const;
113
114protected:
115
117
119
120 Tensor<Index, 1> original_input_columns_indices;
121 Tensor<Index, 1> original_target_columns_indices;
122
124
125 Index trials_number = 1;
126
128
129 bool display = true;
130
131 // Stopping criteria
132
134
136
138
140
142
144
146
148
150
152
153 const Eigen::array<int, 1> rows_sum = {Eigen::array<int, 1>({1})};
154};
155
156
158
160{
161 // Default constructor
162
163 explicit InputsSelectionResults()
164 {
165 }
166
167 // Maximum epochs number constructor
168
169 explicit InputsSelectionResults(const Index& maximum_epochs_number)
170 {
171 set(maximum_epochs_number);
172 }
173
174 Index get_epochs_number() const
175 {
176 return training_error_history.size();
177 }
178
179 void set(const Index& maximum_epochs_number)
180 {
181 training_error_history.resize(maximum_epochs_number);
182 training_error_history.setConstant(type(-1));
183
184 selection_error_history.resize(maximum_epochs_number);
185 selection_error_history.setConstant(type(-1));
186 }
187
188 virtual ~InputsSelectionResults() {}
189
190 string write_stopping_condition() const;
191
192 void resize_history(const Index& new_size)
193 {
194 const Tensor<type, 1> old_training_error_history = training_error_history;
195 const Tensor<type, 1> old_selection_error_history = selection_error_history;
196
197 training_error_history.resize(new_size);
198 selection_error_history.resize(new_size);
199
200 for(Index i = 0; i < new_size; i++)
201 {
202 training_error_history(i) = old_training_error_history(i);
203 selection_error_history(i) = old_selection_error_history(i);
204 }
205 }
206
207
208 void print() const
209 {
210 cout << endl;
211 cout << "Inputs Selection Results" << endl;
212
213 cout << "Optimal inputs number: " << optimal_input_columns_names.size() << endl;
214
215 cout << "Inputs: " << endl;
216
217 for(Index i = 0; i < optimal_input_columns_names.size(); i++) cout << " " << optimal_input_columns_names(i) << endl;
218
219 cout << "Optimum training error: " << optimum_training_error << endl;
220 cout << "Optimum selection error: " << optimum_selection_error << endl;
221 }
222
223
224 // Neural network
225
227
228 Tensor<type, 1> optimal_parameters;
229
230 // Loss index
231
233
234 Tensor<type, 1> training_error_history;
235
237
238 Tensor<type, 1> selection_error_history;
239
241
242 type optimum_training_error = numeric_limits<type>::max();
243
245
246 type optimum_selection_error = numeric_limits<type>::max();
247
249
251
252 Tensor<Index, 1> optimal_input_columns_indices;
253
254 Tensor<bool, 1> optimal_inputs;
255
256 // Model selection
257
259
261
263
265};
266
267}
268
269#endif
270
271// OpenNN: Open Neural Networks Library.
272// Copyright(C) 2005-2021 Artificial Intelligence Techniques, SL.
273//
274// This library is free software; you can redistribute it and/or
275// modify it under the terms of the GNU Lesser General Public
276// License as published by the Free Software Foundation; either
277// version 2.1 of the License, or any later version.
278//
279// This library is distributed in the hope that it will be useful,
280// but WITHOUT ANY WARRANTY; without even the implied warranty of
281// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
282// Lesser General Public License for more details.
283
284// You should have received a copy of the GNU Lesser General Public
285// License along with this library; if not, write to the Free Software
286// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
This abstract class represents the concept of inputs selection algorithm for a ModelSelection[1].
const type & get_maximum_correlation() const
Return the maximum correlation for the algorithm.
const type & get_maximum_time() const
Returns the maximum time in the inputs selection algorithm.
TrainingStrategy * training_strategy_pointer
Pointer to a training strategy object.
const bool & get_display() const
void set_selection_error_goal(const type &)
void set_default()
Sets the members of the inputs selection object to their default values.
InputsSelection()
Default constructor.
void check() const
Checks that the different pointers needed for performing the inputs selection are not nullptr.
Index trials_number
Number of trials for each neural network.
void set_maximum_correlation(const type &)
bool display
Display messages to screen.
const type & get_minimum_correlation() const
Return the minimum correlation for the algorithm.
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.
virtual InputsSelectionResults perform_inputs_selection()=0
Performs the inputs selection for a neural network.
const Index & get_trials_number() const
Returns the number of trials for each network architecture.
type maximum_time
Maximum selection algorithm time. It is used as a stopping criterion.
void set_maximum_epochs_number(const Index &)
const type & get_selection_error_goal() const
Returns the goal for the selection error in the inputs selection algorithm.
type minimum_correlation
Minimum value for the correlations.
TrainingStrategy * get_training_strategy_pointer() const
Returns a pointer to the training strategy object.
void set(TrainingStrategy *)
virtual ~InputsSelection()
Destructor.
Index get_input_index(const Tensor< DataSet::VariableUse, 1 > &, const Index &)
Index maximum_epochs_number
Maximum number of epochs to perform_inputs_selection. It is used as a stopping criterion.
void set_display(const bool &)
const Index & get_maximum_iterations_number() const
Returns the maximum number of iterations in the inputs selection algorithm.
void set_minimum_correlation(const type &)
StoppingCondition
Enumeration of all possibles condition of stop for the algorithms.
string write_stopping_condition(const TrainingResults &) const
bool has_training_strategy() const
Returns true if this inputs selection algorithm has a training strategy associated,...
type maximum_correlation
Maximum value for the correlations.
void set_trials_number(const Index &)
This class represents the concept of training strategy for a neural network in OpenNN.
This structure contains the results from the inputs selection.
Tensor< string, 1 > optimal_input_columns_names
Inputs of the neural network with minimum selection error.
type optimum_training_error
Value of training for the neural network with minimum selection error.
Tensor< type, 1 > selection_error_history
Final selection errors of the different neural networks.
Tensor< type, 1 > optimal_parameters
Vector of parameters for the neural network with minimum selection error.
InputsSelection::StoppingCondition stopping_condition
Stopping condition of the algorithm.
type optimum_selection_error
Value of minimum selection error.
Tensor< type, 1 > training_error_history
Final training errors of the different neural networks.
string elapsed_time
Elapsed time during the loss of the algortihm.
string write_stopping_condition() const
Return a string with the stopping condition of the Results.
This structure contains the optimization algorithm results.