Multilayer perceptron

From Artificial Neural Network for PHP

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:

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

Sigmoid activation function

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

Hyperbolic tangent activation function

o = tanh(s)

using output range between -1 and 1, or

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

using output range between 0 and 1.

s cumulative input
w weight of input
i value of input
n number of inputs
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:

E={1\over2} \sum^{n}_{i=1} (t_{i}-o_{i})^{2}

E network error
n count of input patterns
t_{i} desired output
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:

\Delta w_{ij}= -\alpha \cdot {\partial E \over \partial w_{ij}} = \alpha \cdot \delta_{j} \cdot x_{i}

E network error
\Delta w_{ij} delta value w_{ij} of neuron connection i to j
\alpha learning rate
\delta_{j} the error of neuron j
x_{i} input of neuron i
t_{j} desired output of output neuron j
o_{j} real output of output neuron j.

Programming solution of backpropagation

In this PHP implementation of multilayer perceptron the following algorithm is used for weight changes in hidden layers:

s_{kl} = \sum^{n}_{l=1} w_{k} \cdot \Delta w_{l} \cdot \beta

\Delta w_{k} = o_{k} \cdot (1 - o_{k}) \cdot s_{kl}

w_{mk} = w_{mk} + \alpha \cdot i_{m} \cdot \Delta w_{k}

\alpha learning rate
\beta momentum
k neuron k
l neuron l
m weight m
i input
o output
n count of neurons

To avoid overfitting of neural network 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 (\alpha) and momentum (\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 \alpha and 0.95 for \beta. Theses factors can be change by runtime.

Binary and linear input

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

0 : False

1 : True

Using linear input values normalization is needed:

i = \frac{f - f_{min}}{f_{max} - f_{min}}

i input value for neural network
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:

o >= 0.5 : True

o < 0.5 : False

o output value

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

f = o \cdot (f_{max} - f_{min}) + f_{min}

f real world value
o real output value of neural network

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

o = \frac{f - f_{min}}{f_{max} - f_{min}}

o desired output value for neural network
f real world value

This PHP implementation is supporting output normalization.