NeuralNetwork Class Reference

This class represents the concept of neural network in the OpenNN library. More...

#include <neural_network.h>

Public Types

enum  ProjectType {
  Approximation, Classification, Forecasting, ImageApproximation,
  ImageClassification
}
 

Public Member Functions

 NeuralNetwork ()
 
 NeuralNetwork (const NeuralNetwork::ProjectType &, const Vector< size_t > &)
 
 NeuralNetwork (const Vector< size_t > &, const size_t &, const Vector< size_t > &, const size_t &)
 
 NeuralNetwork (const string &)
 
 NeuralNetwork (const tinyxml2::XMLDocument &)
 
 NeuralNetwork (const Vector< Layer * > &)
 
 NeuralNetwork (const NeuralNetwork &)
 
virtual ~NeuralNetwork ()
 Destructor.
 
void add_layer (Layer *)
 
bool check_layer_type (const Layer::LayerType)
 
bool has_scaling_layer () const
 
bool has_principal_components_layer () const
 
bool has_long_short_term_memory_layer () const
 
bool has_recurrent_layer () const
 
bool has_unscaling_layer () const
 
bool has_bounding_layer () const
 
bool has_probabilistic_layer () const
 
bool is_empty () const
 
Vector< string > get_inputs_names () const
 Returns a string vector with the names of the variables used as inputs.
 
string get_input_name (const size_t &) const
 
size_t get_input_index (const string &) const
 
Vector< string > get_outputs_names () const
 Returns a string vector with the names of the variables used as outputs.
 
string get_output_name (const size_t &) const
 
size_t get_output_index (const string &) const
 
Vector< Layer * > get_layers_pointers () const
 Returns a pointer to the layers object composing this neural network object.
 
Vector< Layer * > get_trainable_layers_pointers () const
 Returns a pointer to the trainable layers object composing this neural network object.
 
Vector< size_t > get_trainable_layers_indices () const
 Returns a vector with the indices of the trainable layers.
 
ScalingLayerget_scaling_layer_pointer () const
 Returns a pointer to the scaling layers object composing this neural network object.
 
UnscalingLayerget_unscaling_layer_pointer () const
 Returns a pointer to the unscaling layers object composing this neural network object.
 
BoundingLayerget_bounding_layer_pointer () const
 Returns a pointer to the bounding layers object composing this neural network object.
 
ProbabilisticLayerget_probabilistic_layer_pointer () const
 Returns a pointer to the probabilistic layers object composing this neural network object.
 
PrincipalComponentsLayerget_principal_components_layer_pointer () const
 Returns a pointer to the main components of the layers object composing this neural network object.
 
LongShortTermMemoryLayerget_long_short_term_memory_layer_pointer () const
 Returns a pointer to the long short term memory layer of this neural network, if exits.
 
RecurrentLayerget_recurrent_layer_pointer () const
 Returns a pointer to the recurrent layer of this neural network, if exits.
 
Layerget_output_layer_pointer () const
 
Layerget_layer_pointer (const size_t &) const
 
PerceptronLayerget_first_perceptron_layer_pointer () const
 
const bool & get_display () const
 
void set ()
 
void set (const NeuralNetwork::ProjectType &, const Vector< size_t > &)
 
void set (const Vector< size_t > &, const size_t &, const Vector< size_t > &, const size_t &)
 
void set (const string &)
 
void set (const NeuralNetwork &)
 
void set_inputs_names (const Vector< string > &)
 
void set_outputs_names (const Vector< string > &)
 
void set_inputs_number (const size_t &)
 
void set_inputs_number (const Vector< bool > &)
 
virtual void set_default ()
 Sets those members which are not pointer to their default values.
 
void set_layers_pointers (Vector< Layer * > &)
 
void set_scaling_layer (ScalingLayer &)
 
void set_display (const bool &)
 
size_t get_layers_number () const
 
Vector< size_t > get_layers_neurons_numbers () const
 
size_t get_trainable_layers_number () const
 
