10#define NEURALNETWORK_H
27#include "perceptron_layer.h"
28#include "scaling_layer.h"
29#include "unscaling_layer.h"
30#include "bounding_layer.h"
31#include "probabilistic_layer.h"
32#include "convolutional_layer.h"
33#include "pooling_layer.h"
34#include "long_short_term_memory_layer.h"
35#include "recurrent_layer.h"
39 struct NeuralNetworkForwardPropagation;
40 struct NeuralNetworkBackPropagation;
51 enum class ProjectType{Approximation, Classification, Forecasting, ImageApproximation, ImageClassification};
57 explicit NeuralNetwork(
const NeuralNetwork::ProjectType&,
const Tensor<Index, 1>&);
58 explicit NeuralNetwork(
const NeuralNetwork::ProjectType&,
const initializer_list<Index>&);
60 explicit NeuralNetwork(
const Tensor<Index, 1>&,
const Index&,
const Tensor<Index, 1>&,
const Index&);
100 Layer* get_layer_pointer(
const Index&)
const;
111 Layer* get_last_trainable_layer_pointer()
const;
120 void set(
const NeuralNetwork::ProjectType&,
const Tensor<Index, 1>&);
121 void set(
const NeuralNetwork::ProjectType&,
const initializer_list<Index>&);
122 void set(
const Tensor<Index, 1>&,
const Index&,
const Tensor<Index, 1>&,
const Index&);
124 void set(
const string&);
126 void set_layers_pointers(Tensor<Layer*, 1>&);
136 void set_threads_number(
const int&);
146 Tensor<Index, 1> get_layers_neurons_numbers()
const;
148 Index get_trainable_layers_number()
const;
150 Index get_perceptron_layers_number()
const;
151 Index get_probabilistic_layers_number()
const;
152 Index get_long_short_term_memory_layers_number()
const;
153 Index get_recurrent_layers_number()
const;
158 Index get_outputs_number()
const;
160 Tensor<Index, 1> get_trainable_layers_neurons_numbers()
const;
161 Tensor<Index, 1> get_trainable_layers_inputs_numbers()
const;
162 Tensor<Index, 1> get_trainable_layers_synaptic_weight_numbers()
const;
171 Tensor<Index, 1> get_trainable_layers_parameters_numbers()
const;
172 Tensor<Tensor<type, 1>, 1> get_trainable_layers_parameters(
const Tensor<type, 1>&)
const;
211 void save(
const string&)
const;
214 virtual void load(
const string&);
217 Tensor<string, 1> get_layers_names()
const;
221 string write_expression()
const;
228 void save_outputs(
const Tensor<type, 2>&,
const string&);
237 string name =
"neural_network";
256 #include "../../opennn-cuda/opennn-cuda/neural_network_cuda.h"
269 set(new_batch_samples_number, new_neural_network_pointer);
276 void set(
const Index& new_batch_samples_number,
NeuralNetwork* new_neural_network_pointer)
278 if(new_batch_samples_number == 0)
return;
280 batch_samples_number = new_batch_samples_number;
282 neural_network_pointer = new_neural_network_pointer;
286 const Index trainable_layers_number = trainable_layers_pointers.size();
288 layers.resize(trainable_layers_number);
290 for(Index i = 0; i < trainable_layers_number; i++)
292 switch (trainable_layers_pointers(i)->get_type())
294 case Layer::Type::Perceptron:
300 case Layer::Type::Probabilistic:
302 layers(i) =
new ProbabilisticLayerForwardPropagation(new_batch_samples_number, trainable_layers_pointers(i));
306 case Layer::Type::Recurrent:
308 layers(i) =
new RecurrentLayerForwardPropagation(new_batch_samples_number, trainable_layers_pointers(i));
312 case Layer::Type::LongShortTermMemory:
314 layers(i) =
new LongShortTermMemoryLayerForwardPropagation(new_batch_samples_number, trainable_layers_pointers(i));
318 case Layer::Type::Convolutional:
320 layers(i) =
new ConvolutionalLayerForwardPropagation(new_batch_samples_number, trainable_layers_pointers(i));
331 const Index layers_number = layers.size();
333 cout <<
"Layers number: " << layers_number << endl;
335 for(Index i = 0; i < layers_number; i++)
337 cout <<
"Layer " << i + 1 << endl;
343 Index batch_samples_number = 0;
345 NeuralNetwork* neural_network_pointer =
nullptr;
347 Tensor<LayerForwardPropagation*, 1> layers;
357 batch_samples_number = new_batch_samples_number;
359 neural_network_pointer = new_neural_network_pointer;
362 void set(
const Index& new_batch_samples_number,
NeuralNetwork* new_neural_network_pointer)
364 batch_samples_number = new_batch_samples_number;
366 neural_network_pointer = new_neural_network_pointer;
370 const Index trainable_layers_number = trainable_layers_pointers.size();
372 layers.resize(trainable_layers_number);
374 for(Index i = 0; i < trainable_layers_number; i++)
376 switch (trainable_layers_pointers(i)->get_type())
378 case Layer::Type::Perceptron:
384 case Layer::Type::Probabilistic:
390 case Layer::Type::Recurrent:
396 case Layer::Type::LongShortTermMemory:
402 case Layer::Type::Convolutional:
415 cout <<
"Neural network back-propagation" << endl;
417 const Index layers_number = layers.size();
419 cout <<
"Layers number: " << layers_number << endl;
421 for(Index i = 0; i < layers_number; i++)
423 cout <<
"Layer " << i + 1 << endl;
429 Index batch_samples_number = 0;
433 Tensor<LayerBackPropagation*, 1> layers;
443 batch_samples_number = new_batch_samples_number;
445 neural_network_pointer = new_neural_network_pointer;
448 void set(
const Index& new_batch_samples_number,
NeuralNetwork* new_neural_network_pointer)
450 batch_samples_number = new_batch_samples_number;
452 neural_network_pointer = new_neural_network_pointer;
456 const Index trainable_layers_number = trainable_layers_pointers.size();
458 layers.resize(trainable_layers_number);
460 for(Index i = 0; i < trainable_layers_number; i++)
462 switch (trainable_layers_pointers(i)->get_type())
464 case Layer::Type::Perceptron:
470 case Layer::Type::Probabilistic:
478 ostringstream buffer;
480 buffer <<
"OpenNN Exception: NeuralNetwork class.\n"
481 <<
"Levenberg-Marquardt can only be used with Perceptron and Probabilistic layers.\n";
483 throw logic_error(buffer.str());
491 const Index layers_number = layers.size();
493 cout <<
"Layers number: " << layers_number << endl;
495 for(Index i = 0; i < layers_number; i++)
497 cout <<
"Layer " << i + 1 << endl;
503 Index batch_samples_number = 0;
507 Tensor<LayerBackPropagationLM*, 1> layers;
This class represents a layer of bounding neurons.
This abstract class represents the concept of layer of neurons in OpenNN.
Type
This enumeration represents the possible types of layers.
ScalingLayer * get_scaling_layer_pointer() const
Returns a pointer to the scaling layers object composing this neural network object.
string write_expression_c() const
Returns a string with the c function of the expression represented by the neural network.
Tensor< string, 1 > outputs_names
Names of ouputs.
Tensor< Layer *, 1 > get_trainable_layers_pointers() const
Returns a pointer to the trainable layers object composing this neural network object.
bool has_long_short_term_memory_layer() const
void load_parameters_binary(const string &)
bool has_scaling_layer() const
void set_parameters_constant(const type &)
Initializes all the neural and the independent parameters with a given value.
bool check_layer_type(const Layer::Type)
Tensor< Index, 1 > get_trainable_layers_indices() const
Returns a vector with the indices of the trainable layers.
const Tensor< string, 1 > & get_outputs_names() const
Returns a string vector with the names of the variables used as outputs.
type calculate_parameters_norm() const
Returns the norm of the vector of parameters.
const bool & get_display() const
Tensor< Layer *, 1 > layers_pointers
Layers.
Index get_inputs_number() const
Returns the number of inputs to the neural network.
LongShortTermMemoryLayer * get_long_short_term_memory_layer_pointer() const
Returns a pointer to the long short term memory layer of this neural network, if exits.
virtual void from_XML(const tinyxml2::XMLDocument &)
virtual void set_default()
Sets those members which are not pointer to their default values.
bool has_probabilistic_layer() const
virtual void load(const string &)
string write_expression_python() const
Returns a string with the python function of the expression represented by the neural network.
bool has_unscaling_layer() const
bool display
Display messages to screen.
Tensor< type, 2 > calculate_directional_inputs(const Index &, const Tensor< type, 1 > &, const type &, const type &, const Index &=101) const
bool has_recurrent_layer() const
bool has_convolutional_layer() const
bool has_bounding_layer() const
Tensor< Layer *, 1 > get_layers_pointers() const
Returns a pointer to the layers object composing this neural network object.
Index get_input_index(const string &) const
Tensor< string, 2 > get_information() const
const Tensor< string, 1 > & get_inputs_names() const
Returns a string vector with the names of the variables used as inputs.
void save_expression_python(const string &)
virtual ~NeuralNetwork()
Destructor.
void save_parameters(const string &) const
void forward_propagate(const DataSetBatch &, NeuralNetworkForwardPropagation &) const
Calculate forward propagation in neural network.
void save(const string &) const
Tensor< Index, 1 > get_architecture() const
void set_inputs_number(const Index &)
Index get_layers_number() const
string get_output_name(const Index &) const
Index get_output_index(const string &) const
Tensor< type, 2 > calculate_outputs(const Tensor< type, 2 > &)
void set_parameters(Tensor< type, 1 > &)
void save_expression_c(const string &)
Tensor< string, 2 > get_perceptron_layers_information() const
For each perceptron layer: inputs, neurons, activation function.
string get_input_name(const Index &) const
Tensor< string, 2 > get_probabilistic_layer_information() const
For each probabilistic layer: inputs, neurons, activation function.
void set_parameters_random()
BoundingLayer * get_bounding_layer_pointer() const
Returns a pointer to the bounding layers object composing this neural network object.
Tensor< string, 1 > inputs_names
Names of inputs.
void print() const
Prints to the screen the most important information about the neural network object.
UnscalingLayer * get_unscaling_layer_pointer() const
Returns a pointer to the unscaling layers object composing this neural network object.
void set_display(const bool &)
ProbabilisticLayer * get_probabilistic_layer_pointer() const
Returns a pointer to the first probabilistic layer composing this neural network.
void set_inputs_names(const Tensor< string, 1 > &)
virtual void write_XML(tinyxml2::XMLPrinter &) const
RecurrentLayer * get_recurrent_layer_pointer() const
Returns a pointer to the recurrent layer of this neural network, if exits.
Index get_parameters_number() const
void perturbate_parameters(const type &)
void save_outputs(const Tensor< type, 2 > &, const string &)
void set_outputs_names(const Tensor< string, 1 > &)
Tensor< type, 1 > get_parameters() const
This class represents a layer of perceptrons.
This class represents a layer of probabilistic neurons.
This class represents a layer of scaling neurons.
This class represents a layer of unscaling neurons.
virtual ~NeuralNetworkForwardPropagation()
Destructor.
NeuralNetworkForwardPropagation()
Default constructor.