Neural network class

A neural network can be defined as a biologically inspired computational model consisting of a network architecture made of artificial neurons. This structure contains a set of parameters, which can be adjusted to perform specific tasks.

In this tutorial, we will use the iris data set to show how to apply some of the main methods in the NeuralNetwork class. Before continuing, it is advisable to read the DataSet class.

As well as the DataSet class, the NeuralNetwork class implements a wide variety of constructors. In this example, we are going to use the following:

Neural Network neural_network(NeuralNetwork::ModelType::Classification, {4}, {6}, {3});

The first argument indicates the model type. There are six different types: Approximation, Classification, Forecasting, AutoAssociation, TextClassification and ImageClassification. For classification problems, the default architecture consists of a scaling layer, two perceptron layers, and a probabilistic layer.

The second argument represents the number of inputs, the third is the number of neurons in the perceptron layer, and the last one is the number of neurons in the probabilistic layer. If more numbers are included in the second argument, additional perceptron layers will be added to the neural network.

It is also possible to set the input and target variables names:

const vector <string> inputs_names = neural_network.get_input_names();
neural_network.set_inputs_names(inputs_names);
const vector <string> outputs_names = neural_network.get_outputs_names();
neural_network.set_outputs_names(outputs_names);

Once the neural network has been designed, we can get all the information about it as follows:

Index inputs_number = neural_network.get_inputs_number();
Index outputs_number = data_set.get_outputs_number();

The neural network will receive the data scaled in the training phase, setting the scaling method as NoScaling. When the network is trained, in order to check the selection error, selection data will be passed as unscaled so that the ScalingData method can scale it using the statistics vector.

ScalingLayer2D* scaling_layer = static_cast<ScalingLayer2D*>(neural_network.get_first(Layer::Type::Scaling2D));
scaling_layer->set_descriptives(inputs_descriptives);
scaling_layer->set_scalers(Scaler::NoScaling);

The probabilistic layer takes the outputs to produce new outputs whose elements can be interpreted as probabilities. This way, the probabilistic outputs will always fall in the range [0,1], and the sum will always be 1. We can change the neurons’ activation function as follows:

ProbabilisticLayer* probabilistic_layer = static_cast<ProbabilisticLayer*>(neural_network.get_first(Layer::Type::Probabilistic));
probabilistic_layer->set_activation_function(ProbabilisticLayer::ActivationFunction::Softmax);

For more information on the NeuralNetwork class visit NeuralNetwork Class Reference.