neurons_selection.h
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 H E A D E R
5//
6// Artificial Intelligence Techniques SL
7// artelnics@artelnics.com
8
9#ifndef NEURONSSELECTION_H
10#define NEURONSSELECTION_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
21// OpenNN includes
22
23#include "config.h"
24#include "training_strategy.h"
25
26namespace OpenNN
27{
28
29struct NeuronsSelectionResults;
30
32
37
39{
40public:
41
42 // Constructors
43
44 explicit NeuronsSelection();
45
47
48 // Destructor
49
50 virtual ~NeuronsSelection();
51
52 // Enumerators
53
55
56 enum class StoppingCondition{MaximumTime, SelectionErrorGoal, MaximumEpochs, MaximumSelectionFailures, MaximumNeurons};
57
58 // Get methods
59
61
62 bool has_training_strategy() const;
63
64 const Index& get_maximum_neurons() const;
65 const Index& get_minimum_neurons() const;
66 const Index& get_trials_number() const;
67
68 const bool& get_display() const;
69
70 const type& get_selection_error_goal() const;
71 const Index& get_maximum_epochs_number() const;
72 const type& get_maximum_time() const;
73
74 // Set methods
75
77
78 void set_default();
79
80 void set_maximum_neurons_number(const Index&);
81 void set_minimum_neurons(const Index&);
82 void set_trials_number(const Index&);
83
84 void set_display(const bool&);
85
86 void set_selection_error_goal(const type&);
87 void set_maximum_epochs_number(const Index&);
88 void set_maximum_time(const type&);
89
90 // Loss calculation methods
91
92 string write_stopping_condition(const TrainingResults&) const;
93
94 // Neuron selection methods
95
98 void check() const;
99
100 // Utilities
101
103
105
107
108 const string write_time(const type&) const;
109
110protected:
111
113
115
117
118 Tensor<Index, 1> neurons_history;
119
121
122 Tensor<type, 1> selection_error_history;
123
125
126 Tensor<type, 1> training_error_history;
127
129
131
133
135
137
138 Index trials_number = 1;
139
141
142 bool display = true;
143
145
147
149
151
153
155};
156
157
159
161{
162 // Default constructor
163
164 explicit NeuronsSelectionResults() {}
165
166 // Epochs constructor
167
168 explicit NeuronsSelectionResults(const Index& maximum_epochs_number)
169 {
170 neurons_number_history.resize(maximum_epochs_number);
171 neurons_number_history.setConstant(0);
172
173 training_error_history.resize(maximum_epochs_number);
174 training_error_history.setConstant(type(-1));
175
176 selection_error_history.resize(maximum_epochs_number);
177 selection_error_history.setConstant(type(-1));
178
179 optimum_training_error = numeric_limits<type>::max();
180 optimum_selection_error = numeric_limits<type>::max();
181 }
182
183 virtual ~NeuronsSelectionResults() {}
184
185 void resize_history(const Index& new_size)
186 {
187 const Tensor<Index, 1> old_neurons_number_history = neurons_number_history;
188 const Tensor<type, 1> old_training_error_history = training_error_history;
189 const Tensor<type, 1> old_selection_error_history = selection_error_history;
190
191 neurons_number_history.resize(new_size);
192 training_error_history.resize(new_size);
193 selection_error_history.resize(new_size);
194
195 for(Index i = 0; i < new_size; i++)
196 {
197 neurons_number_history(i) = old_neurons_number_history(i);
198 training_error_history(i) = old_training_error_history(i);
199 selection_error_history(i) = old_selection_error_history(i);
200 }
201 }
202
203 string write_stopping_condition() const;
204
205 void print() const
206 {
207 cout << endl;
208 cout << "Neurons Selection Results" << endl;
209
210 cout << "Optimal neurons number: " << optimal_neurons_number << endl;
211
212 cout << "Optimum training error: " << optimum_training_error << endl;
213 cout << "Optimum selection error: " << optimum_selection_error << endl;
214 }
215
216 // Neural network
217
219
220 Tensor<Index, 1> neurons_number_history;
221
223
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
243
245
247
248 // Model selection
249
251
252 NeuronsSelection::StoppingCondition stopping_condition = NeuronsSelection::StoppingCondition::MaximumTime;
253
255
257};
258
259
260}
261
262#endif
263
264
265// OpenNN: Open Neural Networks Library.
266// Copyright(C) 2005-2021 Artificial Intelligence Techniques, SL.
267//
268// This library is free software; you can redistribute it and/or
269// modify it under the terms of the GNU Lesser General Public
270// License as published by the Free Software Foundation; either
271// version 2.1 of the License, or any later version.
272//
273// This library is distributed in the hope that it will be useful,
274// but WITHOUT ANY WARRANTY; without even the implied warranty of
275// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
276// Lesser General Public License for more details.
277
278// You should have received a copy of the GNU Lesser General Public
279// License along with this library; if not, write to the Free Software
280// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
This abstract class represents the concept of neurons selection algorithm for a ModelSelection[1].
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.
Tensor< Index, 1 > neurons_history
Neurons of all the neural networks trained.
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.
virtual NeuronsSelectionResults perform_neurons_selection()=0
Performs the neurons selection for a neural network.
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 &)
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 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.
This structure contains the results from the neurons selection.
type optimum_training_error
Value of loss for the neural network with minimum selection error.
Tensor< type, 1 > selection_error_history
Selection loss of the different neural networks.
Index optimal_neurons_number
Neurons of the neural network with minimum selection error.
Tensor< type, 1 > optimal_parameters
Vector of parameters for the neural network with minimum selection error.
type optimum_selection_error
Value of minimum selection error.
Tensor< type, 1 > training_error_history
Performance of the different neural networks.
NeuronsSelection::StoppingCondition stopping_condition
Stopping condition of the algorithm.
string elapsed_time
Elapsed time during the loss of the algortihm.
Tensor< Index, 1 > neurons_number_history
Neurons of the diferent neural networks.
string write_stopping_condition() const
Return a string with the stopping condition of the Results.
This structure contains the optimization algorithm results.