inc\classes\help\classes\NeuralNetwork.phpView Source

Show: PublicProtectedPrivateinherited
Table of Contents
Class
Package
Temply-Account\Helpers  

\HelpClasses\NeuralNetwork

Package: Temply-Account\Helpers
Multi-layer Neural Network in PHP

Loosely based on source code by Phil Brierley, that was translated into PHP by 'dspink' in sep 2005

Algorithm was obtained from the excellent introductory book "Artificial Intelligence - a guide to intelligent systems" by Michael Negnevitsky (ISBN 0-201-71159-1)

Example: learning the 'XOR'-function // Create a new neural network with 3 input neurons, // 4 hidden neurons, and 1 output neuron $n = new NeuralNetwork(3, 4, 1); $n->setVerbose(false);

// Add test-data to the network. In this case, // we want the network to learn the 'XOR'-function $n->addTestData(array (-1, -1, 1), array (-1)); $n->addTestData(array (-1, 1, 1), array ( 1)); $n->addTestData(array ( 1, -1, 1), array ( 1)); $n->addTestData(array ( 1, 1, 1), array (-1));

// we try training the network for at most $max times $max = 3;

// train the network in max 1000 epochs, with a max squared error of 0.01 while (!($success = $n->train(1000, 0.01)) && ++$i<$max) { echo "Round $i: No success...


"; }

// print a message if the network was succesfully trained if ($success) { $epochs = $n->getEpoch(); echo "Success in $epochs training rounds!


"; }

// in any case, we print the output of the neural network echo "

End result

"; for ($i = 0; $i < count($n->trainInputs); $i ++) { $output = $n->calculate($n->trainInputs[$i]); echo "
Testset $i; "; echo "expected output = (".implode(", ", $n->trainOutput[$i]).") "; echo "output from neural network = (".implode(", ", $output).")\n"; }

The resulting output could for example be something along the following lines:

Success in 719 training rounds! Testset 0; expected output = (-1) output from neural network = (-0.986415991978) Testset 1; expected output = (1) output from neural network = (0.992121412998) Testset 2; expected output = (1) output from neural network = (0.992469534962) Testset 3; expected output = (-1) output from neural network = (-0.990224120384)

...which indicates the network has learned the task.

