# Multilayer perceptron

## General

A multilayer perceptron is a feedforward artificial neural network. This means the signal inside the neural network flows from input layer passing hidden layers to output layer. While training the error correction of neural weights are done in the opposite direction. This is done by the backpropagation algorithm.

## Activation

At first a cumulative input is calculated by the following equation:

${\displaystyle s=\sum _{k=1}^{n}i_{k}\cdot w_{k}}$

Considering the BIAS value the equation is:

${\displaystyle s=(\sum _{k=1}^{n}i_{k}\cdot w_{k})+BIAS\cdot w_{k}}$
${\displaystyle BIAS=1}$

### Sigmoid activation function

${\displaystyle o={\rm {{sig}(s)={\frac {1}{1+{\rm {e^{-s}}}}}}}}$

### Hyperbolic tangent activation function

${\displaystyle o=tanh(s)}$

using output range between -1 and 1, or

${\displaystyle o={\frac {tanh(s)+1}{2}}}$

using output range between 0 and 1.

${\displaystyle s}$ cumulative input
${\displaystyle w}$ weight of input
${\displaystyle i}$ value of input
${\displaystyle n}$ number of inputs
${\displaystyle k}$ number of neuron

## Error of neural network

If the neural network is initialized by random weights it has of course not the expected output. Therefore training is necessary. While supervised training known inputs and their corresponded output values are presented to the network. So it is possible to compare the real output with the desired output. The error is described as the following algorithm:

${\displaystyle E={1 \over 2}\sum _{i=1}^{n}(t_{i}-o_{i})^{2}}$
${\displaystyle E}$ network error
${\displaystyle n}$ count of input patterns
${\displaystyle t_{i}}$ desired output
${\displaystyle o_{i}}$ calculated output

## Backpropagation

The learning algorithm of a single layer perceptron is easy compared to a multilayer perceptron. The reason is that just the output layer is directly connected to the output, but not the hidden layers. Therefore the calculation of the right weights of the hidden layers is difficult mathematically. To get the right delta value for changing the weights of hidden neuron is described in the following equation:

${\displaystyle \Delta w_{ij}=-\alpha \cdot {\partial E \over \partial w_{ij}}=\alpha \cdot \delta _{j}\cdot x_{i}}$
${\displaystyle E}$ network error
${\displaystyle \Delta w_{ij}}$ delta value ${\displaystyle w_{ij}}$ of neuron connection ${\displaystyle i}$ to ${\displaystyle j}$
${\displaystyle \alpha }$ learning rate
${\displaystyle \delta _{j}}$ the error of neuron ${\displaystyle j}$
${\displaystyle x_{i}}$ input of neuron ${\displaystyle i}$
${\displaystyle t_{j}}$ desired output of output neuron ${\displaystyle j}$
${\displaystyle o_{j}}$ real output of output neuron ${\displaystyle j}$.

## Programming solution of backpropagation

In this PHP implementation of multilayer perceptron the following algorithm is used for weight changes in hidden layers and output layer.

### Weight change of output layer

${\displaystyle \Delta w_{k}=o_{k}\cdot (a_{k}-o_{k})\cdot (1-o_{k})}$
${\displaystyle w_{mk}=w_{mk}+\alpha \cdot i_{m}\cdot \Delta w_{k}}$
${\displaystyle k}$ neuron k
${\displaystyle o}$ output
${\displaystyle i}$ input
${\displaystyle a}$ desired output
${\displaystyle w}$ weight
${\displaystyle m}$ weight m

### Weight change of hidden layers

${\displaystyle s_{kl}=\sum _{l=1}^{n}w_{k}\cdot \Delta w_{l}\cdot \beta }$
${\displaystyle \Delta w_{k}=o_{k}\cdot (1-o_{k})\cdot s_{kl}}$
${\displaystyle w_{mk}=w_{mk}+\alpha \cdot i_{m}\cdot \Delta w_{k}}$
${\displaystyle \alpha }$ learning rate
${\displaystyle \beta }$ momentum
${\displaystyle k}$ neuron k
${\displaystyle l}$ neuron l
${\displaystyle w}$ weight
${\displaystyle m}$ weight m
${\displaystyle i}$ input
${\displaystyle o}$ output
${\displaystyle n}$ count of neurons

### Momentum

To avoid oscillating weight changes the momentum factor ${\displaystyle \beta }$ is defined. Therefore the calculated weight change would not be the same always.

### Overfitting

To avoid overfitting of neural networks in this PHP implementation the training procedure is finished if real output value has a fault tolerance of 1 per cent of desired output value.

