conjugate_gradient.h
1// OpenNN: Open Neural Networks Library
2// www.opennn.net
3//
4// C O N J U G A T E G R A D I E N T C L A S S H E A D E R
5//
6// Artificial Intelligence Techniques SL
7// artelnics@artelnics.com
8
9#ifndef CONJUGATEGRADIENT_H
10#define CONJUGATEGRADIENT_H
11
12// System inlcludes
13
14#include <string>
15#include <sstream>
16#include <iostream>
17#include <fstream>
18#include <algorithm>
19#include <functional>
20#include <climits>
21#include <cmath>
22#include <ctime>
23
24// OpenNN includes
25
26#include "config.h"
27#include "loss_index.h"
28#include "optimization_algorithm.h"
29#include "learning_rate_algorithm.h"
30
31namespace OpenNN
32{
33
34struct ConjugateGradientData;
35
38
44
46{
47
48public:
49
50 // Enumerations
51
53
54 enum class TrainingDirectionMethod{PR, FR};
55
56 // DEFAULT CONSTRUCTOR
57
58 explicit ConjugateGradient();
59
60 explicit ConjugateGradient(LossIndex*);
61
62 virtual ~ConjugateGradient();
63
64 // Get methods
65
68
69 // Training operators
70
73
74 // Stopping criteria
75
76
77
78 const type& get_minimum_loss_decrease() const;
79 const type& get_loss_goal() const;
80 const Index& get_maximum_selection_failures() const;
81
82
83 const Index& get_maximum_epochs_number() const;
84 const type& get_maximum_time() const;
85
86 // Set methods
87
88 void set_default();
89
91
92 // Training operators
93
95 void set_training_direction_method(const string&);
96
97 // Stopping criteria
98
99
100
101 void set_loss_goal(const type&);
102 void set_minimum_loss_decrease(const type&);
103 void set_maximum_selection_failures(const Index&);
104
105
106 void set_maximum_epochs_number(const Index&);
107 void set_maximum_time(const type&);
108
109 // Utilities
110
111 void set_save_period(const Index&);
112
113 // Training direction methods
114
115 type calculate_PR_parameter(const Tensor<type, 1>&, const Tensor<type, 1>&) const;
116 type calculate_FR_parameter(const Tensor<type, 1>&, const Tensor<type, 1>&) const;
117
118 void calculate_PR_training_direction(const Tensor<type, 1>&, const Tensor<type, 1>&, const Tensor<type, 1>&, Tensor<type, 1>&) const;
119 void calculate_FR_training_direction(const Tensor<type, 1>&, const Tensor<type, 1>&, const Tensor<type, 1>&, Tensor<type, 1>&) const;
120
121 void calculate_gradient_descent_training_direction(const Tensor<type, 1>&, Tensor<type, 1>&) const;
122
123 void calculate_conjugate_gradient_training_direction(const Tensor<type, 1>&,
124 const Tensor<type, 1>&,
125 const Tensor<type, 1>&,
126 Tensor<type, 1>&) const;
127
128 // Training methods
129
131
133
134 // Serialization methods
135
136 Tensor<string, 2> to_string_matrix() const;
137
138 void from_XML(const tinyxml2::XMLDocument&);
139
140 void write_XML(tinyxml2::XMLPrinter&) const;
141
143 const DataSetBatch& batch,
144 NeuralNetworkForwardPropagation& forward_propagation,
145 LossIndexBackPropagation& back_propagation,
146 ConjugateGradientData& optimization_data);
147
148private:
149
150 type first_learning_rate = static_cast<type>(0.01);
151
153
154 TrainingDirectionMethod training_direction_method = ConjugateGradient::TrainingDirectionMethod::FR;
155
157
159
160 // Stopping criteria
161
163
164 type minimum_loss_decrease = type(0);
165
167
168 type training_loss_goal = type(0);
169
172
174
176
178
180
182};
183
184
186{
188
189 explicit ConjugateGradientData();
190
192
193 virtual ~ConjugateGradientData();
194
195 void set(ConjugateGradient*);
196
197 void print() const;
198
199 ConjugateGradient* conjugate_gradient_pointer = nullptr;
200
201 Tensor<type, 1> parameters_increment;
202
203 Tensor<type, 1> old_gradient;
204
205 Tensor<type, 1> old_training_direction;
206
207 Index epoch = 0;
208
209 type learning_rate = type(0);
210 type old_learning_rate = type(0);
211
212 Tensor<type, 0> training_slope;
213};
214
215}
216
217#endif
218
219
220// OpenNN: Open Neural Networks Library.
221// Copyright(C) 2005-2021 Artificial Intelligence Techniques, SL.
222//
223// This library is free software; you can redistribute it and/or
224// modify it under the terms of the GNU Lesser General Public
225// License as published by the Free Software Foundation; either
226// version 2.1 of the License, or any later version.
227//
228// This library is distributed in the hope that it will be useful,
229// but WITHOUT ANY WARRANTY; without even the implied warranty of
230// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
231// Lesser General Public License for more details.
232
233// You should have received a copy of the GNU Lesser General Public
234// License along with this library; if not, write to the Free Software
235// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
TrainingResults perform_training()
void set_maximum_selection_failures(const Index &)
void calculate_FR_training_direction(const Tensor< type, 1 > &, const Tensor< type, 1 > &, const Tensor< type, 1 > &, Tensor< type, 1 > &) const
void set_loss_index_pointer(LossIndex *)
const type & get_maximum_time() const
Returns the maximum training time.
const type & get_loss_goal() const
void from_XML(const tinyxml2::XMLDocument &)
const Index & get_maximum_epochs_number() const
Returns the maximum number of epochs 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 conjugate gradient method object.
void calculate_conjugate_gradient_training_direction(const Tensor< type, 1 > &, const Tensor< type, 1 > &, const Tensor< type, 1 > &, Tensor< type, 1 > &) const
string write_optimization_algorithm_type() const
Write a string with best algorithm type for the model.
const LearningRateAlgorithm & get_learning_rate_algorithm() const
Returns a constant reference to the learning rate algorithm object inside the conjugate gradient meth...
void set_save_period(const Index &)
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 calculate_PR_parameter(const Tensor< type, 1 > &, const Tensor< type, 1 > &) const
void set_training_direction_method(const TrainingDirectionMethod &)
type maximum_time
Maximum training time. It is used as a stopping criterion.
void set_maximum_epochs_number(const Index &)
void set_minimum_loss_decrease(const type &)
TrainingDirectionMethod
Enumeration of the available training operators for obtaining the training direction.
virtual ~ConjugateGradient()
Destructor.
type calculate_FR_parameter(const Tensor< type, 1 > &, const Tensor< type, 1 > &) const
type training_loss_goal
Goal value for the loss. It is used as a stopping criterion.
Index maximum_epochs_number
Maximum number of epochs to perform_training. It is used as a stopping criterion.
void write_XML(tinyxml2::XMLPrinter &) const
const Index & get_maximum_selection_failures() const
Returns the maximum number of selection error increases during the training process.
void calculate_PR_training_direction(const Tensor< type, 1 > &, const Tensor< type, 1 > &, const Tensor< type, 1 > &, Tensor< type, 1 > &) const
string write_training_direction_method() const
Returns a string with the name of the training direction.
TrainingDirectionMethod training_direction_method
Applied method for calculating the conjugate gradient direction.
void update_parameters(const DataSetBatch &batch, NeuralNetworkForwardPropagation &forward_propagation, LossIndexBackPropagation &back_propagation, ConjugateGradientData &optimization_data)
ConjugateGradient::update_parameters.
const TrainingDirectionMethod & get_training_direction_method() const
Returns the conjugate gradient training direction method used for training.
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.
This abstract class represents the concept of loss index composed of an error term and a regularizati...
Definition: loss_index.h:48
ConjugateGradientData()
Default constructor.
This structure contains the optimization algorithm results.