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 A L G O R I T H M 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 27 namespace OpenNN 28 { 29 30 /// This abstract class represents the concept of inputs selection algorithm for a ModelSelection[1]. 31 32 /// 33 /// Any derived class must implement the perform_inputs_selection() method. 34 /// 35 /// [1] Neural Designer "Model Selection Algorithms in Predictive Analytics." 36 /// \ref https://www.neuraldesigner.com/blog/model-selection 37 38 class InputsSelection 39 { 40 public: 41 42 // Constructors 43 44 explicit InputsSelection(); 45 46 explicit InputsSelection(TrainingStrategy*); 47 48 // Destructor 49 50 virtual ~InputsSelection(); 51 52 // Enumerations 53 54 /// Enumeration of all possibles condition of stop for the algorithms. 55 56 enum StoppingCondition{MaximumTime,SelectionErrorGoal,MaximumInputs,MinimumInputs,MaximumEpochs, 57 MaximumSelectionFailures,CorrelationGoal,AlgorithmFinished}; 58 59 // STRUCTURES 60 61 /// This structure contains the results from the inputs selection. 62 63 struct Results 64 { ResultsResults65 explicit Results() {} 66 ~ResultsResults67 virtual ~Results() {} 68 69 string write_stopping_condition() const; 70 71 /// Inputs of the different neural networks. 72 73 Tensor<bool, 2> inputs_data; 74 75 /// Performance of the different neural networks. 76 77 Tensor<type, 1> training_error_data; 78 79 /// Selection loss of the different neural networks. 80 81 Tensor<type, 1> selection_error_data; 82 83 /// Vector of parameters for the neural network with minimum selection error. 84 85 Tensor<type, 1> minimal_parameters; 86 87 /// Value of minimum selection error. 88 89 type final_selection_error; 90 91 /// Value of loss for the neural network with minimum selection error. 92 93 type final_training_error; 94 95 /// Inputs of the neural network with minimum selection error. 96 97 Tensor<Index, 1> optimal_inputs_indices; 98 99 /// Inputs of the neural network with minimum selection error. 100 101 Tensor<bool, 1> optimal_inputs; 102 103 /// Number of iterations to perform the inputs selection. 104 105 Index iterations_number; 106 107 /// Stopping condition of the algorithm. 108 109 StoppingCondition stopping_condition; 110 111 /// Elapsed time during the loss of the algortihm. 112 113 string elapsed_time; 114 }; 115 116 // Get methods 117 118 const bool& get_approximation() const; 119 120 TrainingStrategy* get_training_strategy_pointer() const; 121 122 bool has_training_strategy() const; 123 124 const Index& get_trials_number() const; 125 126 const bool& get_reserve_training_error_data() const; 127 const bool& get_reserve_selection_error_data() const; 128 const bool& get_reserve_minimal_parameters() const; 129 130 const bool& get_display() const; 131 132 const type& get_selection_error_goal() const; 133 const Index& get_maximum_iterations_number() const; 134 const type& get_maximum_time() const; 135 const type& get_maximum_correlation() const; 136 const type& get_minimum_correlation() const; 137 const type& get_tolerance() const; 138 139 // Set methods 140 141 void set_approximation(const bool&); 142 143 void set_training_strategy_pointer(TrainingStrategy*); 144 145 void set_default(); 146 147 void set_trials_number(const Index&); 148 149 void set_reserve_training_error_data(const bool&); 150 void set_reserve_selection_error_data(const bool&); 151 void set_reserve_minimal_parameters(const bool&); 152 153 void set_display(const bool&); 154 155 void set_selection_error_goal(const type&); 156 void set_maximum_iterations_number(const Index&); 157 void set_maximum_time(const type&); 158 void set_maximum_correlation(const type&); 159 void set_minimum_correlation(const type&); 160 void set_tolerance(const type&); 161 162 // Performances calculation methods 163 164 Tensor<type, 1> calculate_losses(const Tensor<bool, 1>&); 165 166 Tensor<type, 1> get_parameters_inputs(const Tensor<bool, 1>&) const; 167 168 string write_stopping_condition(const OptimizationAlgorithm::Results&) const; 169 170 // inputs selection methods 171 172 void delete_selection_history(); 173 void delete_loss_history(); 174 void delete_parameters_history(); 175 void check() const; 176 177 // Utilities 178 179 Tensor<type, 1> insert_result(const type&, const Tensor<type, 1>&) const; 180 Tensor<Index, 1> insert_result(const Index&, const Tensor<Index, 1>&) const; 181 Tensor< Tensor<type, 1>, 1> insert_result(const Tensor<type, 1>&, const Tensor< Tensor<type, 1>, 1>&) const; 182 183 Tensor<Index, 1> delete_result(const Index&, const Tensor<Index, 1>&) const; 184 185 Index get_input_index(const Tensor<DataSet::VariableUse, 1>, const Index); 186 187 /// Performs the inputs selection for a neural network. 188 189 virtual Results* perform_inputs_selection() = 0; 190 191 /// Writes the time from seconds in format HH:mm:ss. 192 193 const string write_elapsed_time(const type&) const; 194 195 protected: 196 197 /// Pointer to a training strategy object. 198 199 TrainingStrategy* training_strategy_pointer = nullptr; 200 201 /// True if this is a function regression problem. 202 203 bool approximation; 204 205 /// Inputs of all the neural networks trained. 206 207 Tensor<bool, 2> inputs_history; 208 209 /// Selection loss of all the neural networks trained. 210 211 Tensor<type, 1> selection_error_history; 212 213 /// Performance of all the neural networks trained. 214 215 Tensor<type, 1> training_error_history; 216 217 /// Parameters of all the neural network trained. 218 219 Tensor<Tensor<type, 1>, 1> parameters_history; 220 221 /// Number of trials for each neural network. 222 223 Index trials_number = 1; 224 225 // Inputs selection results 226 227 /// True if the loss of all neural networks are to be reserved. 228 229 bool reserve_training_error_data; 230 231 /// True if the selection error of all neural networks are to be reserved. 232 233 bool reserve_selection_error_data; 234 235 /// True if the vector parameters of the neural network presenting minimum selection error is to be reserved. 236 237 bool reserve_minimal_parameters; 238 239 /// Display messages to screen. 240 241 bool display = true; 242 243 // Stopping criteria 244 245 /// Goal value for the selection error. It is used as a stopping criterion. 246 247 type selection_error_goal; 248 249 /// Maximum number of epochs to perform_inputs_selection. It is used as a stopping criterion. 250 251 Index maximum_epochs_number; 252 253 /// Maximum value for the correlations. 254 255 type maximum_correlation; 256 257 /// Minimum value for the correlations. 258 259 type minimum_correlation; 260 261 /// Maximum selection algorithm time. It is used as a stopping criterion. 262 263 type maximum_time; 264 265 /// Tolerance for the error in the trainings of the algorithm. 266 267 type tolerance; 268 }; 269 } 270 271 #endif 272 273 // OpenNN: Open Neural Networks Library. 274 // Copyright(C) 2005-2020 Artificial Intelligence Techniques, SL. 275 // 276 // This library is free software; you can redistribute it and/or 277 // modify it under the terms of the GNU Lesser General Public 278 // License as published by the Free Software Foundation; either 279 // version 2.1 of the License, or any later version. 280 // 281 // This library is distributed in the hope that it will be useful, 282 // but WITHOUT ANY WARRANTY; without even the implied warranty of 283 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 284 // Lesser General Public License for more details. 285 286 // You should have received a copy of the GNU Lesser General Public 287 // License along with this library; if not, write to the Free Software 288 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 289