### Choosing learning rate and momentum

The proper choosing of learning rate (${\displaystyle \alpha }$) and momentum (${\displaystyle \beta }$) is done by experience. Both values have a range between 0 and 1. This PHP implementation uses a default value of 0.5 for ${\displaystyle \alpha }$ and 0.95 for ${\displaystyle \beta }$. ${\displaystyle \alpha }$ and ${\displaystyle \beta }$ cannot be zero. Otherwise no weight change will be happen and the network would never reach an errorless level. Theses factors can be changed by runtime.

### Dynamic learning rate

To convergent the network faster to its lowest error, use of dynamic learning rate may be a good way.

${\displaystyle w_{mk}=w_{mk}+\alpha \cdot \gamma \cdot i_{m}\cdot \Delta w_{k}}$
${\displaystyle \alpha \cdot \gamma =[0.5..0.9]}$
${\displaystyle \alpha }$ learning rate
${\displaystyle \beta }$ momentum
${\displaystyle \gamma }$ dynamic learning rate factor
${\displaystyle k}$ neuron k
${\displaystyle w}$ weight
${\displaystyle m}$ weight m
${\displaystyle i}$ input

### Weight decay

Normally weights grow up to large numbers. But in fact this is not necessary. The weight decay algorithm tries to avoid large weights. Through large weights maybe the network convergence takes too long.

The weight change algorithm without weight decay is the following:

${\displaystyle \Delta w_{i}(t)=\alpha \cdot {\frac {\partial E(t)}{\partial w_{i}(t)}}}$

By subtracting a value the weight change will be reduce in relation to the last weight.

${\displaystyle \Delta w_{i}(t)=\alpha \cdot {\frac {\partial E(t)}{\partial w_{i}(t)}}-\lambda \cdot w_{i}(t-1)}$
${\displaystyle \lambda =[0.03..0.05]}$
${\displaystyle w}$ weight
${\displaystyle i}$ neuron
${\displaystyle E}$ error function
${\displaystyle t}$ time (training step)
${\displaystyle \alpha }$ learning rate
${\displaystyle \lambda }$ weight decay factor

### Quick propagation algorithm

