OpenNN  2.2
Open Neural Networks Library
neural_network.h
1 /****************************************************************************************************************/
2 /* */
3 /* OpenNN: Open Neural Networks Library */
4 /* www.artelnics.com/opennn */
5 /* */
6 /* N E U R A L N E T W O R K C L A S S H E A D E R */
7 /* */
8 /* Roberto Lopez */
9 /* Artelnics - Making intelligent use of data */
11 /* */
12 /****************************************************************************************************************/
13 
14 #ifndef __NEURALNETWORK_H__
15 #define __NEURALNETWORK_H__
16 
17 // System includes
18 
19 #include <cmath>
20 #include <cstdlib>
21 #include <fstream>
22 #include <iostream>
23 #include <string>
24 #include <sstream>
25 
26 // OpenNN includes
27 
28 #include "vector.h"
29 #include "matrix.h"
30 
31 #include "data_set.h"
32 
33 #include "perceptron.h"
34 #include "perceptron_layer.h"
35 #include "multilayer_perceptron.h"
36 #include "scaling_layer.h"
37 #include "unscaling_layer.h"
38 #include "bounding_layer.h"
39 #include "probabilistic_layer.h"
40 #include "conditions_layer.h"
41 #include "independent_parameters.h"
42 #include "inputs.h"
43 #include "outputs.h"
44 
45 // TinyXml includes
46 
47 #include "../tinyxml2/tinyxml2.h"
48 
49 namespace OpenNN
50 {
51 
56 
58 {
59 
60 public:
61 
62  // DEFAULT CONSTRUCTOR
63 
64  explicit NeuralNetwork(void);
65 
66  // MULTILAYER PERCEPTRON CONSTRUCTOR
67 
68  explicit NeuralNetwork(const MultilayerPerceptron&);
69 
70  // MULTILAYER PERCEPTRON ARCHITECTURE CONSTRUCTOR
71 
72  explicit NeuralNetwork(const Vector<size_t>&);
73 
74  // ONE PERCEPTRON LAYER CONSTRUCTOR
75 
76  explicit NeuralNetwork(const size_t&, const size_t&);
77 
78  // TWO PERCEPTRON LAYERS CONSTRUCTOR
79 
80  explicit NeuralNetwork(const size_t&, const size_t&, const size_t&);
81 
82  // INDEPENDENT PARAMETERS CONSTRUCTOR
83 
84  //explicit NeuralNetwork(const IndependentParameters&);
85 
86  // INDEPENDENT PARAMETERS NUMBER CONSTRUCTOR
87 
88  explicit NeuralNetwork(const size_t&);
89 
90  // MULTILAYER PERCEPTRON AND INDEPENDENT PARAMETERS CONSTRUCTOR
91 
92  //explicit NeuralNetwork(const MultilayerPerceptron&, const IndependentParameters&);
93 
94  // MULTILAYER PERCEPTRON ARCHITECTURE AND INDEPENDENT PARAMETERS NUMBER CONSTRUCTOR
95 
96  //explicit NeuralNetwork(const Vector<size_t>&, const size_t&);
97 
98  // FILE CONSTRUCTOR
99 
100  explicit NeuralNetwork(const std::string&);
101 
102  // XML CONSTRUCTOR
103 
104  explicit NeuralNetwork(const tinyxml2::XMLDocument&);
105 
106  // COPY CONSTRUCTOR
107 
109 
110  // DESTRUCTOR
111 
112  virtual ~NeuralNetwork(void);
113 
114  // ASSIGNMENT OPERATOR
115 
117 
118  // EQUAL TO OPERATOR
119 
120  bool operator == (const NeuralNetwork&) const;
121 
122 
123  // GET METHODS
124 
125  bool has_multilayer_perceptron(void) const;
126  bool has_inputs(void) const;
127  bool has_outputs(void) const;
128  bool has_scaling_layer(void) const;
129  bool has_unscaling_layer(void) const;
130  bool has_bounding_layer(void) const;
131  bool has_probabilistic_layer(void) const;
132  bool has_conditions_layer(void) const;
133  bool has_independent_parameters(void) const;
134 
136  Inputs* get_inputs_pointer(void) const;
137  Outputs* get_outputs_pointer(void) const;
144 
145  const bool& get_display(void) const;
146 
147  // SET METHODS
148 
149  void set(void);
150 
151  void set(const MultilayerPerceptron&);
152  void set(const Vector<size_t>&);
153  void set(const size_t&, const size_t&);
154  void set(const size_t&, const size_t&, const size_t&);
155 
156 // void set(const IndependentParameters&);
157  void set(const size_t&);
158 
159  void set(const std::string&);
160  void set(const NeuralNetwork&);
161 
162  virtual void set_default(void);
163 
170  void set_inputs_pointer(Inputs*);
173 
174  void set_display(const bool&);
175 
176  // Growing and pruning
177 
178  void prune_input(const size_t&);
179  void prune_output(const size_t&);
180 
181  void resize_inputs_number(const size_t&);
182  void resize_outputs_number(const size_t&);
183 
184  // Pointer methods
185 
187  void construct_scaling_layer(void);
188  void construct_unscaling_layer(void);
189  void construct_bounding_layer(void);
191  void construct_conditions_layer(void);
192  void construct_inputs(void);
193  void construct_outputs(void);
195 
197  void destruct_scaling_layer(void);
198  void destruct_unscaling_layer(void);
199  void destruct_bounding_layer(void);
200  void destruct_probabilistic_layer(void);
201  void destruct_conditions_layer(void);
202  void destruct_inputs(void);
203  void destruct_outputs(void);
205 
206  void delete_pointers(void);
207 
208  // Initialization methods
209 
210  void initialize_random(void);
211 
212  // Layers
213 
214  size_t get_layers_number(void);
215 
216  // Architecture
217 
218  size_t get_inputs_number(void) const;
219  size_t get_outputs_number(void) const;
220 
222 
223  // Parameters
224 
225  size_t count_parameters_number(void) const;
226  Vector<double> arrange_parameters(void) const;
227 
228  void set_parameters(const Vector<double>&);
229 
230  // Parameters initialization methods
231 
232  void initialize_parameters(const double&);
233 
234  void randomize_parameters_uniform(void);
235  void randomize_parameters_uniform(const double&, const double&);
238 
239  void randomize_parameters_normal(void);
240  void randomize_parameters_normal(const double&, const double&);
243 
244  // Parameters
245 
246  double calculate_parameters_norm(void) const;
248  Histogram<double> calculate_parameters_histogram(const size_t& = 10) const;
249 
250  // Output
251 
255 
256  Matrix<double> calculate_directional_input_data(const size_t&, const Vector<double>&, const double&, const double&, const size_t& = 101) const;
257 
261 
264 
265  // Serialization methods
266 
267  std::string to_string(void) const;
268 
269  virtual tinyxml2::XMLDocument* to_XML(void) const;
270  virtual void from_XML(const tinyxml2::XMLDocument&);
271 
272  void print(void) const;
273  void save(const std::string&) const;
274  void save_parameters(const std::string&) const;
275 
276  virtual void load(const std::string&);
277  void load_parameters(const std::string&);
278 
279  void save_data(const std::string&) const;
280 
281  // Expression methods
282 
283  std::string write_expression(void) const;
284 
285  void save_expression(const std::string&);
286 
287 protected:
288 
289  // MEMBERS
290 
292 
294 
296 
298 
300 
302 
304 
306 
308 
310 
312 
314 
316 
318 
320 
322 
324 
326 
328 
329  bool display;
330 };
331 
332 }
333 
334 #endif
335 
336 
337 // OpenNN: Open Neural Networks Library.
338 // Copyright (c) 2005-2015 Roberto Lopez.
339 //
340 // This library is free software; you can redistribute it and/or
341 // modify it under the terms of the GNU Lesser General Public
342 // License as published by the Free Software Foundation; either
343 // version 2.1 of the License, or any later version.
344 //
345 // This library is distributed in the hope that it will be useful,
346 // but WITHOUT ANY WARRANTY; without even the implied warranty of
347 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
348 // Lesser General Public License for more details.
349 
350 // You should have received a copy of the GNU Lesser General Public
351 // License along with this library; if not, write to the Free Software
352 
353 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
354 
void load_parameters(const std::string &)
bool has_independent_parameters(void) const
size_t count_parameters_number(void) const
bool has_inputs(void) const
Outputs * get_outputs_pointer(void) const
Returns a pointer to the outputs object composing this neural network.
void construct_scaling_layer(void)
size_t get_layers_number(void)
ScalingLayer * get_scaling_layer_pointer(void) const
Returns a pointer to the scaling layer composing this neural network.
bool display
Display messages to screen.
size_t get_inputs_number(void) const
Returns the number of inputs to the neural network.
void destruct_independent_parameters(void)
This method deletes the independent parameters object within the neural network.
bool has_outputs(void) const
Vector< double > arrange_parameters(void) const
std::string write_expression(void) const
Returns a string with the expression of the function represented by the neural network.
Vector< Matrix< double > > calculate_Hessian_form(const Vector< double > &) const
void set_outputs_pointer(Outputs *)
BoundingLayer * bounding_layer_pointer
Pointer to a bounding layer object.
double calculate_parameters_norm(void) const
Returns the norm of the vector of parameters.
Vector< double > calculate_outputs(const Vector< double > &) const
IndependentParameters * independent_parameters_pointer
Pointer to an independent parameters object.
ScalingLayer * scaling_layer_pointer
Pointer to a scaling layer object.
void construct_bounding_layer(void)
NeuralNetwork & operator=(const NeuralNetwork &)
virtual void set_default(void)
Sets those members which are not pointer to their default values.
void set_scaling_layer_pointer(ScalingLayer *)
Matrix< double > calculate_directional_input_data(const size_t &, const Vector< double > &, const double &, const double &, const size_t &=101) const
void prune_output(const size_t &)
void destruct_scaling_layer(void)
This method deletes the scaling layer within the neural network.
void initialize_parameters(const double &)
Initializes all the neural and the independent parameters with a given value.
void set_independent_parameters_pointer(IndependentParameters *)
void destruct_inputs(void)
This method deletes the inputs object within the neural network.
void randomize_parameters_uniform(void)
ProbabilisticLayer * get_probabilistic_layer_pointer(void) const
Returns a pointer to the probabilistic layer composing this neural network.
void destruct_conditions_layer(void)
This method deletes the conditions layer within the neural network.
void destruct_multilayer_perceptron(void)
This method deletes the multilayer perceptron within the neural network.
ConditionsLayer * conditions_layer_pointer
Pointer to a conditions object.
ProbabilisticLayer * probabilistic_layer_pointer
Pointer to a probabilistic layer.
Statistics< double > calculate_parameters_statistics(void) const
void print(void) const
Prints to the screen the members of a neural network object in a XML-type format. ...
void construct_conditions_layer(void)
Inputs * get_inputs_pointer(void) const
Returns a pointer to the inputs object composing this neural network.
void set_probabilistic_layer_pointer(ProbabilisticLayer *)
bool has_conditions_layer(void) const
void save_parameters(const std::string &) const
virtual void from_XML(const tinyxml2::XMLDocument &)
void destruct_probabilistic_layer(void)
This method deletes the probabilistic layer within the neural network.
MultilayerPerceptron * get_multilayer_perceptron_pointer(void) const
Returns a pointer to the multilayer perceptron composing this neural network.
void prune_input(const size_t &)
bool has_unscaling_layer(void) const
UnscalingLayer * unscaling_layer_pointer
Pointer to an unscaling layer object.
Matrix< double > calculate_output_data(const Matrix< double > &) const
Outputs * outputs_pointer
Pointer to an outputs object.
void save_data(const std::string &) const
void set_multilayer_perceptron_pointer(MultilayerPerceptron *)
virtual tinyxml2::XMLDocument * to_XML(void) const
size_t get_outputs_number(void) const
Returns the number of outputs to the neural network.
Vector< Matrix< double > > calculate_Jacobian_data(const Matrix< double > &) const
Matrix< double > calculate_Jacobian(const Vector< double > &) const
void resize_inputs_number(const size_t &)
void construct_independent_parameters(void)
UnscalingLayer * get_unscaling_layer_pointer(void) const
Returns a pointer to the unscaling layer composing this neural network.
void set_unscaling_layer_pointer(UnscalingLayer *)
void construct_multilayer_perceptron(void)
This method constructs an empty multilayer perceptron within the neural network.
BoundingLayer * get_bounding_layer_pointer(void) const
Returns a pointer to the bounding layer composing this neural network.
ConditionsLayer * get_conditions_layer_pointer(void) const
Returns a pointer to the conditions layer composing this neural network.
IndependentParameters * get_independent_parameters_pointer(void) const
Returns a pointer to the independent parameters object composing this neural network.
void save(const std::string &) const
void randomize_parameters_normal(void)
void destruct_bounding_layer(void)
This method deletes the bounding layer within the neural network.
std::string to_string(void) const
Returns a string representation of the current neural network object.
void set_bounding_layer_pointer(BoundingLayer *)
void set_inputs_pointer(Inputs *)
void construct_probabilistic_layer(void)
MultilayerPerceptron * multilayer_perceptron_pointer
Pointer to a multilayer perceptron object.
Inputs * inputs_pointer
Pointer to an inputs object.
bool has_probabilistic_layer(void) const
Vector< size_t > arrange_architecture(void) const
virtual ~NeuralNetwork(void)
Destructor.
const bool & get_display(void) const
void set_conditions_layer_pointer(ConditionsLayer *)
void destruct_outputs(void)
This method deletes the outputs object within the neural network.
void save_expression(const std::string &)
bool has_multilayer_perceptron(void) const
void resize_outputs_number(const size_t &)
void set_display(const bool &)
void destruct_unscaling_layer(void)
This method deletes the unscaling layer within the neural network.
virtual void load(const std::string &)
bool has_bounding_layer(void) const
void construct_unscaling_layer(void)
bool operator==(const NeuralNetwork &) const
bool has_scaling_layer(void) const
void set_parameters(const Vector< double > &)
Histogram< double > calculate_parameters_histogram(const size_t &=10) const