## Definition

An MLP (Multilayer perceptron) is a feedforward neural network which consists of many perceptrons joined in many layers. The most basic MLP has 3 layers: one input layer, one output layer and one hidden layer, but it may have as many hidden layers as necessary. Each individual layer may contain a different number of neurons.

Feedforward means that nodes have a direct connection such that nodes located in between are fed by the outputs of previous nodes and so on. This is very simple to program because it is basically as the perceptron: multiply weights (including bias nodes) by the input vector, give the result to the activation function, and the output will be the input of the next layer until the output layer is reached.

In the entry about the perceptron I used the step function as the activation function, but in MLP a variety of them can be used. Most of them are __sigmoid__ (mathematical functions having an “S” shape) such as:

The difference between these two sigmoids is that the hyperbolic tangent goes from -1 to 1 whereas the other goes from 0 to 1.

Sometimes __linear__ functions are also needed for cases when neural networks are not used for classification but for extrapolation and any kind of prediction.

If the MLP misclassifies an input, it is necessary to back-propagate the error rate through the neural network in order to update old weights. Gradient descent through derivatives is used to correct the weights as much as possible. Derivatives show how the error varies depending on the weights to later apply the proper correction.

In perceptrons, the error function was basically (desired output minus system’s output) but in MLP the **squared error function** will be used instead :

- It has a sum because each element has its own error and it is necessary to sum them up.
- It is squared because convex functions’ local and global minima are the same.
- It has a to make it simpler when obtaining the derivative.

The following mathematical explanation regarding the formulas for the learning process is based on the 2x3x1 neural network depicted in the first picture of the entry, but before that, some terms need to be introduced to avoid misunderstandings:

Input matrix.

Weights’ matrix that connects the input and hidden layers.

Weights’ matrix that connects the hidden and output layers.

Desired output.

Learning rate.

Content of each node in the hidden layer (before activation).

Content of each node in the hidden layer (after activation).

Content of each node in the output layer (before activation).

MLP output.

Squared error function.

First, let us calculate the update formula for . For this, it is necessary to calculate the derivative of the error with respect to to see how much the error varies depending on .

By the sum rule in differentiation we can take the sum off the fraction, and from now on it is no longer needed since at the end we can sum each sample individually, so let us derive it.

The derivative of y with respect to is 0 since y does not depend on .

By applying the chain rule:

Which finally:

In case of the procedure is very similar:

__Learning process__ (update rules):

## Results

The MLP I developed was able to correctly classify samples from its training set from 3 different classes. I have not written a word about overfitting because I will probably do that in a different entry with a good solution to overcome it, but this algorithm probably overfits and cannot classify other samples with the same good result.

Finally, I recorded the evolution of the error with two different learning rate to show that if is too big, it can escape from the minimum with an unknown result. In this case, it resulted absolutely good, but it is not always the case.

Left: . Right:

The code is provided in the Source code section.

## References

I strongly recommend the set of videos to which this one belongs to.