size_t get_inputs_number () const
 Returns the number of inputs to the neural network.
 
size_t get_outputs_number () const
 
Vector< size_t > get_architecture () const
 
size_t get_parameters_number () const
 
size_t get_trainable_parameters_number () const
 
Vector< double > get_parameters () const
 
Vector< size_t > get_trainable_layers_parameters_numbers () const
 
Vector< Vector< double > > get_trainable_layers_parameters (const Vector< double > &) const
 
void set_parameters (const Vector< double > &)
 
void initialize_parameters (const double &)
 Initializes all the neural and the independent parameters with a given value.
 
void randomize_parameters_uniform (const double &=-1.0, const double &=1.0)
 
void randomize_parameters_normal (const double &=0.0, const double &=1.0)
 
double calculate_parameters_norm () const
 Returns the norm of the vector of parameters.
 
Descriptives calculate_parameters_descriptives () const
 
Histogram calculate_parameters_histogram (const size_t &=10) const
 
void perturbate_parameters (const double &)
 
Tensor< double > calculate_outputs (const Tensor< double > &)
 
Eigen::MatrixXd calculate_outputs_eigen (const Eigen::MatrixXd &)
 
Tensor< double > calculate_trainable_outputs (const Tensor< double > &) const
 
Tensor< double > calculate_trainable_outputs (const Tensor< double > &, const Vector< double > &) const
 
Matrix< double > calculate_directional_inputs (const size_t &, const Vector< double > &, const double &, const double &, const size_t &=101) const
 
Vector< Histogramcalculate_outputs_histograms (const size_t &=1000, const size_t &=10)
 
Vector< Histogramcalculate_outputs_histograms (const Tensor< double > &, const size_t &=10)
 
vector< double > calculate_outputs_std (const vector< double > &)
 
string object_to_string () const
 Returns a string representation of the current neural network object.
 
Matrix< string > get_information () const
 
virtual tinyxml2::XMLDocumentto_XML () const
 
virtual void from_XML (const tinyxml2::XMLDocument &)
 
virtual void write_XML (tinyxml2::XMLPrinter &) const
 
void print () const
 Prints to the screen the members of a neural network object in a XML-type format.
 
void print_summary () const
 
void save (const string &) const
 
void save_parameters (const string &) const
 
virtual void load (const string &)
 
void load_parameters (const string &)
 
void save_data (const string &) const
 
string write_expression () const
 Returns a string with the expression of the function represented by the neural network. More...
 
string write_mathematical_expression_php () const
 Returns a string with the expression of the function represented by the neural network.
 
string write_expression_python () const
 Returns a string with the python function of the expression represented by the neural network.
 
string write_expression_php () const
 
string write_expression_R () const
 
void save_expression (const string &)
 
void save_expression_python (const string &)
 
void save_expression_R (const string &)
 
Vector< Layer::FirstOrderActivationscalculate_trainable_forward_propagation (const Tensor< double > &) const
 Calculate de Forward Propagation in Neural Network

 

Protected Attributes

Vector< string > inputs_names
 Names of inputs.
 
Vector< string > outputs_names
 Names of ouputs.
 
Vector< Layer * > layers_pointers
 Layers.
 
bool display = true
 Display messages to screen.
 

Detailed Description

This class represents the concept of neural network in the OpenNN library.

This neural network is used to span a function space for the problem at hand.

Definition at line 51 of file neural_network.h.

Constructor & Destructor Documentation

◆ NeuralNetwork() [1/7]

NeuralNetwork ( )
explicit

Default constructor. It creates an empty neural network object. All pointers in the object are initialized to nullptr. The rest of members are initialized to their default values.

Definition at line 19 of file neural_network.cpp.

◆ NeuralNetwork() [2/7]

NeuralNetwork ( const NeuralNetwork::ProjectType &  model_type,
const Vector< size_t > &  architecture 
)
explicit

Type of model and architecture of the Neural Network constructor. It creates a neural network object with the given model type and architecture. The rest of members are initialized to their default values.