Author
E. Akerboom  
Author
{@link http://www.tremani.nl/ Tremani}, {@link http://maps.google.com/maps?f=q&hl=en&q=delft%2C+the+netherlands&ie=UTF8&t=k&om=1&ll=53.014783%2C4.921875&spn=36.882665%2C110.566406&z=4 Delft}, The Netherlands  
License
BSD License  
Version
1.1  

Properties

>VPropertypublicarray $controlDataID = array()

DataID control history

Default valuearray()Details
Type
array
>VPropertypublicarray $controlInputs = array()

Input control history

Default valuearray()Details
Type
array
>VPropertypublicarray $controlOutput = array()

Output control history

Default valuearray()Details
Type
array
>VPropertyprotectedarray $edgeWeight = array()

Edge weights

Default valuearray()Details
Type
array
>VPropertyprotectedinteger $epoch

NN epoch

Details
Type
integer
>VPropertyprotectedfloat $errorControlset

Control error

Details
Type
float
>VPropertyprotectedfloat $errorTrainingset

Error epoch

Details
Type
float
>VPropertyprotectedboolean $isVerbose = true

Is verbose

Default valuetrueDetails
Type
boolean
>VPropertyprotectedarray<mixed,integer> $layerCount = 0

Layers count

Default value0Details
Type
array<mixed,integer>
>VPropertyprotectedarray<mixed,float> $learningRate = array(0.1)

Learning rate

Default valuearray(0.1)Details
Type
array<mixed,float>
>VPropertyprotectedfloat $momentum = 0.8

Momentumn

Default value0.8Details
Type
float
>VPropertyprotectedarray $nodeCount = array()

Amount of nodes

Default valuearray()Details
Type
array
>VPropertyprotectedarray $nodeThreshold = array()

Threshold of nodes

Default valuearray()Details
Type
array
>VPropertyprotectedarray $nodeValue = array()

Values of nodes

Default valuearray()Details
Type
array
>VPropertyprotectedarray $previousWeightCorrection = array()

Fallback position

Default valuearray()Details
Type
array
>VPropertyprotectedboolean $success

Success

Details
Type
boolean
>VPropertypublicarray $trainDataID = array()

Training ID

Default valuearray()Details
Type
array
>VPropertypublicarray $trainInputs = array()

Input story

Default valuearray()Details
Type
array
>VPropertypublicarray $trainOutput = array()

Output story

Default valuearray()Details
Type
array
>VPropertyprotectedboolean $weightsInitialized = false

Is inite

Default valuefalseDetails
Type
boolean

Methods

methodpublic__construct(array $nodeCount) : void

Creates a neural network.

Example: // create a network with 4 input nodes, 10 hidden nodes, and 4 output nodes $n = new NeuralNetwork(4, 10, 4);

// create a network with 4 input nodes, 1 hidden layer with 10 nodes, // another hidden layer with 10 nodes, and 4 output nodes $n = new NeuralNetwork(4, 10, 10, 4);

// alternative syntax $n = new NeuralNetwork(array(4, 10, 10, 4));

Parameters
NameTypeDescription
$nodeCountarray

The number of nodes in the consecutive layers.

methodprotectedactivation(float $value) : float

Implements the standard (default) activation function for backpropagation networks, the 'tanh' activation function.

Parameters
NameTypeDescription
$valuefloat

The preliminary output to apply this function to

Returns
TypeDescription
floatThe final output of the node
methodpublicaddControlData(array $input, array $output, integer $id = null) : void

Add a set of control data to the network.

This set of data is used to prevent 'overlearning' of the network. The network will stop training if the results obtained for the control data are worsening.

The data added as control data is not used for training.

Parameters
NameTypeDescription
$inputarray

An input vector

$outputarray

The corresponding output

$idinteger

(optional) An identifier for this piece of data

methodpublicaddTestData(array $input, array $output, integer $id = null) : void

Add a test vector and its output

Parameters
NameTypeDescription
$inputarray

An input vector

$outputarray

The corresponding output

$idinteger

(optional) An identifier for this piece of data

methodprivatebackpropagate(array $output, array $desired_output) : void

Performs the backpropagation algorithm. This changes the weights and thresholds of the network.

Parameters
NameTypeDescription
$outputarray

The output obtained by the network

$desired_outputarray

The desired output

methodpubliccalculate(array $input) : mixed

Calculate the output of the neural network for a given input vector

Parameters
NameTypeDescription
$inputarray

The vector to calculate

Returns
TypeDescription
mixedThe output of the network
methodpublicclear() : void

Resets the state of the neural network, so it is ready for a new round of training.

methodprotectedderivativeActivation(float $value) : \HelpClasses\$float

Implements the derivative of the activation function. By default, this is the inverse of the 'tanh' activation function: 1.0 - tanh($value)*tanh($value);

Parameters
NameTypeDescription
$valuefloat

'X'

Returns
TypeDescription
\HelpClasses\$float
methodpublicexport() : void

Exports the neural network

Details
Returns
array  
methodprivatefitLine(array $data) : array

Finds the least square fitting line for the given data.

This function is used to determine if the network is overtraining itself. If the line through the controlset's most recent squared errors is going 'up', then it's time to stop training.

Parameters
NameTypeDescription
$dataarray

The points to fit a line to. The keys of this array represent the 'x'-value of the point, the corresponding value is the 'y'-value of the point.

Returns
TypeDescription
arrayAn array containing, respectively, the slope and the offset of the fitted line.
methodpublicgetControlDataIDs() : array

Returns the identifiers of the control data used during the training of the network (if available)

Returns
TypeDescription
arrayAn array of identifiers
methodpublicgetEpoch() : integer

Gets the number of epochs the network needed for training.

Returns
TypeDescription
integerThe number of epochs.
methodpublicgetErrorControlSet() : float

Gets the squared error between the desired output and the obtained output of the control data.

Returns
TypeDescription
floatThe squared error of the control data
methodpublicgetErrorTrainingSet() : float

Gets the squared error between the desired output and the obtained output of the training data.

Returns
TypeDescription
floatThe squared error of the training data
methodpublicgetLearningRate(integer $layer) : float

Gets the learning rate for a specific layer

Parameters
NameTypeDescription
$layerinteger

The layer to obtain the learning rate for

Returns
TypeDescription
floatThe learning rate for that layer
methodpublicgetMomentum() : float

Gets the momentum.

Returns
TypeDescription
floatThe momentum
methodprivategetRandomWeight( $layer) : float

Gets a random weight between [-0.25 .

. 0.25]. Used to initialize the network.

Parameters
NameTypeDescription
$layer
Returns
TypeDescription
floatA random weight
methodpublicgetTestDataIDs() : array

Returns the identifiers of the data used to train the network (if available)

Returns
TypeDescription
arrayAn array of identifiers
methodpublicgetTrainingSuccessful() : boolean

Determines if the training was successful.

Returns
TypeDescription
boolean'true' if the training was successful, 'false' otherwise
methodpublicimport(array $nn_array) : void

Import a neural network

Parameters
NameTypeDescription
$nn_arrayarray

An array of the neural network parameters

methodprivateinitWeights() : void

Randomise the weights in the neural network

methodpublicisVerbose() : boolean

Returns whether or not the network displays status and error messages.

Returns
TypeDescription
boolean'true' if status and error messages are displayed, 'false' otherwise
methodpublicload(string $filename) : boolean

Loads a neural network from a file saved by the 'save()' function. Clears the training and control data added so far.

Parameters
NameTypeDescription
$filenamestring

The filename to load the network from

Returns
TypeDescription
boolean'true' on success, 'false' otherwise
methodpublicsave(string $filename) : boolean

Saves a neural network to a file

Parameters
NameTypeDescription
$filenamestring

The filename to save the neural network to

Returns
TypeDescription
boolean'true' on success, 'false' otherwise
methodprivatesetEpoch(integer $epoch) : void

After training, this function is used to store the number of epochs the network needed for training the network. An epoch is defined as the number of times the complete trainingset is used for training.

Parameters
NameTypeDescription
$epochinteger
methodprivatesetErrorControlSet(float $error) : void

After training, this function is used to store the squared error between the desired output and the obtained output of the control data.

Parameters
NameTypeDescription
$errorfloat

The squared error of the control data

methodprivatesetErrorTrainingSet(float $error) : void

After training, this function is used to store the squared error between the desired output and the obtained output of the training data.

Parameters
NameTypeDescription
$errorfloat

The squared error of the training data

methodpublicsetLearningRate(array $learningRate) : void

Sets the learning rate between the different layers.

Parameters
NameTypeDescription
$learningRatearray

An array containing the learning rates [range 0.0 - 1.0]. The size of this array is 'layerCount - 1'. You might also provide a single number. If that is the case, then this will be the learning rate for the whole network.

methodpublicsetMomentum(float $momentum) : void

Sets the 'momentum' for the learning algorithm. The momentum should accelerate the learning process and help avoid local minima.

Parameters
NameTypeDescription
$momentumfloat

The momentum. Must be between 0.0 and 1.0; Usually between 0.5 and 0.9

methodprivatesetTrainingSuccessful(boolean $success) : void

After training, this function is used to store whether or not the training was successful.

Parameters
NameTypeDescription
$successboolean

'true' if the training was successful, 'false' otherwise

methodpublicsetVerbose(boolean $isVerbose) : void

Determines if the neural network displays status and error messages. By default, it does.

Parameters
NameTypeDescription
$isVerboseboolean

'true' if you want to display status and error messages, 'false' if you don't

methodpublicshowWeights(boolean $force = false) : void

Shows the current weights and thresholds

Parameters
NameTypeDescription
$forceboolean

Force the output, even if the network is {@link setVerbose() not verbose}.

methodprivatesquaredError(array $input, array $desired_output) : float

Calculate the root-mean-squared error of the output, given the desired output.

Parameters
NameTypeDescription
$inputarray

The input to test

$desired_outputarray

The desired output

Returns
TypeDescription
floatThe root-mean-squared error of the output compared to the desired output
methodprivatesquaredErrorControlSet() : float

Calculate the root-mean-squared error of the output, given the controldata.

Returns
TypeDescription
floatThe root-mean-squared error of the output
methodprivatesquaredErrorEpoch() : float

Calculate the root-mean-squared error of the output, given the trainingdata.

Returns
TypeDescription
floatThe root-mean-squared error of the output
methodpublictrain(integer $maxEpochs = 500, float $maxError = 0.01) : boolean

Start the training process

Parameters
NameTypeDescription
$maxEpochsinteger

The maximum number of epochs

$maxErrorfloat

The maximum squared error in the training data

Returns
TypeDescription
boolean'true' if the training was successful, 'false' otherwise
Documentation was generated by phpDocumentor v2.9.0.