neurons_selection.cpp
1// OpenNN: Open Neural Networks Library
2// www.opennn.net
3//
4// N E U R O N 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 "neurons_selection.h"
10
11namespace OpenNN
12{
13
15
17{
19
21}
22
23
26
28{
29 training_strategy_pointer = new_training_strategy_pointer;
30
32}
33
34
36
38{
39}
40
41
43
45{
46#ifdef OPENNN_DEBUG
47
49 {
50 ostringstream buffer;
51
52 buffer << "OpenNN Exception: NeuronsSelection class.\n"
53 << "DataSet* get_training_strategy_pointer() const method.\n"
54 << "Training strategy pointer is nullptr.\n";
55
56 throw logic_error(buffer.str());
57 }
58
59#endif
60
62}
63
64
66
68{
69 if(training_strategy_pointer != nullptr)
70 {
71 return true;
72 }
73 else
74 {
75 return false;
76 }
77}
78
79
81
83{
84 return maximum_neurons;
85}
86
87
89
91{
92 return minimum_neurons;
93}
94
95
97
99{
100 return trials_number;
101}
102
103
106
108{
109 return display;
110}
111
112
114
116{
118}
119
120
122
124{
126}
127
128
130
132{
133 return maximum_time;
134}
135
136
139
141{
142 training_strategy_pointer = new_training_strategy_pointer;
143}
144
145
147
149{
150 Index inputs_number;
151 Index outputs_number;
152
153 if(training_strategy_pointer == nullptr
154 || !training_strategy_pointer->has_neural_network())
155 {
156 inputs_number = 0;
157 outputs_number = 0;
158 }
159 else
160 {
162 outputs_number = training_strategy_pointer->get_neural_network_pointer()->get_outputs_number();
163 }
164 // MEMBERS
165
166 minimum_neurons = 1;
167
168 // Heuristic value for the maximum_neurons
169
170 maximum_neurons = 2*(inputs_number + outputs_number);
171 trials_number = 1;
172
173 display = true;
174
175 // Stopping criteria
176
177 selection_error_goal = type(0);
178
180 maximum_time = type(3600);
181}
182
183
186
187void NeuronsSelection::set_maximum_neurons_number(const Index& new_maximum_neurons)
188{
189#ifdef OPENNN_DEBUG
190
191 if(new_maximum_neurons <= 0)
192 {
193 ostringstream buffer;
194
195 buffer << "OpenNN Exception: NeuronsSelection class.\n"
196 << "void set_maximum_neurons_number(const Index&) method.\n"
197 << "maximum_neurons(" << new_maximum_neurons << ") must be greater than 0.\n";
198
199 throw logic_error(buffer.str());
200 }
201
202 if(new_maximum_neurons < minimum_neurons)
203 {
204 ostringstream buffer;
205
206 buffer << "OpenNN Exception: NeuronsSelection class.\n"
207 << "void set_maximum_neurons_number(const Index&) method.\n"
208 << "maximum_neurons(" << new_maximum_neurons << ") must be equal or greater than minimum_neurons(" << minimum_neurons << ").\n";
209
210 throw logic_error(buffer.str());
211 }
212
213#endif
214
215 maximum_neurons = new_maximum_neurons;
216}
217
218
221
222void NeuronsSelection::set_minimum_neurons(const Index& new_minimum_neurons)
223{
224#ifdef OPENNN_DEBUG
225
226 if(new_minimum_neurons <= 0)
227 {
228 ostringstream buffer;
229
230 buffer << "OpenNN Exception: NeuronsSelection class.\n"
231 << "void set_minimum_neurons(const Index&) method.\n"
232 << "minimum_neurons(" << new_minimum_neurons << ") must be greater than 0.\n";
233
234 throw logic_error(buffer.str());
235 }
236
237 if(new_minimum_neurons >= maximum_neurons)
238 {
239 ostringstream buffer;
240 buffer << "OpenNN Exception: NeuronsSelection class.\n"
241 << "void set_minimum_neurons(const Index&) method.\n"
242 << "minimum_neurons(" << new_minimum_neurons << ") must be less than maximum_neurons(" << maximum_neurons << ").\n";
243
244 throw logic_error(buffer.str());
245 }
246
247#endif
248
249 minimum_neurons = new_minimum_neurons;
250}
251
252
255
256void NeuronsSelection::set_trials_number(const Index& new_trials_number)
257{
258#ifdef OPENNN_DEBUG
259
260 if(new_trials_number <= 0)
261 {
262 ostringstream buffer;
263 buffer << "OpenNN Exception: NeuronsSelection class.\n"
264 << "void set_trials_number(const Index&) method.\n"
265 << "Number of assays must be greater than 0.\n";
266
267 throw logic_error(buffer.str());
268 }
269
270#endif
271
272 trials_number = new_trials_number;
273}
274
275
280
281void NeuronsSelection::set_display(const bool& new_display)
282{
283 display = new_display;
284}
285
286
289
290void NeuronsSelection::set_selection_error_goal(const type& new_selection_error_goal)
291{
292#ifdef OPENNN_DEBUG
293
294 if(new_selection_error_goal < 0)
295 {
296 ostringstream buffer;
297
298 buffer << "OpenNN Exception: NeuronsSelection class.\n"
299 << "void set_selection_error_goal(const type&) method.\n"
300 << "Selection loss goal must be greater or equal than 0.\n";
301
302 throw logic_error(buffer.str());
303 }
304
305#endif
306
307 selection_error_goal = new_selection_error_goal;
308}
309
310
313
314void NeuronsSelection::set_maximum_epochs_number(const Index& new_maximum_epochs_number)
315{
316#ifdef OPENNN_DEBUG
317
318 if(new_maximum_epochs_number <= 0)
319 {
320 ostringstream buffer;
321
322 buffer << "OpenNN Exception: NeuronsSelection class.\n"
323 << "void set_maximum_epochs_number(const Index&) method.\n"
324 << "Maximum epochs number must be greater than 0.\n";
325
326 throw logic_error(buffer.str());
327 }
328
329#endif
330
331 maximum_epochs_number = new_maximum_epochs_number;
332}
333
334
337
338void NeuronsSelection::set_maximum_time(const type& new_maximum_time)
339{
340#ifdef OPENNN_DEBUG
341
342 if(new_maximum_time < 0)
343 {
344 ostringstream buffer;
345
346 buffer << "OpenNN Exception: NeuronsSelection class.\n"
347 << "void set_maximum_time(const type&) method.\n"
348 << "Maximum time must be greater than 0.\n";
349
350 throw logic_error(buffer.str());
351 }
352
353#endif
354
355 maximum_time = new_maximum_time;
356}
357
358
361
363{
364 return results.write_stopping_condition();
365}
366
367
369
371{
372 selection_error_history.resize(0);
373}
374
375
377
379{
380 training_error_history.resize(0);
381}
382
383
385
387{
388 // Optimization algorithm
389
390 ostringstream buffer;
391
393 {
394 buffer << "OpenNN Exception: NeuronsSelection class.\n"
395 << "void check() const method.\n"
396 << "Pointer to training strategy is nullptr.\n";
397
398 throw logic_error(buffer.str());
399 }
400
401 // Loss index
402
403 const LossIndex* loss_index_pointer = training_strategy_pointer->get_loss_index_pointer();
404
405 if(!loss_index_pointer)
406 {
407 buffer << "OpenNN Exception: NeuronsSelection class.\n"
408 << "void check() const method.\n"
409 << "Pointer to loss index is nullptr.\n";
410
411 throw logic_error(buffer.str());
412 }
413
414 // Neural network
415
416 const NeuralNetwork* neural_network_pointer = loss_index_pointer->get_neural_network_pointer();
417
418 if(!neural_network_pointer)
419 {
420 buffer << "OpenNN Exception: NeuronsSelection class.\n"
421 << "void check() const method.\n"
422 << "Pointer to neural network is nullptr.\n";
423
424 throw logic_error(buffer.str());
425 }
426
427 if(neural_network_pointer->is_empty())
428 {
429 buffer << "OpenNN Exception: NeuronsSelection class.\n"
430 << "void check() const method.\n"
431 << "Multilayer Perceptron is empty.\n";
432
433 throw logic_error(buffer.str());
434 }
435
436 if(neural_network_pointer->get_layers_number() == 1)
437 {
438 buffer << "OpenNN Exception: NeuronsSelection class.\n"
439 << "void check() const method.\n"
440 << "Number of layers in neural network must be greater than 1.\n";
441
442 throw logic_error(buffer.str());
443 }
444
445 // Data set
446
447 const DataSet* data_set_pointer = loss_index_pointer->get_data_set_pointer();
448
449 if(!data_set_pointer)
450 {
451 buffer << "OpenNN Exception: NeuronsSelection class.\n"
452 << "void check() const method.\n"
453 << "Pointer to data set is nullptr.\n";
454
455 throw logic_error(buffer.str());
456 }
457
458 const Index selection_samples_number = data_set_pointer->get_selection_samples_number();
459
460 if(selection_samples_number == 0)
461 {
462 buffer << "OpenNN Exception: NeuronsSelection class.\n"
463 << "void check() const method.\n"
464 << "Number of selection samples is zero.\n";
465
466 throw logic_error(buffer.str());
467 }
468}
469
470
472
473const string NeuronsSelection::write_time(const type& time) const
474{
475#ifdef OPENNN_DEBUG
476
477 if(time > static_cast<type>(3600e5))
478 {
479 ostringstream buffer;
480
481 buffer << "OpenNN Exception: OptimizationAlgorithm class.\n"
482 << "const string write_time(const type& time) const method.\n"
483 << "Time must be lower than 10e5 seconds.\n";
484
485 throw logic_error(buffer.str());
486 }
487
488 if(time < static_cast<type>(0))
489 {
490 ostringstream buffer;
491
492 buffer << "OpenNN Exception: OptimizationAlgorithm class.\n"
493 << "const string write_time(const type& time) const method.\n"
494 << "Time must be greater than 0.\n";
495
496 throw logic_error(buffer.str());
497 }
498#endif
499
500 const int hours = static_cast<int>(time) / 3600;
501 int seconds = static_cast<int>(time) % 3600;
502 const int minutes = seconds / 60;
503 seconds = seconds % 60;
504
505 ostringstream elapsed_time;
506
507 elapsed_time << setfill('0') << setw(2) << hours << ":"
508 << setfill('0') << setw(2) << minutes << ":"
509 << setfill('0') << setw(2) << seconds << endl;
510
511 return elapsed_time.str();
512}
513
514
516
518{
519 switch(stopping_condition)
520 {
521 case NeuronsSelection::StoppingCondition::MaximumTime:
522 return "MaximumTime";
523
524 case NeuronsSelection::StoppingCondition::SelectionErrorGoal:
525 return "SelectionErrorGoal";
526
527 case NeuronsSelection::StoppingCondition::MaximumEpochs:
528 return "MaximumEpochs";
529
530 case NeuronsSelection::StoppingCondition::MaximumSelectionFailures:
531 return "MaximumSelectionFailures";
532
533 case NeuronsSelection::StoppingCondition::MaximumNeurons:
534 return "MaximumNeurons";
535 }
536
537 return string();
538}
539
540}
541
542// OpenNN: Open Neural Networks Library.
543// Copyright(C) 2005-2021 Artificial Intelligence Techniques, SL.
544//
545// This library is free software; you can redistribute it and/or
546// modify it under the terms of the GNU Lesser General Public
547// License as published by the Free Software Foundation; either
548// version 2.1 of the License, or any later version.
549//
550// This library is distributed in the hope that it will be useful,
551// but WITHOUT ANY WARRANTY; without even the implied warranty of
552// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
553// Lesser General Public License for more details.
554
555// You should have received a copy of the GNU Lesser General Public
556// License along with this library; if not, write to the Free Software
557// 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
This abstract class represents the concept of loss index composed of an error term and a regularizati...
Definition: loss_index.h:48
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
Index get_inputs_number() const
Returns the number of inputs to the neural network.
Index get_layers_number() const
Tensor< type, 1 > selection_error_history
Selection loss of all the neural networks trained.
Index minimum_neurons
Minimum number of hidden neurons.
void set_training_strategy_pointer(TrainingStrategy *)
NeuronsSelection()
Default constructor.
const type & get_maximum_time() const
Returns the maximum time in the neurons 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 neurons selection object to their default values.
Index maximum_neurons
Maximum number of hidden neurons.
void delete_training_error_history()
Delete the history of the loss values.
virtual ~NeuronsSelection()
Destructor.
void check() const
Checks that the different pointers needed for performing the neurons selection are not nullptr.
const Index & get_minimum_neurons() const
Returns the minimum of the hidden perceptrons number used in the neurons selection.
Index trials_number
Number of trials for each neural network.
const Index & get_maximum_epochs_number() const
Returns the maximum number of epochs in the neurons selection algorithm.
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.
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 neurons selection algorithm.
void set_maximum_neurons_number(const Index &)
TrainingStrategy * get_training_strategy_pointer() const
Returns a pointer to the training strategy object.
const Index & get_maximum_neurons() const
Returns the maximum of the hidden perceptrons number used in the neurons selection.
Tensor< type, 1 > training_error_history
Error of all the neural networks trained.
void set_minimum_neurons(const Index &)
Index maximum_epochs_number
Maximum number of epochs to perform neurons selection. It is used as a stopping criterion.
void set_display(const bool &)
string write_stopping_condition(const TrainingResults &) const
bool has_training_strategy() const
Returns true if this neurons selection algorithm has a training strategy associated,...
void set_trials_number(const Index &)
void delete_selection_history()
Delete the history of the selection error values.
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.
NeuralNetwork * get_neural_network_pointer() const
Returns a pointer to the NeuralNetwork class.
NeuronsSelection::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.