Parameters
model_typeType of problem to be solved with the neural network (Approximation, Classification, Forecasting, ImageApproximation, ImageClassification).
architectureArchitecture of the neural network({inputs_number, hidden_neurons_number, outputs_number}).

Definition at line 32 of file neural_network.cpp.

◆ NeuralNetwork() [3/7]

NeuralNetwork ( const Vector< size_t > &  new_inputs_dimensions,
const size_t &  new_blocks_number,
const Vector< size_t > &  new_filters_dimensions,
const size_t &  new_outputs_number 
)
explicit

(Convolutional layer) constructor. It creates a neural network object with the given parameters. Note that this method is only valid when our problem presents convolutional layers.

Definition at line 42 of file neural_network.cpp.

◆ NeuralNetwork() [4/7]

NeuralNetwork ( const string &  file_name)
explicit

File constructor. It creates a neural network object by loading its members from an XML-type file. Please be careful with the format of that file, which is specified in the OpenNN manual.

Parameters
file_nameName of neural network file.

Definition at line 56 of file neural_network.cpp.

◆ NeuralNetwork() [5/7]

NeuralNetwork ( const tinyxml2::XMLDocument document)
explicit

XML constructor. It creates a neural network object by loading its members from an XML document.

Parameters
documentTinyXML document containing the neural network data.

Definition at line 66 of file neural_network.cpp.

◆ NeuralNetwork() [6/7]

NeuralNetwork ( const Vector< Layer * > &  new_layers_pointers)
explicit

Layers constructor. It creates a neural network object by It also sets the rest of members to their default values.

Definition at line 76 of file neural_network.cpp.

◆ NeuralNetwork() [7/7]

NeuralNetwork ( const NeuralNetwork other_neural_network)

Copy constructor. It creates a copy of an existing neural network object.

Parameters
other_neural_networkNeural network object to be copied.

Definition at line 88 of file neural_network.cpp.

Member Function Documentation

◆ add_layer()

void add_layer ( Layer layer_pointer)

Add a new layer to the Neural Network model.

Parameters
layerThe layer that will be added.
Todo:
break the software.

Definition at line 113 of file neural_network.cpp.

◆ calculate_directional_inputs()

Matrix< double > calculate_directional_inputs ( const size_t &  direction,
const Vector< double > &  point,
const double &  minimum,
const double &  maximum,
const size_t &  points_number = 101 
) const

Calculates the input data which is necessary to compute the output data from the neural network in some direction.

Parameters
directionInput index(must be between 0 and number of inputs - 1).
pointInput point through the directional input passes.
minimumMinimum value of the input with the above index.
maximumMaximum value of the input with the above index.
points_numberNumber of points in the directional input data set.

Definition at line 1348 of file neural_network.cpp.

◆ calculate_outputs()

Tensor< double > calculate_outputs ( const Tensor< double > &  inputs)

Calculates the outputs vector from the neural network in response to an inputs vector. The activity for that is the following:

  • Check inputs range.
  • Calculate scaled inputs.
  • Calculate forward propagation.
  • Calculate unscaled outputs.
  • Apply boundary condtions.
  • Calculate bounded outputs.
Parameters
inputsSet of inputs to the neural network.

Definition at line 1173 of file neural_network.cpp.

◆ calculate_outputs_histograms() [1/2]

Vector< Histogram > calculate_outputs_histograms ( const size_t &  points_number = 1000,
const size_t &  bins_number = 10 
)

Calculates the histogram of the outputs with random inputs.

Parameters
points_numberNumber of random instances to evaluate the neural network.
bins_numberNumber of bins for the histograms.
Todo:

Definition at line 1378 of file neural_network.cpp.

◆ calculate_outputs_histograms() [2/2]

Vector< Histogram > calculate_outputs_histograms ( const Tensor< double > &  inputs,
const size_t &  bins_number = 10 
)

Calculates the histogram of the outputs with a matrix of given inputs.

