gradient_descent.cpp
1// OpenNN: Open Neural Networks Library
2// www.opennn.net
3//
4// G R A D I E N T D E S C E N T C L A S S
5//
6// Artificial Intelligence Techniques SL
7// artelnics@artelnics.com
8
9#include "gradient_descent.h"
10
11namespace OpenNN
12{
13
17
20{
22}
23
24
29
31 : OptimizationAlgorithm(new_loss_index_pointer)
32{
33 learning_rate_algorithm.set_loss_index_pointer(new_loss_index_pointer);
34
36}
37
38
40
42{
43}
44
45
47
49{
51}
52
53
55
57{
59}
60
61
63
65{
66 return hardware_use;
67}
68
69
71
73{
75}
76
77
80
82{
83 return training_loss_goal;
84}
85
86
88
90{
92}
93
94
96
98{
100}
101
102
104
106{
107 return maximum_time;
108}
109
110
114
116{
117 loss_index_pointer = new_loss_index_pointer;
118
119 learning_rate_algorithm.set_loss_index_pointer(new_loss_index_pointer);
120}
121
122
124{
125 // Stopping criteria
126
127 minimum_loss_decrease = type(0);
128
129 training_loss_goal = type(0);
130 maximum_selection_failures = numeric_limits<Index>::max();
131
133 maximum_time = type(3600);
134
135 // UTILITIES
136
137 display_period = 10;
138}
139
140
143
144void GradientDescent::set_maximum_epochs_number(const Index& new_maximum_epochs_number)
145{
146#ifdef OPENNN_DEBUG
147
148 if(new_maximum_epochs_number < static_cast<type>(0.0))
149 {
150 ostringstream buffer;
151
152 buffer << "OpenNN Exception: GradientDescent class.\n"
153 << "void set_maximum_epochs_number(const type&) method.\n"
154 << "Maximum epochs number must be equal or greater than 0.\n";
155
156 throw logic_error(buffer.str());
157 }
158
159#endif
160
161 maximum_epochs_number = new_maximum_epochs_number;
162}
163
164
167
168void GradientDescent::set_minimum_loss_decrease(const type& new_minimum_loss_decrease)
169{
170 minimum_loss_decrease = new_minimum_loss_decrease;
171}
172
173
177
178void GradientDescent::set_loss_goal(const type& new_loss_goal)
179{
180 training_loss_goal = new_loss_goal;
181}
182
183
187
188void GradientDescent::set_maximum_selection_failures(const Index& new_maximum_selection_failures)
189{
190 maximum_selection_failures = new_maximum_selection_failures;
191}
192
193
196
197void GradientDescent::set_maximum_time(const type& new_maximum_time)
198{
199#ifdef OPENNN_DEBUG
200
201 if(new_maximum_time < static_cast<type>(0.0))
202 {
203 ostringstream buffer;
204
205 buffer << "OpenNN Exception: GradientDescent class.\n"
206 << "void set_maximum_time(const type&) method.\n"
207 << "Maximum time must be equal or greater than 0.\n";
208
209 throw logic_error(buffer.str());
210 }
211
212#endif
213
214 // Set maximum time
215
216 maximum_time = new_maximum_time;
217}
218
219
223
224void GradientDescent::calculate_training_direction(const Tensor<type, 1>& gradient, Tensor<type, 1>& training_direction) const
225{
226#ifdef OPENNN_DEBUG
227
228 ostringstream buffer;
229
231 {
232 buffer << "OpenNN Exception: GradientDescent class.\n"
233 << "Tensor<type, 1> calculate_training_direction(const Tensor<type, 1>&) const method.\n"
234 << "Loss index pointer is nullptr.\n";
235
236 throw logic_error(buffer.str());
237 }
238
239 const NeuralNetwork* neural_network_pointer = loss_index_pointer->get_neural_network_pointer();
240
241 const Index parameters_number = neural_network_pointer->get_parameters_number();
242
243 const Index gradient_size = gradient.size();
244
245 if(gradient_size != parameters_number)
246 {
247 buffer << "OpenNN Exception: GradientDescent class.\n"
248 << "Tensor<type, 1> calculate_training_direction(const Tensor<type, 1>&) const method.\n"
249 << "Size of gradient(" << gradient_size
250 << ") is not equal to number of parameters(" << parameters_number << ").\n";
251
252 throw logic_error(buffer.str());
253 }
254
255#endif
256
257 training_direction.device(*thread_pool_device) = -gradient;
258}
259
260
266
268 const DataSetBatch& batch,
269 NeuralNetworkForwardPropagation& forward_propagation,
270 LossIndexBackPropagation& back_propagation,
271 GradientDescentData& optimization_data)
272{
273 calculate_training_direction(back_propagation.gradient, optimization_data.training_direction);
274
275 if(is_zero(optimization_data.training_direction)) return;
276 //throw logic_error("Training direction is zero");
277
278 // Get initial learning_rate
279
280 optimization_data.epoch == 0
281 ? optimization_data.initial_learning_rate = first_learning_rate
282 : optimization_data.initial_learning_rate = optimization_data.old_learning_rate;
283
284 const pair<type,type> directional_point = learning_rate_algorithm.calculate_directional_point(
285 batch,
286 forward_propagation,
287 back_propagation,
288 optimization_data);
289
290 optimization_data.learning_rate = directional_point.first;
291 back_propagation.loss = directional_point.second;
292
293 if(abs(optimization_data.learning_rate) > type(0))
294 {
295 optimization_data.parameters_increment.device(*thread_pool_device)
296 = optimization_data.training_direction*optimization_data.learning_rate;
297
298 back_propagation.parameters.device(*thread_pool_device) += optimization_data.parameters_increment;
299 }
300 else
301 {
302 const Index parameters_number = back_propagation.parameters.size();
303
304 for(Index i = 0; i < parameters_number; i++)
305 {
306 if(abs(back_propagation.gradient(i)) < type(NUMERIC_LIMITS_MIN))
307 {
308 optimization_data.parameters_increment(i) = type(0);
309 }
310 else if(back_propagation.gradient(i) > type(0))
311 {
312 back_propagation.parameters(i) -= numeric_limits<type>::epsilon();
313
314 optimization_data.parameters_increment(i) = -numeric_limits<type>::epsilon();
315 }
316 else if(back_propagation.gradient(i) < type(0))
317 {
318 back_propagation.parameters(i) += numeric_limits<type>::epsilon();
319
320 optimization_data.parameters_increment(i) = numeric_limits<type>::epsilon();
321 }
322 }
323
324 optimization_data.learning_rate = optimization_data.old_learning_rate;
325 }
326
327 // Update parameters
328
329 optimization_data.old_learning_rate = optimization_data.learning_rate;
330
331 forward_propagation.neural_network_pointer->set_parameters(back_propagation.parameters);
332
333}
334
335
340
342{
344
345#ifdef OPENNN_DEBUG
346 check();
347#endif
348
349 // Start training
350
351 if(display) cout << "Training with gradient descent...\n";
352
353 // Data set
354
355 DataSet* data_set_pointer = loss_index_pointer->get_data_set_pointer();
356
357 const Index training_samples_number = data_set_pointer->get_training_samples_number();
358 const Index selection_samples_number = data_set_pointer->get_selection_samples_number();
359
360 const bool has_selection = data_set_pointer->has_selection();
361
362 const Tensor<Index, 1> training_samples_indices = data_set_pointer->get_training_samples_indices();
363 const Tensor<Index, 1> selection_samples_indices = data_set_pointer->get_selection_samples_indices();
364
365 const Tensor<Index, 1> input_variables_indices = data_set_pointer->get_input_variables_indices();
366 const Tensor<Index, 1> target_variables_indices = data_set_pointer->get_target_variables_indices();
367
368 const Tensor<string, 1> inputs_names = data_set_pointer->get_input_variables_names();
369 const Tensor<string, 1> targets_names = data_set_pointer->get_target_variables_names();
370
371 const Tensor<Scaler, 1> input_variables_scalers = data_set_pointer->get_input_variables_scalers();
372 const Tensor<Scaler, 1> target_variables_scalers = data_set_pointer->get_target_variables_scalers();
373
374 const Tensor<Descriptives, 1> input_variables_descriptives = data_set_pointer->scale_input_variables();
375 Tensor<Descriptives, 1> target_variables_descriptives;
376
377 // Neural network
378
380
381 neural_network_pointer->set_inputs_names(inputs_names);
382 neural_network_pointer->set_outputs_names(targets_names);
383
384 if(neural_network_pointer->has_scaling_layer())
385 {
386 ScalingLayer* scaling_layer_pointer = neural_network_pointer->get_scaling_layer_pointer();
387 scaling_layer_pointer->set(input_variables_descriptives, input_variables_scalers);
388 }
389
390 if(neural_network_pointer->has_unscaling_layer())
391 {
392 target_variables_descriptives = data_set_pointer->scale_target_variables();
393
394 UnscalingLayer* unscaling_layer_pointer = neural_network_pointer->get_unscaling_layer_pointer();
395 unscaling_layer_pointer->set(target_variables_descriptives, target_variables_scalers);
396 }
397
398 NeuralNetworkForwardPropagation training_forward_propagation(training_samples_number, neural_network_pointer);
399 NeuralNetworkForwardPropagation selection_forward_propagation(selection_samples_number, neural_network_pointer);
400
401 DataSetBatch training_batch(training_samples_number, data_set_pointer);
402 training_batch.fill(training_samples_indices, input_variables_indices, target_variables_indices);
403
404 DataSetBatch selection_batch(selection_samples_number, data_set_pointer);
405 selection_batch.fill(selection_samples_indices, input_variables_indices, target_variables_indices);
406
407 // Loss index
408
409 const string error_type = loss_index_pointer->get_error_type();
410
411 loss_index_pointer->set_normalization_coefficient();
412
413 LossIndexBackPropagation training_back_propagation(training_samples_number, loss_index_pointer);
414 LossIndexBackPropagation selection_back_propagation(selection_samples_number, loss_index_pointer);
415
416 // Optimization algorithm
417
418 GradientDescentData optimization_data(this);
419
420 Index selection_failures = 0;
421
422 bool stop_training = false;
423
424 type old_loss = type(0);
425 type loss_decrease = numeric_limits<type>::max();
426
427 // Main loop
428
429 time_t beginning_time, current_time;
430 time(&beginning_time);
431 type elapsed_time = type(0);
432
433 for(Index epoch = 0; epoch <= maximum_epochs_number; epoch++)
434 {
435 if(display && epoch%display_period == 0) cout << "Epoch: " << epoch << endl;
436
437 optimization_data.epoch = epoch;
438
439 // Neural network
440
441 neural_network_pointer->forward_propagate(training_batch, training_forward_propagation);
442
443 // Loss index
444
445 loss_index_pointer->back_propagate(training_batch, training_forward_propagation, training_back_propagation);
446 results.training_error_history(epoch) = training_back_propagation.error;
447
448 if(has_selection)
449 {
450 neural_network_pointer->forward_propagate(selection_batch, selection_forward_propagation);
451
452 loss_index_pointer->calculate_errors(selection_batch, selection_forward_propagation, selection_back_propagation);
453 loss_index_pointer->calculate_error(selection_batch, selection_forward_propagation, selection_back_propagation);
454
455 results.selection_error_history(epoch) = selection_back_propagation.error;
456
457 if(epoch != 0 && results.selection_error_history(epoch) > results.selection_error_history(epoch-1)) selection_failures++;
458 }
459
460 // Optimization algorithm
461
462 time(&current_time);
463 elapsed_time = static_cast<type>(difftime(current_time, beginning_time));
464
465 // Print progress
466
467 if(display && epoch%display_period == 0)
468 {
469 cout << "Training error: " << training_back_propagation.error << endl;
470 if(has_selection) cout << "Selection error: " << selection_back_propagation.error << endl;
471 cout << "Learning rate: " << optimization_data.learning_rate << endl;
472 cout << "Elapsed time: " << write_time(elapsed_time) << endl;
473 }
474
475 // Stopping Criteria
476
477 if(training_back_propagation.loss <= training_loss_goal)
478 {
479 if(display)
480 cout << "Epoch " << epoch << endl << "Loss goal reached: " << training_back_propagation.loss << endl;
481
482 stop_training = true;
483
484 results.stopping_condition = StoppingCondition::LossGoal;
485 }
486
487 else if(selection_failures >= maximum_selection_failures)
488 {
489 if(display) cout << "Epoch " << epoch << endl << "Maximum selection failures reached: " << selection_failures << endl;
490
491 stop_training = true;
492
493 results.stopping_condition = StoppingCondition::MaximumSelectionErrorIncreases;
494 }
495
496 else if(epoch == maximum_epochs_number)
497 {
498 if(display) cout << "Epoch " << epoch << endl << "Maximum number of epochs reached: " << epoch << endl;
499
500 stop_training = true;
501
502 results.stopping_condition = StoppingCondition::MaximumEpochsNumber;
503 }
504
505 else if(elapsed_time >= maximum_time)
506 {
507 if(display) cout << "Epoch " << epoch << endl << "Maximum training time reached: " << elapsed_time;
508
509 stop_training = true;
510
511 results.stopping_condition = StoppingCondition::MaximumTime;
512 }
513
514 if(epoch != 0) loss_decrease = old_loss - training_back_propagation.loss;
515
516 if(loss_decrease < minimum_loss_decrease)
517 {
518 if(display) cout << "Epoch " << epoch << endl << "Minimum loss decrease reached: " << loss_decrease << endl;
519
520 stop_training = true;
521
522 results.stopping_condition = StoppingCondition::MinimumLossDecrease;
523 }
524
525 old_loss = training_back_propagation.loss;
526
527 if(stop_training)
528 {
529 results.resize_training_error_history(epoch+1);
530
531 if(has_selection) results.resize_selection_error_history(epoch+1);
532 else results.resize_selection_error_history(0);
533
534 results.elapsed_time = write_time(elapsed_time);
535
536 break;
537 }
538
539 if(epoch != 0 && epoch%save_period == 0) neural_network_pointer->save(neural_network_file_name);
540
541 update_parameters(training_batch, training_forward_propagation, training_back_propagation, optimization_data);
542 }
543
544 data_set_pointer->unscale_input_variables(input_variables_descriptives);
545
546 if(neural_network_pointer->has_unscaling_layer())
547 data_set_pointer->unscale_target_variables(target_variables_descriptives);
548
549 if(display) results.print();
550
551 return results;
552}
553
554
555string GradientDescent::write_optimization_algorithm_type() const
556{
557 return "GRADIENT_DESCENT";
558}
559
560
562
563Tensor<string, 2> GradientDescent::to_string_matrix() const
564{
565 Tensor<string, 2> labels_values(7, 2);
566
567 // Learning rate method
568
569 labels_values(0,0) = "Learning rate method";
571
572 // Loss tolerance
573
574 labels_values(1,0) = "Learning rate tolerance";
575 labels_values(1,1) = to_string(double(learning_rate_algorithm.get_learning_rate_tolerance()));
576
577 // Minimum loss decrease
578
579 labels_values(2,0) = "Minimum loss decrease";
580 labels_values(2,1) = to_string(double(minimum_loss_decrease));
581
582 // Loss goal
583
584 labels_values(3,0) = "Loss goal";
585 labels_values(3,1) = to_string(double(training_loss_goal));
586
587 // Maximum selection error increases
588
589 labels_values(4,0) = "Maximum selection error increases";
590 labels_values(4,1) = to_string(maximum_selection_failures);
591
592 // Maximum epochs number
593
594 labels_values(5,0) = "Maximum epochs number";
595 labels_values(5,1) = to_string(maximum_epochs_number);
596
597 // Maximum time
598
599 labels_values(6,0) = "Maximum time";
600 labels_values(6,1) = write_time(maximum_time);
601
602 return labels_values;
603}
604
605
609
611{
612 ostringstream buffer;
613
614 // Learning rate algorithm
615
616 file_stream.OpenElement("GradientDescent");
617
619
620 // Minimum loss decrease
621
622 file_stream.OpenElement("MinimumLossDecrease");
623
624 buffer.str("");
625 buffer << minimum_loss_decrease;
626
627 file_stream.PushText(buffer.str().c_str());
628
629 file_stream.CloseElement();
630
631 // Loss goal
632
633 file_stream.OpenElement("LossGoal");
634
635 buffer.str("");
636 buffer << training_loss_goal;
637
638 file_stream.PushText(buffer.str().c_str());
639
640 file_stream.CloseElement();
641
642 // Maximum selection error increases
643
644 file_stream.OpenElement("MaximumSelectionErrorIncreases");
645
646 buffer.str("");
648
649 file_stream.PushText(buffer.str().c_str());
650
651 file_stream.CloseElement();
652
653 // Maximum epochs number
654
655 file_stream.OpenElement("MaximumEpochsNumber");
656
657 buffer.str("");
658 buffer << maximum_epochs_number;
659
660 file_stream.PushText(buffer.str().c_str());
661
662 file_stream.CloseElement();
663
664 // Maximum time
665
666 file_stream.OpenElement("MaximumTime");
667
668 buffer.str("");
669 buffer << maximum_time;
670
671 file_stream.PushText(buffer.str().c_str());
672
673 file_stream.CloseElement();
674
675 // Hardware use
676
677 file_stream.OpenElement("HardwareUse");
678
679 buffer.str("");
680 buffer << hardware_use;
681
682 file_stream.PushText(buffer.str().c_str());
683
684 file_stream.CloseElement();
685
686 file_stream.CloseElement();
687}
688
689
691{
692 const tinyxml2::XMLElement* root_element = document.FirstChildElement("GradientDescent");
693
694 if(!root_element)
695 {
696 ostringstream buffer;
697
698 buffer << "OpenNN Exception: GradientDescent class.\n"
699 << "void from_XML(const tinyxml2::XMLDocument&) method.\n"
700 << "Gradient descent element is nullptr.\n";
701
702 throw logic_error(buffer.str());
703 }
704
705 // Learning rate algorithm
706 {
707 const tinyxml2::XMLElement* learning_rate_algorithm_element
708 = root_element->FirstChildElement("LearningRateAlgorithm");
709
710 if(learning_rate_algorithm_element)
711 {
712 tinyxml2::XMLDocument learning_rate_algorithm_document;
713 tinyxml2::XMLNode* element_clone;
714
715 element_clone = learning_rate_algorithm_element->DeepClone(&learning_rate_algorithm_document);
716
717 learning_rate_algorithm_document.InsertFirstChild(element_clone);
718
719 learning_rate_algorithm.from_XML(learning_rate_algorithm_document);
720 }
721 }
722
723 // Minimum loss decrease
724 {
725 const tinyxml2::XMLElement* element = root_element->FirstChildElement("MinimumLossDecrease");
726
727 if(element)
728 {
729 cout << "MinimumLossDecrease" << endl;
730 const type new_minimum_loss_decrease = static_cast<type>(atof(element->GetText()));
731
732 try
733 {
734 set_minimum_loss_decrease(new_minimum_loss_decrease);
735 }
736 catch(const logic_error& e)
737 {
738 cerr << e.what() << endl;
739 }
740 }
741 }
742
743 // Loss goal
744 {
745 const tinyxml2::XMLElement* element = root_element->FirstChildElement("LossGoal");
746
747 if(element)
748 {
749 const type new_loss_goal = static_cast<type>(atof(element->GetText()));
750
751 try
752 {
753 set_loss_goal(new_loss_goal);
754 }
755 catch(const logic_error& e)
756 {
757 cerr << e.what() << endl;
758 }
759 }
760 }
761
762 // Maximum selection error increases
763 {
764 const tinyxml2::XMLElement* element = root_element->FirstChildElement("MaximumSelectionErrorIncreases");
765
766 if(element)
767 {
768 const Index new_maximum_selection_failures = static_cast<Index>(atoi(element->GetText()));
769
770 try
771 {
772 set_maximum_selection_failures(new_maximum_selection_failures);
773 }
774 catch(const logic_error& e)
775 {
776 cerr << e.what() << endl;
777 }
778 }
779 }
780
781 // Maximum epochs number
782 {
783 const tinyxml2::XMLElement* element = root_element->FirstChildElement("MaximumEpochsNumber");
784
785 if(element)
786 {
787 const Index new_maximum_epochs_number = static_cast<Index>(atoi(element->GetText()));
788
789 try
790 {
791 set_maximum_epochs_number(new_maximum_epochs_number);
792 }
793 catch(const logic_error& e)
794 {
795 cerr << e.what() << endl;
796 }
797 }
798 }
799
800 // Maximum time
801 {
802 const tinyxml2::XMLElement* element = root_element->FirstChildElement("MaximumTime");
803
804 if(element)
805 {
806 const type new_maximum_time = static_cast<type>(atof(element->GetText()));
807
808 try
809 {
810 set_maximum_time(new_maximum_time);
811 }
812 catch(const logic_error& e)
813 {
814 cerr << e.what() << endl;
815 }
816 }
817 }
818
819 // Hardware use
820 {
821 const tinyxml2::XMLElement* element = root_element->FirstChildElement("HardwareUse");
822
823 if(element)
824 {
825 const string new_hardware_use = element->GetText();
826 try
827 {
828 set_hardware_use(new_hardware_use);
829 }
830 catch(const logic_error& e)
831 {
832 cerr << e.what() << endl;
833 }
834 }
835 }
836}
837
838}
839
840// OpenNN: Open Neural Networks Library.
841// Copyright(C) 2005-2021 Artificial Intelligence Techniques, SL.
842//
843// This library is free software; you can redistribute it and/or
844// modify it under the terms of the GNU Lesser General Public
845// License as published by the Free Software Foundation; either
846// version 2.1 of the License, or any later version.
847//
848// This library is distributed in the hope that it will be useful,
849// but WITHOUT ANY WARRANTY; without even the implied warranty of
850// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
851// Lesser General Public License for more details.
852
853// You should have received a copy of the GNU Lesser General Public
854// License along with this library; if not, write to the Free Software
855// 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_training_samples_number() const
Returns the number of samples in the data set which will be used for training.
Definition: data_set.cpp:1382
Tensor< Descriptives, 1 > scale_target_variables()
Definition: data_set.cpp:6298
Tensor< Index, 1 > get_training_samples_indices() const
Returns the indices of the samples which will be used for training.
Definition: data_set.cpp:1073
Tensor< Index, 1 > get_selection_samples_indices() const
Returns the indices of the samples which will be used for selection.
Definition: data_set.cpp:1098
void unscale_input_variables(const Tensor< Descriptives, 1 > &)
Definition: data_set.cpp:6351
Tensor< Index, 1 > get_target_variables_indices() const
Returns the indices of the target variables.
Definition: data_set.cpp:3094
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
void unscale_target_variables(const Tensor< Descriptives, 1 > &)
Definition: data_set.cpp:6397
Tensor< string, 1 > get_target_variables_names() const
Definition: data_set.cpp:2215
Tensor< Index, 1 > get_input_variables_indices() const
Returns the indices of the input variables.
Definition: data_set.cpp:3047
Tensor< string, 1 > get_input_variables_names() const
Definition: data_set.cpp:2184
Tensor< Descriptives, 1 > scale_input_variables()
Definition: data_set.cpp:6243
TrainingResults perform_training()
void set_maximum_selection_failures(const Index &)
void set_loss_index_pointer(LossIndex *)
const type & get_maximum_time() const
Returns the maximum training time.
string get_hardware_use() const
Returns the hardware used. Default: Multi-core.
const type & get_loss_goal() const
void from_XML(const tinyxml2::XMLDocument &)
void set_default()
Sets the members of the optimization algorithm object to their default values.
const Index & get_maximum_epochs_number() const
Returns the maximum number of iterations for training.
Tensor< string, 2 > to_string_matrix() const
Writes as matrix of strings the most representative atributes.
type minimum_loss_decrease
Minimum loss improvement between two successive iterations. It is used as a stopping criterion.
LearningRateAlgorithm * get_learning_rate_algorithm_pointer()
Returns a pointer to the learning rate algorithm object inside the gradient descent object.
const LearningRateAlgorithm & get_learning_rate_algorithm() const
Returns a constant reference to the learning rate algorithm object inside the gradient descent object...
void set_maximum_time(const type &)
LearningRateAlgorithm learning_rate_algorithm
Learning rate algorithm object for one-dimensional minimization.
void set_loss_goal(const type &)
type maximum_time
Maximum training time. It is used as a stopping criterion.
void set_maximum_epochs_number(const Index &)
void calculate_training_direction(const Tensor< type, 1 > &, Tensor< type, 1 > &) const
void set_minimum_loss_decrease(const type &)
type training_loss_goal
Goal value for the loss. It is used as a stopping criterion.
Index maximum_epochs_number
Maximum epochs number.
virtual ~GradientDescent()
Destructor.
void write_XML(tinyxml2::XMLPrinter &) const
void update_parameters(const DataSetBatch &batch, NeuralNetworkForwardPropagation &forward_propagation, LossIndexBackPropagation &back_propagation, GradientDescentData &optimization_data)
GradientDescent::update_parameters.
const Index & get_maximum_selection_failures() const
Returns the maximum number of selection error increases during the training process.
const type & get_minimum_loss_decrease() const
Returns the minimum loss improvement during training.
A learning rate that is adjusted according to an algorithm during training to minimize training time.
void from_XML(const tinyxml2::XMLDocument &)
string write_learning_rate_method() const
Returns a string with the name of the learning rate method to be used.
pair< type, type > calculate_directional_point(const DataSetBatch &, NeuralNetworkForwardPropagation &, LossIndexBackPropagation &, OptimizationAlgorithmData &) const
void write_XML(tinyxml2::XMLPrinter &) const
This abstract class represents the concept of loss index composed of an error term and a regularizati...
Definition: loss_index.h:48
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
DataSet * get_data_set_pointer() const
Returns a pointer to the data set object associated to the error term.
Definition: loss_index.h:92
ScalingLayer * get_scaling_layer_pointer() const
Returns a pointer to the scaling layers object composing this neural network object.
bool has_scaling_layer() const
bool has_unscaling_layer() const
void forward_propagate(const DataSetBatch &, NeuralNetworkForwardPropagation &) const
Calculate forward propagation in neural network.
void save(const string &) const
void set_parameters(Tensor< type, 1 > &)
UnscalingLayer * get_unscaling_layer_pointer() const
Returns a pointer to the unscaling layers object composing this neural network object.
void set_inputs_names(const Tensor< string, 1 > &)
Index get_parameters_number() const
void set_outputs_names(const Tensor< string, 1 > &)
string neural_network_file_name
Path where the neural network is saved.
void set_hardware_use(const string &)
Set hardware to use. Default: Multi-core.
LossIndex * loss_index_pointer
Pointer to a loss index for a neural network object.
bool display
Display messages to screen.
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.
Index display_period
Number of iterations between the training showing progress.
This class represents a layer of scaling neurons.
Definition: scaling_layer.h:38
void set()
Sets the scaling layer to be empty.
This class represents a layer of unscaling neurons.
void set()
Sets the unscaling layer to be empty.
void PushText(const char *text, bool cdata=false)
Add a text node.
Definition: tinyxml2.cpp:2878
virtual void CloseElement(bool compactMode=false)
If streaming, close the Element.
Definition: tinyxml2.cpp:2834
HALF_CONSTEXPR half abs(half arg)
Definition: half.hpp:2735
This structure contains the optimization algorithm results.
Tensor< type, 1 > selection_error_history
History of the selection error over the training iterations.
void resize_training_error_history(const Index &)
Resizes the training error history keeping the values.
OptimizationAlgorithm::StoppingCondition stopping_condition
Stopping condition of the algorithm.
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.