The Quickprop algorithm calculates the weight change by using the quadratic function ${\displaystyle f(x)=x^{2}}$. Two different error values of two different weights are the two points of a secant. Relating this secant to a quadratic function it is possible to calculate its minimum ${\displaystyle f'(x)=0}$. The x-coordinate of the minimum point is the new weight value.

${\displaystyle S(t)={\frac {\partial E}{\partial w_{i}(t)}}}$
${\displaystyle \Delta w_{i}(t)=\alpha \cdot {\frac {\partial E}{\partial w_{i}(t)}}}$ (normal backpropagation)
${\displaystyle {\frac {\Delta w_{i}(t)}{\alpha }}={\frac {\partial E}{\partial w_{i}(t)}}}$
${\displaystyle S(t)={\frac {\partial E}{\partial w_{i}(t)}}={\frac {\Delta w_{i}(t)}{\alpha }}}$
${\displaystyle \Delta w_{i}(t)={\frac {S(t)}{S(t-1)-S(t)}}\cdot \Delta w_{i}(t-1)}$ (quick propagation)
${\displaystyle w}$ weight
${\displaystyle i}$ neuron
${\displaystyle E}$ error function
${\displaystyle t}$ time (training step)
${\displaystyle \alpha }$ learning rate

To avoid too big changes the maximum weight change is limited by the following equation:

${\displaystyle \Delta w_{i}(t)\leq \mu \cdot \Delta w_{i}(t-1)}$
${\displaystyle \mu =[1.75..2.25]}$
${\displaystyle w}$ weight
${\displaystyle i}$ neuron
${\displaystyle t}$ time (training step)
${\displaystyle \mu }$ maximal weight change factor

### RProp (Resilient Propagation)

The RProp algorithm just refers to the direction of the gradient.

${\displaystyle \Delta w_{ij}(t)={\begin{cases}-\Delta p_{ij},&{\text{if }}{\frac {\partial E}{\partial w_{ij}}}>0\\+\Delta p_{ij},&{\text{if }}{\frac {\partial E}{\partial w_{ij}}}<0\\0,&{\text{if }}{\frac {\partial E}{\partial w_{ij}}}=0\end{cases}}}$

${\displaystyle \Delta p_{ij}(t)={\begin{cases}\alpha ^{+}\cdot \Delta w_{ij}(t-1),&{\text{if }}{\frac {\partial E}{\partial w_{ij}}}(t-1)\cdot {\frac {\partial E}{\partial w_{ij}}}(t)>0\\\alpha ^{-}\cdot \Delta w_{ij}(t-1),&{\text{if }}{\frac {\partial E}{\partial w_{ij}}}(t-1)\cdot {\frac {\partial E}{\partial w_{ij}}}(t)<0\\\Delta w_{ij}(t-1),&{\text{if }}{\frac {\partial E}{\partial w_{ij}}}(t-1)\cdot {\frac {\partial E}{\partial w_{ij}}}(t)=0\end{cases}}}$

${\displaystyle \alpha }$ learning rate
${\displaystyle w}$ weight
${\displaystyle p}$ weight change
${\displaystyle \alpha ^{+}=1.2}$
${\displaystyle \alpha ^{-}=0.5}$
${\displaystyle \Delta w(0)=0.5}$
${\displaystyle \Delta w(t)_{max}=50}$
${\displaystyle \Delta w(t)_{min}=0}$

### RProp+

The RProp+ algorithm reduce the previous weight change from the last weight change if the mathematical sign of the gradient changes.

${\displaystyle \Delta w_{ij}(t)={\begin{cases}\alpha ^{+}\cdot \Delta w_{ij}(t-1),&{\text{if }}{\frac {\partial E}{\partial w_{ij}}}(t-1)\cdot {\frac {\partial E}{\partial w_{ij}}}(t)>0\\\Delta w_{ij}(t-1)-\Delta w_{ij}(t-2),&{\text{if }}{\frac {\partial E}{\partial w_{ij}}}(t-1)\cdot {\frac {\partial E}{\partial w_{ij}}}(t)<0\\\Delta w_{ij}(t-1),&{\text{if }}{\frac {\partial E}{\partial w_{ij}}}(t-1)\cdot {\frac {\partial E}{\partial w_{ij}}}(t)=0\end{cases}}}$

### iRProp+

The iRProp+ is a improve RProp+ algorithm with a little change. Before reducing the previous weight change from the last weight change, the network error will be calculated and compared. If the network error increases from ${\displaystyle E(t-2)}$ to ${\displaystyle E(t-1)}$, then the procedure of RProp+ will be done. Otherwise no change will be done, because if ${\displaystyle E(t-1)}$ has a lower value than ${\displaystyle E(t-2)}$ the weight change seems to be correct to convergent the neural network.

${\displaystyle \Delta w_{ij}(t)={\begin{cases}\alpha ^{+}\cdot \Delta w_{ij}(t-1),&{\text{if }}{\frac {\partial E}{\partial w_{ij}}}(t-1)\cdot {\frac {\partial E}{\partial w_{ij}}}(t)>0\\\Delta w_{ij}(t-1)-\Delta w_{ij}(t-2),&{\text{if }}{\frac {\partial E}{\partial w_{ij}}}(t-1)\cdot {\frac {\partial E}{\partial w_{ij}}}(t)<0{\text{ and if }}E(t)>E(t-1)\\\Delta w_{ij}(t-1),&{\text{if }}{\frac {\partial E}{\partial w_{ij}}}(t-1)\cdot {\frac {\partial E}{\partial w_{ij}}}(t)=0\end{cases}}}$

## Binary and linear input

If binary input is used easily the input value is 0 for false and 1 for true.

${\displaystyle 0:False}$
${\displaystyle 1:True}$

Using linear input values normalization is needed:

${\displaystyle i={\frac {f-f_{min}}{f_{max}-f_{min}}}}$
${\displaystyle i}$ input value for neural network
${\displaystyle f}$ real world value

This PHP implementation is supporting input normalization.

## Binary and linear output

The interpretation of output values just makes sense for the output layer. The interpretation is depending on the use of the neural network. If the network is used for classification, so binary output is used. Binary has two states: True or false. The network will produce always linear output values. Therefore these values has to be converted to binary values:

${\displaystyle o<0.5:False}$
${\displaystyle o>=0.5:True}$
${\displaystyle o}$ output value

If using linear output the output values have to be normalized to a real value the network is trained for:

${\displaystyle f=o\cdot (f_{max}-f_{min})+f_{min}}$
${\displaystyle f}$ real world value
${\displaystyle o}$ real output value of neural network

The same normalization equation for input values is used for output values while training the network.

${\displaystyle o={\frac {f-f_{min}}{f_{max}-f_{min}}}}$
${\displaystyle o}$ desired output value for neural network
${\displaystyle f}$ real world value

This PHP implementation is supporting output normalization.