optimization_algorithm.h
1// OpenNN: Open Neural Networks Library
2// www.opennn.net
3//
4// O P T I M I Z A 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 OPTIMIZATIONALGORITHM_H
10#define OPTIMIZATIONALGORITHM_H
11
12// System includes
13
14#include <iostream>
15#include <fstream>
16#include <algorithm>
17#include <functional>
18#include <limits>
19#include <cmath>
20#include <ctime>
21#include <iomanip>
22
23// OpenNN includes
24
25#include "config.h"
26#include "tensor_utilities.h"
27#include "loss_index.h"
28
29using namespace std;
30using namespace Eigen;
31
32
33namespace OpenNN
34{
35
36struct TrainingResults;
37
40
42{
43
44public:
45
46 explicit OptimizationAlgorithm();
47
49
50 virtual ~OptimizationAlgorithm();
51
53
54 enum class StoppingCondition{MinimumLossDecrease, LossGoal,
55 MaximumSelectionErrorIncreases, MaximumEpochsNumber, MaximumTime};
56
57 // Get methods
58
60
62 string get_hardware_use() const;
63 void set_hardware_use(const string&);
64
65 bool has_loss_index() const;
66
67 // Utilities
68
69 const bool& get_display() const;
70
71 const Index& get_display_period() const;
72
73 const Index& get_save_period() const;
74
75 const string& get_neural_network_file_name() const;
76
78
79 const string write_time(const type&) const;
80
81 // Set methods
82
83 void set();
84
85 virtual void set_default();
86
87 virtual void set_threads_number(const int&);
88
89 virtual void set_loss_index_pointer(LossIndex*);
90
91 virtual void set_display(const bool&);
92
93 void set_display_period(const Index&);
94
95 void set_save_period(const Index&);
96 void set_neural_network_file_name(const string&);
97
98 // Training methods
99
100 virtual void check() const;
101
103
105
106 virtual string write_optimization_algorithm_type() const {return string();}
107
108 // Serialization methods
109
110 virtual void print() const;
111
112 virtual Tensor<string, 2> to_string_matrix() const;
113
114 virtual void from_XML(const tinyxml2::XMLDocument&);
115
116 virtual void write_XML(tinyxml2::XMLPrinter&) const;
117
118 void save(const string&) const;
119 void load(const string&);
120
121protected:
122
123 NonBlockingThreadPool* non_blocking_thread_pool = nullptr;
124 ThreadPoolDevice* thread_pool_device;
125
127
129
131
132 Index epochs_number = 10000;
133
134 // UTILITIES
135
137
138 string hardware_use = "Multi-core";
139
141
142 Index display_period = 10;
143
145
146 Index save_period = numeric_limits<Index>::max();
147
149
150 string neural_network_file_name = "neural_network.xml";
151
153
154 bool display = true;
155
156 const Eigen::array<IndexPair<Index>, 1> AT_B = {IndexPair<Index>(0, 0)};
157 const Eigen::array<IndexPair<Index>, 1> product_vector_matrix = {IndexPair<Index>(0, 1)}; // Normal product vector times matrix
158 const Eigen::array<IndexPair<Index>, 1> A_B = {IndexPair<Index>(1, 0)};
159
160#ifdef OPENNN_CUDA
161 #include "../../opennn-cuda/opennn-cuda/optimization_algorithm_cuda.h"
162#endif
163
164};
165
166
168{
170 {
171 }
172
174 {
175 }
176
177 void print() const
178 {
179 cout << "Potential parameters:" << endl;
180 cout << potential_parameters << endl;
181
182 cout << "Training direction:" << endl;
183 cout << training_direction << endl;
184
185 cout << "Initial learning rate:" << endl;
186 cout << initial_learning_rate << endl;
187 }
188
189 Tensor<type, 1> potential_parameters;
190 Tensor<type, 1> training_direction;
191 type initial_learning_rate = type(0);
192};
193
194
196
198{
200
202 {
203 }
204
205 explicit TrainingResults(const Index& epochs_number)
206 {
207 training_error_history.resize(1+epochs_number);
208 training_error_history.setConstant(type(-1.0));
209
210 selection_error_history.resize(1+epochs_number);
211 selection_error_history.setConstant(type(-1.0));
212 }
213
215
216 virtual ~TrainingResults() {}
217
218 string write_stopping_condition() const;
219
220 type get_training_error()
221 {
222 const Index size = training_error_history.size();
223
224 return training_error_history(size-1);
225 }
226
227 type get_selection_error()
228 {
229 const Index size = selection_error_history.size();
230
231 return selection_error_history(size-1);
232 }
233
235
236 void save(const string&) const;
237
238 void print(const string& message = string())
239 {
240 cout << message << endl;
241
242 const Index epochs_number = training_error_history.size();
243
244 cout << "Training results" << endl;
245 cout << "Epochs number: " << epochs_number-1 << endl;
246
247 cout << "Training error: " << training_error_history(epochs_number-1) << endl;
248
249 if(abs(training_error_history(epochs_number-1) + type(1)) < type(NUMERIC_LIMITS_MIN))
250 cout << "Selection error: " << selection_error_history(epochs_number-1) << endl;
251
252 cout << "Stopping condition: " << write_stopping_condition() << endl;
253 }
254
256
257 OptimizationAlgorithm::StoppingCondition stopping_condition = OptimizationAlgorithm::StoppingCondition::MaximumTime;
258
260
261 Tensor<string, 2> write_final_results(const Index& = 3) const;
262
264
265 void resize_training_error_history(const Index&);
266
268
269 void resize_selection_error_history(const Index&);
270
271 // Training history
272
274
275 Tensor<type, 1> training_error_history;
276
278
279 Tensor<type, 1> selection_error_history;
280
282
284
285};
286
287}
288
289#endif
290
291
292// OpenNN: Open Neural Networks Library.
293// Copyright(C) 2005-2021 Artificial Intelligence Techniques, SL.
294//
295// This library is free software; you can redistribute it and/or
296// modify it under the terms of the GNU Lesser General Public
297// License as published by the Free Software Foundation; either
298// version 2.1 of the License, or any later version.
299//
300// This library is distributed in the hope that it will be useful,
301// but WITHOUT ANY WARRANTY; without even the implied warranty of
302// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
303// Lesser General Public License for more details.
304
305// You should have received a copy of the GNU Lesser General Public
306// License along with this library; if not, write to the Free Software
307// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
This abstract class represents the concept of loss index composed of an error term and a regularizati...
Definition: loss_index.h:48
string neural_network_file_name
Path where the neural network is saved.
virtual void set_loss_index_pointer(LossIndex *)
void set_hardware_use(const string &)
Set hardware to use. Default: Multi-core.
string get_hardware_use() const
Hardware use.
virtual void from_XML(const tinyxml2::XMLDocument &)
virtual void set_default()
Sets the members of the optimization algorithm object to their default values.
void set_neural_network_file_name(const string &)
LossIndex * loss_index_pointer
Pointer to a loss index for a neural network object.
bool display
Display messages to screen.
virtual Tensor< string, 2 > to_string_matrix() const
const Index & get_display_period() const
Returns the number of iterations between the training showing progress.
const string write_time(const type &) const
Writes the time from seconds in format HH:mm:ss.
Index save_period
Number of iterations between the training saving progress.
const string & get_neural_network_file_name() const
Returns the file name where the neural network will be saved.
const Index & get_save_period() const
Returns the number of iterations between the training saving progress.
virtual void print() const
Prints to the screen the XML-type representation of the optimization algorithm object.
virtual void set_display(const bool &)
virtual void write_XML(tinyxml2::XMLPrinter &) const
StoppingCondition
Enumeration of all possibles condition of stop for the algorithms.
virtual ~OptimizationAlgorithm()
Destructor.
virtual TrainingResults perform_training()=0
Trains a neural network which has a loss index associated.
Index epochs_number
Number of training epochs in the neural network.
Index display_period
Number of iterations between the training showing progress.
HALF_CONSTEXPR half abs(half arg)
Definition: half.hpp:2735
Extensions to the C++ standard library.
Definition: half.hpp:2325
This structure contains the optimization algorithm results.
Tensor< type, 1 > selection_error_history
History of the selection error over the training iterations.
Tensor< string, 2 > write_final_results(const Index &=3) const
Writes final results of the training.
void resize_training_error_history(const Index &)
Resizes the training error history keeping the values.
void save(const string &) const
Returns a string representation of the results structure.
OptimizationAlgorithm::StoppingCondition stopping_condition
Stopping condition of the algorithm.
TrainingResults()
Default constructor.
virtual ~TrainingResults()
Destructor.
void resize_selection_error_history(const Index &)
Resizes the selection error history keeping the values.
Tensor< type, 1 > training_error_history
History of the loss function loss over the training iterations.
string elapsed_time
Elapsed time of the training process.
string write_stopping_condition() const
Return a string with the stopping condition of the Results.