OpenNN  2.2
Open Neural Networks Library
multilayer_perceptron.h
1 /****************************************************************************************************************/
2 /* */
3 /* OpenNN: Open Neural MultilayerPerceptrons Library */
4 /* www.artelnics.com/opennn */
5 /* */
6 /* M U L T I L A Y E R P E R C E P T R O N 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 _MULTILAYERPERCEPTRON_H__
15 #define _MULTILAYERPERCEPTRON_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 "perceptron.h"
29 #include "perceptron_layer.h"
30 #include "inputs.h"
31 #include "outputs.h"
32 
33 #include "vector.h"
34 #include "matrix.h"
35 
36 // TinyXml includes
37 
38 #include "../tinyxml2/tinyxml2.h"
39 
40 
41 namespace OpenNN
42 {
43 
47 
49 {
50 
51 public:
52 
53  // DEFAULT CONSTRUCTOR
54 
55  explicit MultilayerPerceptron(void);
56 
57  // LAYER CONSTRUCTOR
58 
60 
61  // NETWORK ARCHITECTURE CONSTRUCTOR
62 
63  explicit MultilayerPerceptron(const Vector<size_t>&);
64 
65  // ONE LAYER CONSTRUCTOR
66 
67  explicit MultilayerPerceptron(const size_t&, const size_t&);
68 
69  // TWO LAYERS CONSTRUCTOR
70 
71  explicit MultilayerPerceptron(const size_t&, const size_t&, const size_t&);
72 
73  // COPY CONSTRUCTOR
74 
76 
77  // DESTRUCTOR
78 
79  virtual ~MultilayerPerceptron(void);
80 
81  // ASSIGNMENT OPERATOR
82 
84 
85  // EQUAL TO OPERATOR
86 
87  bool operator == (const MultilayerPerceptron&) const;
88 
89  // GET METHODS
90 
99 
101  {
102  const size_t layers_number = get_layers_number();
103 
104  Vector<size_t> architecture;
105 
106  if(layers_number != 0)
107  {
108  const size_t inputs_number = get_inputs_number();
109  architecture.resize(1+layers_number);
110 
111  architecture[0] = inputs_number;
112 
114 
115  for(size_t i = 0; i < layers_number; i++)
116  {
117  architecture[i+1] = layers_size[i];
118  }
119  }
120 
121  return(architecture);
122  }
123 
124 
126 
128  {
129  const size_t layers_number = get_layers_number();
130 
131  Vector<size_t> complexity;
132 
133  if(layers_number != 1)
134  {
135  complexity.resize(layers_number-1);
136 
138 
139  for(size_t i = 0; i < layers_number-1; i++)
140  {
141  complexity[i] = layers_size[i];
142  }
143  }
144 
145  return(complexity);
146  }
147 
148 
150 
151  inline size_t get_inputs_number(void) const
152  {
153  const size_t layers_number = get_layers_number();
154 
155  if(layers_number == 0)
156  {
157  return(0);
158  }
159  else
160  {
161  return(layers[0].get_inputs_number());
162  }
163  }
164 
166 
167  inline size_t get_layers_number(void) const
168  {
169  return(layers.size());
170  }
171 
173 
175  {
176  const size_t layers_number = get_layers_number();
177 
178  Vector<size_t> layers_inputs_number(layers_number);
179 
180  for(size_t i = 0; i < layers_number; i++)
181  {
182  layers_inputs_number[i] = layers[i].get_inputs_number();
183  }
184 
185  return(layers_inputs_number);
186  }
187 
188 
190 
192  {
193  const size_t layers_number = get_layers_number();
194 
195  Vector<size_t> layers_perceptrons_number(layers_number);
196 
197  for(size_t i = 0; i < layers_number; i++)
198  {
199  layers_perceptrons_number[i] = layers[i].get_perceptrons_number();
200  }
201 
202  return(layers_perceptrons_number);
203  }
204 
205 
207 
208  inline size_t get_outputs_number(void) const
209  {
210  const size_t layers_number = get_layers_number();
211 
212  if(layers_number == 0)
213  {
214  return(0);
215  }
216  else
217  {
218  return(layers[layers_number-1].get_perceptrons_number());
219  }
220  }
221 
222  const Vector<PerceptronLayer>& get_layers(void) const;
223  const PerceptronLayer& get_layer(const size_t&) const;
224  PerceptronLayer* get_layer_pointer(const size_t&);
225 
226  size_t count_perceptrons_number(void) const;
228 
229  // Parameters
230 
233 
236 
238 
239  size_t count_parameters_number(void) const;
240  Vector<double> arrange_parameters(void) const;
241 
243 
244  size_t get_layer_index(const size_t&) const;
245  size_t get_perceptron_index(const size_t&, const size_t&) const;
246 
247  size_t get_layer_bias_index(const size_t&, const size_t&) const;
248  size_t get_layer_synaptic_weight_index(const size_t&, const size_t&, const size_t&) const;
249 
250  Vector<size_t> arrange_parameter_indices(const size_t&) const;
252 
253  // Activation functions
254 
257 
258  // Display messages
259 
260  const bool& get_display(void) const;
261 
262  // SET METHODS
263 
264  void set(void);
265  void set(const Vector<PerceptronLayer>&);
266  void set(const Vector<size_t>&);
267  void set(const size_t&, const size_t&);
268  void set(const size_t&, const size_t&, const size_t&);
269  void set(const MultilayerPerceptron&);
270 
271  virtual void set_default(void);
272 
273  // Architecture
274 
275  void set_inputs_number(const size_t&);
276 
278  void set_layer_perceptrons_number(const size_t&, const size_t&);
279 
280  void set_layers(const Vector<PerceptronLayer>&);
281  //void set_layer(const size_t&, const PerceptronLayer&);
282 
283  // Parameters
284 
285  void set_layers_biases(const Vector< Vector<double> >&);
287 
289 
290  void set_parameters(const Vector<double>&);
291 
292  void initialize_biases(const double&);
293  void initialize_synaptic_weights(const double&);
294  void initialize_parameters(const double&);
295 
296  void randomize_parameters_uniform(void);
297  void randomize_parameters_uniform(const double&, const double&);
300 
301  void randomize_parameters_normal(void);
302  void randomize_parameters_normal(const double&, const double&);
305 
306  void initialize_parameters(void);
307 
308  void perturbate_parameters(const double&);
309 
310  double calculate_parameters_norm(void) const;
311 
312  // Activation functions
313 
316 
318 
319 
320  // Display messages
321 
322  void set_display(const bool&);
323 
324  // Check methods
325 
326  bool is_empty(void) const;
327 
328  // Growing and pruning
329 
330  void grow_input(void);
331  void grow_layer(const size_t&, const size_t&);
332 
333  void prune_input(const size_t&);
334  void prune_output(const size_t&);
335 
336  void prune_layer(const size_t&, const size_t&);
337 
338  // Multilayer perceptron initialization methods
339 
340  void initialize_random(void);
341 
342  // PerceptronLayer combination-combination
343 
346 
347  // Layers multilayer_perceptron_pointer outputs delta
348 
350 
352 // Vector< Matrix<double> > calculate_output_layers_delta(const Vector< Vector<double> >&, const Vector<double>&, const Vector<double>&) const;
353 
354  // Interlayers multilayer_perceptron_pointer outputs Delta
355 
357 
358  // Interlayer combination-combination
359 
360  Vector<double> calculate_interlayer_combination_combination(const size_t&, const size_t&, const Vector<double>&) const;
361  Matrix<double> calculate_interlayer_combination_combination_Jacobian(const size_t&, const size_t&, const Vector<double>&) const;
362 
363  // Forward propagation inputs
364 
368 
369  // Forward propagation combination
370 
373 
375 
377 
378  // Forward propagation activation
379 
383 
384  // Forward propagation outputs
385 
386  //Vector< Vector<double> > calculate_layers_output(const Vector<double>&) const;
389 
392 
393  // Forward propagation
394 
395  //Vector< Vector< Vector<double> > > calculate_zero_order_forward_propagation(const Vector<double>&) const;
398 
399  // Output
400 
404 
408 
409  // Serialization methods
410 
411  tinyxml2::XMLDocument* to_XML(void) const;
412  void from_XML(const tinyxml2::XMLDocument&);
413 
414  // Information
415 
417 
418  // Expression methods
419 
420  std::string write_expression(const Vector<std::string>&, const Vector<std::string>&) const;
421 
422  std::string to_string(void) const;
423 
424 
425 protected:
426 
427  // MEMBERS
428 
432 
434 
436 
437  bool display;
438 };
439 
440 }
441 
442 #endif
443 
444 
445 // OpenNN: Open Neural MultilayerPerceptrons Library.
446 // Copyright (c) 2005-2015 Roberto Lopez.
447 //
448 // This library is free software; you can redistribute it and/or
449 // modify it under the terms of the GNU Lesser General Public
450 // License as published by the Free Software Foundation; either
451 // version 2.1 of the License, or any later version.
452 //
453 // This library is distributed in the hope that it will be useful,
454 // but WITHOUT ANY WARRANTY; without even the implied warranty of
455 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
456 // Lesser General Public License for more details.
457 
458 // You should have received a copy of the GNU Lesser General Public
459 // License along with this library; if not, write to the Free Software
460 
461 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
462 
Vector< double > calculate_output_layer_combination(const size_t &, const Vector< double > &) const
void set_layer_perceptrons_number(const size_t &, const size_t &)
tinyxml2::XMLDocument * to_XML(void) const
Vector< size_t > count_layers_parameters_numbers(void) const
Vector< std::string > write_layers_activation_function(void) const
bool is_empty(void) const
Returns true if the number of layers in the multilayer perceptron is zero, and false otherwise...
Vector< Perceptron::ActivationFunction > get_layers_activation_function(void) const
Returns the activation function of every layer in a single vector.
const Vector< PerceptronLayer > & get_layers(void) const
std::string write_expression(const Vector< std::string > &, const Vector< std::string > &) const
void set_layers_activation_function(const Vector< Perceptron::ActivationFunction > &)
Vector< Vector< double > > arrange_layers_biases(void) const
Matrix< double > calculate_interlayer_combination_combination_Jacobian(const size_t &, const size_t &, const Vector< double > &) const
void initialize_synaptic_weights(const double &)
size_t get_layer_synaptic_weight_index(const size_t &, const size_t &, const size_t &) const
Vector< Vector< Matrix< double > > > calculate_layers_Hessian_form(const Vector< double > &) const
size_t get_layer_bias_index(const size_t &, const size_t &) const
size_t get_inputs_number(void) const
Returns the number of inputs to the multilayer perceptron.
Matrix< size_t > arrange_parameters_indices(void) const
Matrix< Matrix< double > > calculate_interlayers_combination_combination_Jacobian(const Vector< double > &) const
size_t get_layers_number(void) const
Returns the number of layers in the multilayer perceptron.
Vector< double > calculate_layer_combination_combination(const size_t &, const Vector< double > &) const
void from_XML(const tinyxml2::XMLDocument &)
Vector< Matrix< double > > calculate_layers_combination_Jacobian(const Vector< double > &) const
void perturbate_parameters(const double &)
Vector< size_t > arrange_layers_perceptrons_numbers(void) const
Returns a vector with the size of each layer.
ActivationFunction
Enumeration of available activation functions for the perceptron neuron model.
Definition: perceptron.h:72
size_t get_outputs_number(void) const
Returns the number of outputs neurons in the multilayer perceptron.
Vector< double > calculate_outputs(const Vector< double > &) const
Vector< Vector< double > > get_layers_parameters(void) const
Vector< size_t > arrange_complexity(void) const
Returns a vector with the sizes of the hidden layers in the multilayer perceptron.
Matrix< Vector< Matrix< double > > > calculate_output_interlayers_Delta(const Vector< Vector< Vector< double > > > &, const Matrix< Matrix< double > > &, const Vector< Matrix< double > > &) const
Vector< Vector< double > > calculate_layers_combination(const Vector< double > &) const
void set_parameters(const Vector< double > &)
void set(void)
Sets an empty multilayer_perceptron_pointer architecture.
Vector< size_t > arrange_layers_parameters_number(void) const
Vector< size_t > arrange_parameter_indices(const size_t &) const
Vector< Vector< double > > calculate_layers_input(const Vector< double > &) const
size_t count_perceptrons_number(void) const
void initialize_parameters(void)
Initializes the parameters at random with values chosen from a normal distribution with mean 0 and st...
void grow_layer(const size_t &, const size_t &)
Vector< Vector< Vector< double > > > calculate_perceptrons_combination_parameters_gradient(const Vector< Vector< double > > &) const
void set_layers_parameters(const Vector< Vector< double > > &)
double calculate_parameters_norm(void) const
Returns the norm of the vector of multilayer perceptron parameters.
const bool & get_display(void) const
const PerceptronLayer & get_layer(const size_t &) const
Vector< size_t > arrange_architecture(void) const
void prune_layer(const size_t &, const size_t &)
std::string to_string(void) const
Returns a string representation of the current multilayer perceptron object.
Vector< Vector< double > > calculate_layers_activation_second_derivative(const Vector< double > &) const
Matrix< double > calculate_Jacobian(const Vector< double > &) const
PerceptronLayer * get_layer_pointer(const size_t &)
Matrix< double > calculate_layer_combination_combination_Jacobian(const size_t &, const Vector< double > &) const
Vector< PerceptronLayer > layers
Vector< size_t > get_layers_inputs_number(void) const
Returns a vector with the number of inputs of each layer.
Vector< double > calculate_interlayer_combination_combination(const size_t &, const size_t &, const Vector< double > &) const
Vector< Matrix< double > > calculate_layers_combination_parameters_Jacobian(const Vector< Vector< double > > &) const
Vector< Vector< double > > calculate_layers_activation_derivative(const Vector< double > &) const
Vector< size_t > count_cumulative_perceptrons_number(void) const
Returns a vector of size the number of layers, where each element is equal to the total number of neu...
Vector< Matrix< double > > calculate_layers_Jacobian(const Vector< double > &) const
void set_layers_perceptrons_number(const Vector< size_t > &)
Vector< Vector< double > > calculate_layers_activation(const Vector< double > &) const
Vector< Matrix< double > > calculate_output_layers_delta(const Vector< Vector< double > > &) const
void set_layers_synaptic_weights(const Vector< Matrix< double > > &)
Matrix< std::string > write_information(void) const
size_t get_perceptron_index(const size_t &, const size_t &) const
Vector< double > arrange_parameters(void) const
Returns the values of all the biases and synaptic weights in the multilayer perceptron as a single ve...
Vector< Vector< Vector< double > > > calculate_first_order_forward_propagation(const Vector< double > &) const
Vector< size_t > arrange_layers_cumulative_parameters_number(void) const
MultilayerPerceptron & operator=(const MultilayerPerceptron &)
void set_layers(const Vector< PerceptronLayer > &)
void set_layer_activation_function(const size_t &, const Perceptron::ActivationFunction &)
Vector< Matrix< double > > calculate_Hessian_form(const Vector< double > &) const
bool operator==(const MultilayerPerceptron &) const
bool display
Display messages to screen.
void set_layers_biases(const Vector< Vector< double > > &)
size_t get_layer_index(const size_t &) const
Vector< Vector< Vector< double > > > calculate_second_order_forward_propagation(const Vector< double > &) const
Vector< Matrix< double > > arrange_layers_synaptic_weights(void) const
Vector< Vector< double > > arrange_layers_input(const Vector< double > &, const Vector< Vector< double > > &) const
size_t count_parameters_number(void) const
Returns the number of parameters (biases and synaptic weights) in the multilayer perceptron.