Parameters
inputsMatrix of the data to evaluate the neural network.
bins_numberNumber of bins for the histograms.

Definition at line 1436 of file neural_network.cpp.

◆ calculate_parameters_descriptives()

Descriptives calculate_parameters_descriptives ( ) const

Returns a descriptives structure of the parameters vector. That contains the minimum, maximum, mean and standard deviation values of the parameters.

Definition at line 1109 of file neural_network.cpp.

◆ calculate_parameters_histogram()

Histogram calculate_parameters_histogram ( const size_t &  bins_number = 10) const

Returns a histogram structure of the parameters vector. That will be used for looking at the distribution of the parameters.

Parameters
bins_numberNumber of bins in the histogram(10 by default).

Definition at line 1121 of file neural_network.cpp.

◆ check_layer_type()

bool check_layer_type ( const Layer::LayerType  layer_type)

Check if a given layer type can be added to the structure of the neural network. LSTM and Recurrent layers can only be added at the beginning.

Parameters
layer_typeType of new layer to be added.

Definition at line 139 of file neural_network.cpp.

◆ from_XML()

void from_XML ( const tinyxml2::XMLDocument document)
virtual

Deserializes a TinyXML document into this neural network object.

Parameters
documentXML document containing the member data.

Definition at line 1571 of file neural_network.cpp.

◆ get_architecture()

Vector< size_t > get_architecture ( ) const

Returns a vector with the architecture of the neural network. The elements of this vector are as follows;

  • Number of scaling neurons(if there is a scaling layer).
  • Number of principal components neurons(if there is a principal components layer).
  • Multilayer perceptron architecture(if there is a neural network).
  • Number of conditions neurons(if there is a conditions layer).
  • Number of unscaling neurons(if there is an unscaling layer).
  • Number of probabilistic neurons(if there is a probabilistic layer).
  • Number of bounding neurons(if there is a bounding layer).

Definition at line 812 of file neural_network.cpp.

◆ get_display()

const bool & get_display ( ) const

Returns true if messages from this class are to be displayed on the screen, or false if messages from this class are not to be displayed on the screen.

Definition at line 517 of file neural_network.cpp.

◆ get_information()

Matrix< string > get_information ( ) const
Todo:

Definition at line 1477 of file neural_network.cpp.

◆ get_input_index()

size_t get_input_index ( const string &  name) const

Returns the index of the variable with a given name.

Parameters
nameName of the variable to be examined.

Definition at line 303 of file neural_network.cpp.

◆ get_input_name()

string get_input_name ( const size_t &  index) const

Returns a string with the name of the variable used as inputs on a certain index.

Parameters
indexIndex of the variable to be examined.

Definition at line 294 of file neural_network.cpp.

◆ get_layers_number()

size_t get_layers_number ( ) const

Returns the number of layers in the neural network. That includes perceptron, scaling, unscaling, inputs trending, outputs trending, bounding, probabilistic or conditions layers.

Definition at line 1004 of file neural_network.cpp.

◆ get_output_index()

size_t get_output_index ( const string &  name) const

Returns the index of the variable with a given name.

Parameters
nameName of the variable to be examined.

Definition at line 329 of file neural_network.cpp.

◆ get_output_name()

string get_output_name ( const size_t &  index) const

Returns a string with the name of the variable used as outputs on a certain index.

Parameters
indexIndex of the variable to be examined.

Definition at line 320 of file neural_network.cpp.

◆ get_parameters()

Vector< double > get_parameters ( ) const

Returns the values of the parameters in the neural network as a single vector. This contains all the neural network parameters(biases and synaptic weights) and preprocessed independent parameters.

Definition at line 879 of file neural_network.cpp.

◆ get_parameters_number()

size_t get_parameters_number ( ) const

Returns the number of parameters in the neural network The number of parameters is the sum of all the neural network parameters(biases and synaptic weights) and independent parameters.

Definition at line 841 of file neural_network.cpp.

◆ get_trainable_parameters_number()

