Neural Networks
ML implements feed-forward artificial neural networks or, more particularly, multi-layer perceptrons (MLP), the most commonly used type of neural networks. MLP consists of the input layer, output layer, and one or more hidden layers. Each layer of MLP includes one or more neurons directionally linked with the neurons from the previous and the next layer. The example below represents a 3-layer perceptron with three inputs, two outputs, and the hidden layer including five neurons:

All the neurons in MLP are similar. Each of them has several input links (it takes the output values from several neurons in the previous layer as input) and several output links (it passes the response to several neurons in the next layer). The values retrieved from the previous layer are summed up with certain weights, individual for each neuron, plus the bias term. The sum is transformed using the activation function f that may be also different for different neurons.

In other words, given the outputs x_j of the layer n , the outputs y_i of the layer n+1 are computed as:
u_i = \sum _j (w^{n+1}_{i,j}*x_j) + w^{n+1}_{i,bias}
y_i = f(u_i)
Different activation functions may be used. ML implements three standard functions:
-
Identity function (
CvANN_MLP::IDENTITY
): f(x)=x -
Symmetrical sigmoid (
CvANN_MLP::SIGMOID_SYM
): f(x)=\beta*(1-e^{-\alpha x})/(1+e^{-\alpha x} ), which is the default choice for MLP. The standard sigmoid with \beta =1, \alpha =1 is shown below: -
Gaussian function (
CvANN_MLP::GAUSSIAN
): f(x)=\beta e^{-\alpha x*x} , which is not completely supported at the moment.
In ML, all the neurons have the same activation functions, with the same free parameters ( \alpha, \beta ) that are specified by user and are not altered by the training algorithms.
So, the whole trained network works as follows:
- Take the feature vector as input. The vector size is equal to the size of the input layer.
- Pass values as input to the first hidden layer.
- Compute outputs of the hidden layer using the weights and the activation functions.
- Pass outputs further downstream until you compute the output layer.
So, to compute the network, you need to know all the weights w^{n+1)}_{i,j} . The weights are computed by the training algorithm. The algorithm takes a training set, multiple input vectors with the corresponding output vectors, and iteratively adjusts the weights to enable the network to give the desired response to the provided input vectors.
The larger the network size (the number of hidden layers and their sizes) is, the more the potential network flexibility is. The error on the training set could be made arbitrarily small. But at the same time the learned network also "learns" the noise present in the training set, so the error on the test set usually starts increasing after the network size reaches a limit. Besides, the larger networks are trained much longer than the smaller ones, so it is reasonable to pre-process the data, using :ocv:funcx:`PCA::operator()` or similar technique, and train a smaller network on only essential features.
Another MLP feature is an inability to handle categorical
data as is. However, there is a workaround. If a certain feature in the
input or output (in case of n
-class classifier for
n>2
) layer is categorical and can take
M>2
different values, it makes sense to represent it as a binary tuple of M
elements, where the i
-th element is 1 if and only if the
feature is equal to the i
-th value out of M
possible. It
increases the size of the input/output layer but speeds up the
training algorithm convergence and at the same time enables "fuzzy" values
of such variables, that is, a tuple of probabilities instead of a fixed value.
ML implements two algorithms for training MLP's. The first algorithm is a classical random sequential back-propagation algorithm. The second (default) one is a batch RPROP algorithm.
[BackPropWikipedia] | http://en.wikipedia.org/wiki/Backpropagation. Wikipedia article about the back-propagation algorithm. |
[LeCun98] |
|
[RPROP93] |
|
CvANN_MLP_TrainParams
CvANN_MLP_TrainParams::CvANN_MLP_TrainParams
The constructors.
By default the RPROP algorithm is used:
CvANN_MLP_TrainParams::CvANN_MLP_TrainParams()
{
term_crit = cvTermCriteria( CV_TERMCRIT_ITER + CV_TERMCRIT_EPS, 1000, 0.01 );
train_method = RPROP;
bp_dw_scale = bp_moment_scale = 0.1;
rp_dw0 = 0.1; rp_dw_plus = 1.2; rp_dw_minus = 0.5;
rp_dw_min = FLT_EPSILON; rp_dw_max = 50.;
}
CvANN_MLP
MLP model.
Unlike many other models in ML that are constructed and trained at once, in the MLP model these steps are separated. First, a network with the specified topology is created using the non-default constructor or the method :ocv:func:`CvANN_MLP::create`. All the weights are set to zeros. Then, the network is trained using a set of input and output vectors. The training procedure can be repeated more than once, that is, the weights can be adjusted based on the new training data.
CvANN_MLP::CvANN_MLP
The constructors.
The advanced constructor allows to create MLP with the specified topology. See :ocv:func:`CvANN_MLP::create` for details.
CvANN_MLP::create
Constructs MLP with the specified topology.
The method creates an MLP network with the specified topology and assigns the same activation function to all the neurons.
CvANN_MLP::train
Trains/updates MLP.
This method applies the specified training algorithm to computing/adjusting the network weights. It returns the number of done iterations.
The RPROP training algorithm is parallelized with the TBB library.
CvANN_MLP::predict
Predicts responses for input samples.
The method returns a dummy value which should be ignored.
CvANN_MLP::get_layer_count
Returns the number of layers in the MLP.
CvANN_MLP::get_layer_sizes
Returns numbers of neurons in each layer of the MLP.
The method returns the integer vector specifying the number of neurons in each layer including the input and output layers of the MLP.
CvANN_MLP::get_weights
Returns neurons weights of the particular layer.