inputs_selection.cpp
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
5//
6// Artificial Intelligence Techniques SL
7// artelnics@artelnics.com
8
9#include "inputs_selection.h"
10
11namespace OpenNN
12{
13
15
17 : training_strategy_pointer(nullptr)
18{
20}
21
22
25
27 : training_strategy_pointer(new_training_strategy_pointer)
28{
30}
31
32
34
36{
37}
38
39
41
43{
44#ifdef OPENNN_DEBUG
45
47 {
48 ostringstream buffer;
49
50 buffer << "OpenNN Exception: InputsSelection class.\n"
51 << "DataSet* get_training_strategy_pointer() const method.\n"
52 << "Training strategy pointer is nullptr.\n";
53
54 throw logic_error(buffer.str());
55 }
56
57#endif
58
60}
61
62
64
66{
68 {
69 return true;
70 }
71 else
72 {
73 return false;
74 }
75}
76
77
79
81{
82 return trials_number;
83}
84
85
88
90{
91 return display;
92}
93
94
96
98{
100}
101
102
104
106{
108}
109
110
112
114{
115 return maximum_time;
116}
117
118
120
122{
123 return maximum_correlation;
124}
125
126
128
130{
131 return minimum_correlation;
132}
133
134
137
138void InputsSelection::set(TrainingStrategy* new_training_strategy_pointer)
139{
140 training_strategy_pointer = new_training_strategy_pointer;
141}
142
143
145
147{
148 trials_number = 1;
149
150 // Stopping criteria
151
152 selection_error_goal = type(0);
153
155
156 maximum_correlation = type(1);
157 minimum_correlation = type(0);
158
159 maximum_time = type(36000.0);
160}
161
162
165
166void InputsSelection::set_trials_number(const Index& new_trials_number)
167{
168#ifdef OPENNN_DEBUG
169
170 if(new_trials_number <= 0)
171 {
172 ostringstream buffer;
173 buffer << "OpenNN Exception: InputsSelection class.\n"
174 << "void set_trials_number(const Index&) method.\n"
175 << "Number of assays must be greater than 0.\n";
176
177 throw logic_error(buffer.str());
178 }
179
180#endif
181
182 trials_number = new_trials_number;
183}
184
185
190
191void InputsSelection::set_display(const bool& new_display)
192{
193 display = new_display;
194}
195
196
199
200void InputsSelection::set_selection_error_goal(const type& new_selection_error_goal)
201{
202#ifdef OPENNN_DEBUG
203
204 if(new_selection_error_goal < 0)
205 {
206 ostringstream buffer;
207
208 buffer << "OpenNN Exception: InputsSelection class.\n"
209 << "void set_selection_error_goal(const type&) method.\n"
210 << "Selection loss goal must be greater or equal than 0.\n";
211
212 throw logic_error(buffer.str());
213 }
214
215#endif
216
217 selection_error_goal = new_selection_error_goal;
218}
219
220
223
224void InputsSelection::set_maximum_epochs_number(const Index& new_maximum_epochs_number)
225{
226 maximum_epochs_number = new_maximum_epochs_number;
227}
228
229
232
233void InputsSelection::set_maximum_time(const type& new_maximum_time)
234{
235#ifdef OPENNN_DEBUG
236
237 if(new_maximum_time < 0)
238 {
239 ostringstream buffer;
240
241 buffer << "OpenNN Exception: InputsSelection class.\n"
242 << "void set_maximum_time(const type&) method.\n"
243 << "Maximum time must be greater than 0.\n";
244
245 throw logic_error(buffer.str());
246 }
247
248#endif
249
250 maximum_time = new_maximum_time;
251}
252
253
256
257void InputsSelection::set_maximum_correlation(const type& new_maximum_correlation)
258{
259#ifdef OPENNN_DEBUG
260
261 if(new_maximum_correlation < 0 || new_maximum_correlation > 1)
262 {
263 ostringstream buffer;
264
265 buffer << "OpenNN Exception: InputsSelection class.\n"
266 << "void set_maximum_correlation(const type&) method.\n"
267 << "Maximum correlation must be comprised between 0 and 1.\n";
268
269 throw logic_error(buffer.str());
270 }
271
272#endif
273
274 maximum_correlation = new_maximum_correlation;
275}
276
277
280
281void InputsSelection::set_minimum_correlation(const type& new_minimum_correlation)
282{
283#ifdef OPENNN_DEBUG
284
285 if(new_minimum_correlation < 0 || new_minimum_correlation > 1)
286 {
287 ostringstream buffer;
288
289 buffer << "OpenNN Exception: InputsSelection class.\n"
290 << "void set_minimum_correlation(const type&) method.\n"
291 << "Minimum correaltion must be comprised between 0 and 1.\n";
292
293 throw logic_error(buffer.str());
294 }
295
296#endif
297
298 minimum_correlation = new_minimum_correlation;
299}
300
301
304
306{
307 return results.write_stopping_condition();
308}
309
310
312
314{
315 ostringstream buffer;
316
318 {
319 buffer << "OpenNN Exception: InputsSelection class.\n"
320 << "void check() const method.\n"
321 << "Pointer to training strategy is nullptr.\n";
322
323 throw logic_error(buffer.str());
324 }
325
326 // Loss index
327
328 const LossIndex* loss_index_pointer = training_strategy_pointer->get_loss_index_pointer();
329
330 // Neural network
331
332 if(!loss_index_pointer->has_neural_network())
333 {
334 buffer << "OpenNN Exception: InputsSelection class.\n"
335 << "void check() const method.\n"
336 << "Pointer to neural network is nullptr.\n";
337
338 throw logic_error(buffer.str());
339 }
340
341 const NeuralNetwork* neural_network_pointer = loss_index_pointer->get_neural_network_pointer();
342
343 if(neural_network_pointer->is_empty())
344 {
345 buffer << "OpenNN Exception: InputsSelection class.\n"
346 << "void check() const method.\n"
347 << "Neural network is empty.\n";
348
349 throw logic_error(buffer.str());
350 }
351
352 // Data set
353
354 if(!loss_index_pointer->has_data_set())
355 {
356 buffer << "OpenNN Exception: InputsSelection class.\n"
357 << "void check() const method.\n"
358 << "Pointer to data set is nullptr.\n";
359
360 throw logic_error(buffer.str());
361 }
362
363 const DataSet* data_set_pointer = loss_index_pointer->get_data_set_pointer();
364
365 const Index selection_samples_number = data_set_pointer->get_selection_samples_number();
366
367 if(selection_samples_number == 0)
368 {
369 buffer << "OpenNN Exception: InputsSelection class.\n"
370 << "void check() const method.\n"
371 << "Number of selection samples is zero.\n";
372
373 throw logic_error(buffer.str());
374 }
375}
376
377
379
381{
382 switch(stopping_condition)
383 {
384 case InputsSelection::StoppingCondition::MaximumTime:
385 return "MaximumTime";
386
387 case InputsSelection::StoppingCondition::SelectionErrorGoal:
388 return "SelectionErrorGoal";
389
390 case InputsSelection::StoppingCondition::MaximumInputs:
391 return "MaximumInputs";
392
393 case InputsSelection::StoppingCondition::MinimumInputs:
394 return "MinimumInputs";
395
396 case InputsSelection::StoppingCondition::MaximumEpochs:
397 return "MaximumEpochs";
398
399 case InputsSelection::StoppingCondition::MaximumSelectionFailures:
400 return "MaximumSelectionFailures";
401
402 case InputsSelection::StoppingCondition::CorrelationGoal:
403 return "CorrelationGoal";
404 }
405
406 return string();
407}
408
409
411
412const string InputsSelection::write_time(const type& time) const
413{
414
415#ifdef OPENNN_DEBUG
416
417 if(time > static_cast<type>(3600e5))
418 {
419 ostringstream buffer;
420
421 buffer << "OpenNN Exception: OptimizationAlgorithm class.\n"
422 << "const string write_time(const type& time) const method.\n"
423 << "Time must be lower than 10e5 seconds.\n";
424
425 throw logic_error(buffer.str());
426 }
427
428 if(time < static_cast<type>(0))
429 {
430 ostringstream buffer;
431
432 buffer << "OpenNN Exception: OptimizationAlgorithm class.\n"
433 << "const string write_time(const type& time) const method.\n"
434 << "Time must be greater than 0.\n";
435
436 throw logic_error(buffer.str());
437 }
438#endif
439
440 int hours = static_cast<int>(time) / 3600;
441 int seconds = static_cast<int>(time) % 3600;
442 int minutes = seconds / 60;
443 seconds = seconds % 60;
444
445 ostringstream elapsed_time;
446
447 elapsed_time << setfill('0') << setw(2) << hours << ":"
448 << setfill('0') << setw(2) << minutes << ":"
449 << setfill('0') << setw(2) << seconds << endl;
450
451 return elapsed_time.str();
452}
453
454
458
459Index InputsSelection::get_input_index(const Tensor<DataSet::VariableUse, 1>& uses, const Index& inputs_number)
460{
461#ifdef OPENNN_DEBUG
462
463 if(uses.size() < inputs_number)
464 {
465 ostringstream buffer;
466
467 buffer << "OpenNN Exception: InputsSelection class.\n"
468 << "const Index get_input_index(const Tensor<DataSet::VariableUse, 1>, const Index) method.\n"
469 << "Size of uses vector("<< uses.size() <<") must be greater than " << inputs_number << ".\n";
470
471 throw logic_error(buffer.str());
472 }
473#endif
474
475 Index i = 0;
476
477 Index j = 0;
478
479 while(i < uses.size())
480 {
481 if(uses[i] == DataSet::VariableUse::Input && inputs_number == j)
482 {
483 break;
484 }
485 else if(uses[i] == DataSet::VariableUse::Input)
486 {
487 i++;
488 j++;
489 }
490 else
491 {
492 i++;
493 }
494 }
495 return i;
496}
497
498}
499
500
501// OpenNN: Open Neural Networks Library.
502// Copyright(C) 2005-2021 Artificial Intelligence Techniques, SL.
503//
504// This library is free software; you can redistribute it and/or
505// modify it under the terms of the GNU Lesser General Public
506// License as published by the Free Software Foundation; either
507// version 2.1 of the License, or any later version.
508//
509// This library is distributed in the hope that it will be useful,
510// but WITHOUT ANY WARRANTY; without even the implied warranty of
511// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
512// Lesser General Public License for more details.
513
514// You should have received a copy of the GNU Lesser General Public
515// License along with this library; if not, write to the Free Software
516// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
This class represents the concept of data set for data modelling problems, such as approximation,...
Definition: data_set.h:57
Index get_selection_samples_number() const
Returns the number of samples in the data set which will be used for selection.
Definition: data_set.cpp:1402
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.
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 &)
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 abstract class represents the concept of loss index composed of an error term and a regularizati...
Definition: loss_index.h:48
bool has_data_set() const
Definition: loss_index.cpp:86
bool has_neural_network() const
Definition: loss_index.cpp:70
NeuralNetwork * get_neural_network_pointer() const
Returns a pointer to the neural network object associated to the error term.
Definition: loss_index.h:70
DataSet * get_data_set_pointer() const
Returns a pointer to the data set object associated to the error term.
Definition: loss_index.h:92
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.
InputsSelection::StoppingCondition stopping_condition
Stopping condition of the algorithm.
string write_stopping_condition() const
Return a string with the stopping condition of the Results.
This structure contains the optimization algorithm results.
string write_stopping_condition() const
Return a string with the stopping condition of the Results.