size_t get_trainable_parameters_number ( ) const

Returns the number of parameters in the neural network The number of parameters is the sum of all the neural network trainable parameters(biases and synaptic weights) and independent parameters.

Definition at line 859 of file neural_network.cpp.

◆ has_bounding_layer()

bool has_bounding_layer ( ) const

Returns true if the neural network object has a bounding layer object inside, and false otherwise.

Definition at line 245 of file neural_network.cpp.

◆ has_long_short_term_memory_layer()

bool has_long_short_term_memory_layer ( ) const

Returns true if the neural network object has a long short term memory layer object inside, and false otherwise.

Definition at line 196 of file neural_network.cpp.

◆ has_principal_components_layer()

bool has_principal_components_layer ( ) const

Returns true if the neural network object has a principal components layer object inside, and false otherwise.

Definition at line 180 of file neural_network.cpp.

◆ has_probabilistic_layer()

bool has_probabilistic_layer ( ) const

Returns true if the neural network object has a probabilistic layer object inside, and false otherwise.

Definition at line 261 of file neural_network.cpp.

◆ has_recurrent_layer()

bool has_recurrent_layer ( ) const

Returns true if the neural network object has a recurrent layer object inside, and false otherwise.

Definition at line 213 of file neural_network.cpp.

◆ has_scaling_layer()

bool has_scaling_layer ( ) const

Returns true if the neural network object has a scaling layer object inside, and false otherwise.

Definition at line 164 of file neural_network.cpp.

◆ has_unscaling_layer()

bool has_unscaling_layer ( ) const

Returns true if the neural network object has an unscaling layer object inside, and false otherwise.

Definition at line 229 of file neural_network.cpp.

◆ is_empty()

bool is_empty ( ) const

Returns true if the neural network object is empty, and false otherwise.

Definition at line 277 of file neural_network.cpp.

◆ load()

void load ( const string &  file_name)
virtual

Loads from a XML file the members for this neural network object. Please mind about the file format, which is specified in the User's Guide.

Parameters
file_nameName of neural network XML file.

Definition at line 1707 of file neural_network.cpp.

◆ load_parameters()

void load_parameters ( const string &  file_name)

Loads the neural network parameters from a data file. The format of this file is just a sequence of numbers.

Parameters
file_nameName of parameters data file.

Definition at line 1732 of file neural_network.cpp.

◆ perturbate_parameters()

void perturbate_parameters ( const double &  perturbation)

Perturbate parameters of the neural network.

Parameters
perturbationMaximum distance of perturbation.

Definition at line 1132 of file neural_network.cpp.

◆ randomize_parameters_normal()

void randomize_parameters_normal ( const double &  mean = 0.0,
const double &  standard_deviation = 1.0 
)

Initializes all the parameters in the newtork(biases and synaptic weiths + independent parameters) at random with values chosen from a normal distribution with a given mean and a given standard deviation.

Parameters
meanMean of normal distribution.
standard_deviationStandard deviation of normal distribution.

Definition at line 1081 of file neural_network.cpp.

◆ randomize_parameters_uniform()

void randomize_parameters_uniform ( const double &  minimum = -1.0,
const double &  maximum = 1.0 
)

Initializes all the parameters in the newtork(biases and synaptic weiths + independent parameters) at random with values comprised between a given minimum and a given maximum values.

Parameters
minimumMinimum initialization value.
maximumMaximum initialization value.

Definition at line 1062 of file neural_network.cpp.

◆ save()

void save ( const string &  file_name) const

Saves to a XML file the members of a neural network object.

Parameters
file_nameName of neural network XML file.

Definition at line 1665 of file neural_network.cpp.

◆ save_data()

void save_data ( const string &  file_name) const

Saves a set of input-output values from the neural network to a data file.

Parameters
file_nameName of data file.
Todo:

Definition at line 3381 of file neural_network.cpp.

◆ save_expression()

void save_expression ( const string &  file_name)

Saves the mathematical expression represented by the neural network to a text file.

