loss_index.h
1// OpenNN: Open Neural Networks Library
2// www.opennn.net
3//
4// L O S S I N D E X C L A S S H E A D E R
5//
6// Artificial Intelligence Techniques SL
7// artelnics@artelnics.com
8
9#ifndef LOSSINDEX_H
10#define LOSSINDEX_H
11
12// System includes
13
14#include <string>
15#include <sstream>
16#include <fstream>
17#include <ostream>
18#include <iostream>
19#include <cmath>
20
21// OpenNN includes
22
23#include "config.h"
24
25#include "data_set.h"
26#include "neural_network.h"
27
28namespace OpenNN
29{
30
31struct LossIndexBackPropagation;
32struct LossIndexBackPropagationLM;
33
35
46
48{
49
50public:
51
52 // Constructors
53
54 explicit LossIndex();
55
56 explicit LossIndex(NeuralNetwork*, DataSet*);
57
58 // Destructor
59
60 virtual ~LossIndex();
61
62 // Methods
63
65
66 enum class RegularizationMethod{L1, L2, NoRegularization};
67
69
71 {
72 #ifdef OPENNN_DEBUG
73
75 {
76 ostringstream buffer;
77
78 buffer << "OpenNN Exception: LossIndex class.\n"
79 << "NeuralNetwork* get_neural_network_pointer() const method.\n"
80 << "Neural network pointer is nullptr.\n";
81
82 throw logic_error(buffer.str());
83 }
84
85 #endif
86
88 }
89
91
93 {
94 #ifdef OPENNN_DEBUG
95
97 {
98 ostringstream buffer;
99
100 buffer << "OpenNN Exception: LossIndex class.\n"
101 << "DataSet* get_data_set_pointer() const method.\n"
102 << "DataSet pointer is nullptr.\n";
103
104 throw logic_error(buffer.str());
105 }
106
107 #endif
108
109 return data_set_pointer;
110 }
111
112 const type& get_regularization_weight() const;
113
114 const bool& get_display() const;
115
116 bool has_neural_network() const;
117
118 bool has_data_set() const;
119
120 // Get methods
121
123
124 // Set methods
125
126 void set();
127 void set(NeuralNetwork*);
128 void set(DataSet*);
129 void set(NeuralNetwork*, DataSet*);
130
131 void set(const LossIndex&);
132
133 void set_threads_number(const int&);
134
136
137 virtual void set_data_set_pointer(DataSet*);
138
139 void set_default();
140
142 void set_regularization_method(const string&);
143 void set_regularization_weight(const type&);
144
145 void set_display(const bool&);
146
147 virtual void set_normalization_coefficient() {}
148
149 bool has_selection() const;
150
151 // Numerical differentiation
152
153 type calculate_eta() const;
154 type calculate_h(const type&) const;
155
156 Tensor<type, 1> calculate_gradient_numerical_differentiation();
157
158 Tensor<type, 2> calculate_jacobian_numerical_differentiation();
159
160 // Back propagation
161
162 void calculate_errors(const DataSetBatch&,
163 const NeuralNetworkForwardPropagation&,
164 LossIndexBackPropagation&) const;
165
166 virtual void calculate_error(const DataSetBatch&,
167 const NeuralNetworkForwardPropagation&,
168 LossIndexBackPropagation&) const = 0;
169
170 virtual void calculate_output_delta(const DataSetBatch&,
171 NeuralNetworkForwardPropagation&,
172 LossIndexBackPropagation&) const = 0;
173
174 void calculate_layers_delta(const DataSetBatch&,
175 NeuralNetworkForwardPropagation&,
176 LossIndexBackPropagation&) const;
177
178 void calculate_error_gradient(const DataSetBatch&,
179 const NeuralNetworkForwardPropagation&,
180 LossIndexBackPropagation&) const;
181
182 void back_propagate(const DataSetBatch&,
183 NeuralNetworkForwardPropagation&,
184 LossIndexBackPropagation&) const;
185
186 // Back propagation LM
187
188 void calculate_errors_lm(const DataSetBatch&,
189 const NeuralNetworkForwardPropagation&,
190 LossIndexBackPropagationLM&) const; // general
191
192 virtual void calculate_squared_errors_lm(const DataSetBatch&,
193 const NeuralNetworkForwardPropagation&,
194 LossIndexBackPropagationLM&) const;
195
196 virtual void calculate_error_lm(const DataSetBatch&,
197 const NeuralNetworkForwardPropagation&,
198 LossIndexBackPropagationLM&) const {}
199
200 virtual void calculate_output_delta_lm(const DataSetBatch&,
201 NeuralNetworkForwardPropagation&,
202 LossIndexBackPropagationLM&) const {}
203
204 void calculate_layers_delta_lm(const DataSetBatch&,
205 NeuralNetworkForwardPropagation&,
206 LossIndexBackPropagationLM&) const;
207
208 virtual void calculate_error_gradient_lm(const DataSetBatch&,
209 LossIndexBackPropagationLM&) const;
210
211 void calculate_squared_errors_jacobian_lm(const DataSetBatch&,
212 NeuralNetworkForwardPropagation&,
213 LossIndexBackPropagationLM&) const;
214
215 virtual void calculate_error_hessian_lm(const DataSetBatch&,
216 LossIndexBackPropagationLM&) const {}
217
218 void back_propagate_lm(const DataSetBatch&,
219 NeuralNetworkForwardPropagation&,
220 LossIndexBackPropagationLM&) const;
221
222 // Regularization methods
223
224 type calculate_regularization(const Tensor<type, 1>&) const;
225
226 void calculate_regularization_gradient(const Tensor<type, 1>&, Tensor<type, 1>&) const;
227 void calculate_regularization_hessian(const Tensor<type, 1>&, Tensor<type, 2>&) const;
228
229 // Serialization methods
230
231 void from_XML(const tinyxml2::XMLDocument&);
232
233 virtual void write_XML(tinyxml2::XMLPrinter&) const;
234
235 void regularization_from_XML(const tinyxml2::XMLDocument&);
236 void write_regularization_XML(tinyxml2::XMLPrinter&) const;
237
238 virtual string get_error_type() const;
239 virtual string get_error_type_text() const;
240
241 string write_regularization_method() const;
242
243 // Checking methods
244
245 void check() const;
246
247protected:
248
249 NonBlockingThreadPool* non_blocking_thread_pool = nullptr;
250 ThreadPoolDevice* thread_pool_device = nullptr;
251
253
255
257
259
261
262 RegularizationMethod regularization_method = RegularizationMethod::L2;
263
265
266 type regularization_weight = static_cast<type>(0.01);
267
269
270 bool display = true;
271
272 const Eigen::array<IndexPair<Index>, 1> AT_B = {IndexPair<Index>(0, 0)};
273 const Eigen::array<IndexPair<Index>, 1> A_B = {IndexPair<Index>(1, 0)};
274
275 const Eigen::array<IndexPair<Index>, 2> SSE = {IndexPair<Index>(0, 0), IndexPair<Index>(1, 1)};
276
277 const Eigen::array<int, 1> rows_sum = {Eigen::array<int, 1>({1})};
278
279#ifdef OPENNN_CUDA
280 #include "../../opennn-cuda/opennn-cuda/loss_index_cuda.h"
281#endif
282
283};
284
285
288
290{
292
294
295 explicit LossIndexBackPropagation(const Index& new_batch_samples_number, LossIndex* new_loss_index_pointer)
296 {
297 if(new_batch_samples_number == 0) return;
298
299 set(new_batch_samples_number, new_loss_index_pointer);
300 }
301
303
304 void set(const Index& new_batch_samples_number, LossIndex* new_loss_index_pointer)
305 {
306 batch_samples_number = new_batch_samples_number;
307
308 loss_index_pointer = new_loss_index_pointer;
309
310 // Neural network
311
312 NeuralNetwork* neural_network_pointer = loss_index_pointer->get_neural_network_pointer();
313
314 const Index parameters_number = neural_network_pointer->get_parameters_number();
315
316 const Index outputs_number = neural_network_pointer->get_outputs_number();
317
318 // First order loss
319
320 neural_network.set(batch_samples_number, neural_network_pointer);
321
322 error = type(0);
323
324 loss = type(0);
325
326 errors.resize(batch_samples_number, outputs_number);
327
328 parameters = neural_network_pointer->get_parameters();
329
330 gradient.resize(parameters_number);
331
332 regularization_gradient.resize(parameters_number);
333 regularization_gradient.setConstant(type(0));
334 }
335
336 void print() const
337 {
338 cout << "Loss index back-propagation" << endl;
339
340 cout << "Errors:" << endl;
341 cout << errors << endl;
342
343 cout << "Error:" << endl;
344 cout << error << endl;
345
346 cout << "Loss:" << endl;
347 cout << loss << endl;
348
349 cout << "Gradient:" << endl;
350 cout << gradient << endl;
351
352 neural_network.print();
353 }
354
355 LossIndex* loss_index_pointer = nullptr;
356
357 Index batch_samples_number = 0;
358
359 NeuralNetworkBackPropagation neural_network;
360
361 type error = type(0);
362
363 type loss = type(0);
364
365 Tensor<type, 2> errors;
366
367 Tensor<type, 1> parameters;
368
369 Tensor<type, 1> gradient;
370
371 Tensor<type, 1> regularization_gradient;
372};
373
374
376
381
383{
385
387
388 explicit LossIndexBackPropagationLM(const Index& new_batch_samples_number, LossIndex* new_loss_index_pointer)
389 {
390 if(new_batch_samples_number == 0) return;
391
392 set(new_batch_samples_number, new_loss_index_pointer);
393 }
394
395 void set(const Index& new_batch_samples_number, LossIndex* new_loss_index_pointer)
396 {
397 batch_samples_number = new_batch_samples_number;
398
399 loss_index_pointer = new_loss_index_pointer;
400
401 NeuralNetwork* neural_network_pointer = loss_index_pointer->get_neural_network_pointer();
402
403 const Index parameters_number = neural_network_pointer->get_parameters_number();
404
405 const Index outputs_number = neural_network_pointer->get_outputs_number();
406
407 neural_network.set(batch_samples_number, neural_network_pointer);
408
409 parameters = neural_network_pointer->get_parameters();
410
411 error = type(0);
412
413 loss = type(0);
414
415 gradient.resize(parameters_number);
416
417 regularization_gradient.resize(parameters_number);
418 regularization_gradient.setZero();
419
420 squared_errors_jacobian.resize(batch_samples_number, parameters_number);
421
422 hessian.resize(parameters_number, parameters_number);
423
424 regularization_hessian.resize(parameters_number, parameters_number);
425 regularization_hessian.setZero();
426
427 errors.resize(batch_samples_number, outputs_number);
428
429 squared_errors.resize(batch_samples_number);
430 }
431
432 void print() const
433 {
434 cout << "Loss index back-propagation LM" << endl;
435
436 cout << "Errors:" << endl;
437 cout << errors << endl;
438
439 cout << "Squared errors:" << endl;
440 cout << squared_errors << endl;
441
442 cout << "Squared errors Jacobian:" << endl;
443 cout << squared_errors_jacobian << endl;
444
445 cout << "Error:" << endl;
446 cout << error << endl;
447
448 cout << "Loss:" << endl;
449 cout << loss << endl;
450
451 cout << "Gradient:" << endl;
452 cout << gradient << endl;
453
454 cout << "Hessian:" << endl;
455 cout << hessian << endl;
456 }
457
458 LossIndex* loss_index_pointer = nullptr;
459
460 Index batch_samples_number = 0;
461
462 type error = type(0);
463 type loss = type(0);
464
465 Tensor<type, 1> parameters;
466
467 NeuralNetworkBackPropagationLM neural_network;
468
469 Tensor<type, 2> errors;
470 Tensor<type, 1> squared_errors;
471 Tensor<type, 2> squared_errors_jacobian;
472
473 Tensor<type, 1> gradient;
474 Tensor<type, 2> hessian;
475
476 Tensor<type, 1> regularization_gradient;
477 Tensor<type, 2> regularization_hessian;
478};
479
480
481}
482
483#endif
484
485// OpenNN: Open Neural Networks Library.
486// Copyright(C) 2005-2021 Artificial Intelligence Techniques, SL.
487//
488// This library is free software; you can redistribute it and/or
489// modify it under the terms of the GNU Lesser General Public
490// License as published by the Free Software Foundation; either
491// version 2.1 of the License, or any later version.
492//
493// This library is distributed in the hope that it will be useful,
494// but WITHOUT ANY WARRANTY; without even the implied warranty of
495// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
496// Lesser General Public License for more details.
497
498// You should have received a copy of the GNU Lesser General Public
499// License along with this library; if not, write to the Free Software
500// 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
This abstract class represents the concept of loss index composed of an error term and a regularizati...
Definition: loss_index.h:48
type calculate_h(const type &) const
void calculate_regularization_hessian(const Tensor< type, 1 > &, Tensor< type, 2 > &) const
Definition: loss_index.cpp:688
DataSet * data_set_pointer
Pointer to a data set object.
Definition: loss_index.h:258
void set_regularization_method(const RegularizationMethod &)
Definition: loss_index.cpp:251
NeuralNetwork * neural_network_pointer
Pointer to a neural network object.
Definition: loss_index.h:254
virtual void set_data_set_pointer(DataSet *)
Sets a new data set on which the error term is to be measured.
Definition: loss_index.cpp:196
const bool & get_display() const
Definition: loss_index.cpp:61
bool has_selection() const
Returns true if there are selection samples and false otherwise.
Definition: loss_index.cpp:279
void from_XML(const tinyxml2::XMLDocument &)
Definition: loss_index.cpp:979
void set_default()
Sets the members of the error term to their default values:
Definition: loss_index.cpp:204
void check() const
Definition: loss_index.cpp:295
void calculate_squared_errors_jacobian_lm(const DataSetBatch &, NeuralNetworkForwardPropagation &, LossIndexBackPropagationLM &) const
Definition: loss_index.cpp:508
bool display
Display messages to screen.
Definition: loss_index.h:270
bool has_data_set() const
Definition: loss_index.cpp:86
void back_propagate_lm(const DataSetBatch &, NeuralNetworkForwardPropagation &, LossIndexBackPropagationLM &) const
Definition: loss_index.cpp:457
type regularization_weight
Regularization weight value.
Definition: loss_index.h:266
bool has_neural_network() const
Definition: loss_index.cpp:70
virtual string get_error_type() const
Returns a string with the default type of error term, "USER_PERFORMANCE_TERM".
Definition: loss_index.cpp:608
NeuralNetwork * get_neural_network_pointer() const
Returns a pointer to the neural network object associated to the error term.
Definition: loss_index.h:70
RegularizationMethod get_regularization_method() const
Returns the regularization method.
Definition: loss_index.cpp:101
virtual ~LossIndex()
Destructor.
Definition: loss_index.cpp:43
type calculate_regularization(const Tensor< type, 1 > &) const
Definition: loss_index.cpp:648
const type & get_regularization_weight() const
Returns regularization weight.
Definition: loss_index.cpp:52
void set_neural_network_pointer(NeuralNetwork *)
Definition: loss_index.cpp:188
string write_regularization_method() const
Definition: loss_index.cpp:625
RegularizationMethod regularization_method
Pointer to a regularization method object.
Definition: loss_index.h:262
void set_display(const bool &)
Definition: loss_index.cpp:271
RegularizationMethod
Enumeration of available regularization methods.
Definition: loss_index.h:66
virtual void write_XML(tinyxml2::XMLPrinter &) const
Definition: loss_index.cpp:881
void calculate_regularization_gradient(const Tensor< type, 1 > &, Tensor< type, 1 > &) const
Definition: loss_index.cpp:669
virtual string get_error_type_text() const
Returns a string with the default type of error term in text format, "USER_PERFORMANCE_TERM".
Definition: loss_index.cpp:616
DataSet * get_data_set_pointer() const
Returns a pointer to the data set object associated to the error term.
Definition: loss_index.h:92
void set_regularization_weight(const type &)
Definition: loss_index.cpp:260
Index get_parameters_number() const
virtual ~LossIndexBackPropagation()
Destructor.
LossIndexBackPropagation()
Default constructor.
Definition: loss_index.h:293
A loss index composed of several terms, this structure represent the First Order for this function.
Definition: loss_index.h:383
LossIndexBackPropagationLM()
Default constructor.
Definition: loss_index.h:386