Parameters
file_nameName of expression text file.

Definition at line 3309 of file neural_network.cpp.

◆ save_expression_python()

void save_expression_python ( const string &  file_name)

Saves the python function of the expression represented by the neural network to a text file.

Parameters
file_nameName of expression text file.

Definition at line 3333 of file neural_network.cpp.

◆ save_expression_R()

void save_expression_R ( const string &  file_name)

Saves the R function of the expression represented by the neural network to a text file.

Parameters
file_nameName of expression text file.

Definition at line 3357 of file neural_network.cpp.

◆ save_parameters()

void save_parameters ( const string &  file_name) const

Saves to a data file the parameters of a neural network object.

Parameters
file_nameName of parameters data file.

Definition at line 1678 of file neural_network.cpp.

◆ set() [1/4]

void set ( )

This method deletes all the pointers in the neural network. It also sets the rest of members to their default values.

Definition at line 526 of file neural_network.cpp.

◆ set() [2/4]

void set ( const NeuralNetwork other_neural_network)

Sets the members of this neural network object with those from other neural network object.

Parameters
other_neural_networkNeural network object to be copied.

Definition at line 664 of file neural_network.cpp.

◆ set() [3/4]

void set ( const NeuralNetwork::ProjectType &  model_type,
const Vector< size_t > &  architecture 
)

Sets a new neural network with a given neural network architecture. It also sets the rest of members to their default values.

Parameters
architectureArchitecture of the neural network.

Definition at line 542 of file neural_network.cpp.

◆ set() [4/4]

void set ( const string &  file_name)

Sets the neural network members by loading them from a XML file.

Parameters
file_nameNeural network XML file_name.

Definition at line 653 of file neural_network.cpp.

◆ set_display()

void set_display ( const bool &  new_display)

Sets a new display value. If it is set to true messages from this class are to be displayed on the screen; if it is set to false messages from this class are not to be displayed on the screen.

Parameters
new_displayDisplay value.

Definition at line 995 of file neural_network.cpp.

◆ set_inputs_number() [1/2]

void set_inputs_number ( const size_t &  new_inputs_number)

Sets the new inputs number of this neural network object.

Parameters
new_inputs_numberNumber of inputs.

Definition at line 695 of file neural_network.cpp.

◆ set_inputs_number() [2/2]

void set_inputs_number ( const Vector< bool > &  inputs)

Sets the new inputs number of this neural network object.

Parameters
inputsBoolean vector containing the number of inputs.

Definition at line 734 of file neural_network.cpp.

◆ set_parameters()

void set_parameters ( const Vector< double > &  new_parameters)

Sets all the parameters(neural_network_pointer parameters and independent parameters) from a single vector.

Parameters
new_parametersNew set of parameter values.

Definition at line 945 of file neural_network.cpp.

◆ to_XML()

tinyxml2::XMLDocument * to_XML ( ) const
virtual

Serializes the neural network object into a XML document of the TinyXML library. See the OpenNN manual for more information about the format of this element.

Todo:

Definition at line 1487 of file neural_network.cpp.

◆ write_expression()

string write_expression ( ) const

Returns a string with the expression of the function represented by the neural network.

Todo:
write expression for each layer

Definition at line 1761 of file neural_network.cpp.

◆ write_expression_php()

string write_expression_php ( ) const

Returns a string with the php function of the expression represented by the neural network.

Todo:

Definition at line 2688 of file neural_network.cpp.

◆ write_expression_R()

string write_expression_R ( ) const

Returns a string with the R function of the expression represented by the neural network.

Todo:

Definition at line 3006 of file neural_network.cpp.

◆ write_XML()

void write_XML ( tinyxml2::XMLPrinter file_stream) const
virtual

Serializes the neural network object into a XML document of the TinyXML library without keep the DOM tree in memory. See the OpenNN manual for more information about the format of this document.

Definition at line 1546 of file neural_network.cpp.


The documentation for this class was generated from the following files: