inc\classes\help\classes\NeuralNetwork.php
- Package
- Temply-Account\Helpers
\HelpClasses\NeuralNetwork
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

array<mixed,float> $learningRate = array(0.1)Learning rate
array(0.1)Details- Type
- array<mixed,float>
Methods

__construct(array $nodeCount) : voidCreates 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));
| Name | Type | Description |
|---|---|---|
| $nodeCount | array | The number of nodes in the consecutive layers. |

activation(float $value) : floatImplements the standard (default) activation function for backpropagation networks, the 'tanh' activation function.
| Name | Type | Description |
|---|---|---|
| $value | float | The preliminary output to apply this function to |
| Type | Description |
|---|---|
| float | The final output of the node |

addControlData(array $input, array $output, integer $id = null) : voidAdd 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.
| Name | Type | Description |
|---|---|---|
| $input | array | An input vector |
| $output | array | The corresponding output |
| $id | integer | (optional) An identifier for this piece of data |

addTestData(array $input, array $output, integer $id = null) : voidAdd a test vector and its output
| Name | Type | Description |
|---|---|---|
| $input | array | An input vector |
| $output | array | The corresponding output |
| $id | integer | (optional) An identifier for this piece of data |

backpropagate(array $output, array $desired_output) : voidPerforms the backpropagation algorithm. This changes the weights and thresholds of the network.
| Name | Type | Description |
|---|---|---|
| $output | array | The output obtained by the network |
| $desired_output | array | The desired output |

calculate(array $input) : mixedCalculate the output of the neural network for a given input vector
| Name | Type | Description |
|---|---|---|
| $input | array | The vector to calculate |
| Type | Description |
|---|---|
| mixed | The output of the network |

derivativeActivation(float $value) : \HelpClasses\$floatImplements the derivative of the activation function. By default, this is the inverse of the 'tanh' activation function: 1.0 - tanh($value)*tanh($value);
| Name | Type | Description |
|---|---|---|
| $value | float | 'X' |
| Type | Description |
|---|---|
| \HelpClasses\$float |

fitLine(array $data) : arrayFinds 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.
| Name | Type | Description |
|---|---|---|
| $data | array | 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. |
| Type | Description |
|---|---|
| array | An array containing, respectively, the slope and the offset of the fitted line. |

getControlDataIDs() : arrayReturns the identifiers of the control data used during the training of the network (if available)
| Type | Description |
|---|---|
| array | An array of identifiers |

getEpoch() : integerGets the number of epochs the network needed for training.
| Type | Description |
|---|---|
| integer | The number of epochs. |

getErrorControlSet() : floatGets the squared error between the desired output and the obtained output of the control data.
| Type | Description |
|---|---|
| float | The squared error of the control data |

getErrorTrainingSet() : floatGets the squared error between the desired output and the obtained output of the training data.
| Type | Description |
|---|---|
| float | The squared error of the training data |

getLearningRate(integer $layer) : floatGets the learning rate for a specific layer
| Name | Type | Description |
|---|---|---|
| $layer | integer | The layer to obtain the learning rate for |
| Type | Description |
|---|---|
| float | The learning rate for that layer |

getRandomWeight( $layer) : floatGets a random weight between [-0.25 .
. 0.25]. Used to initialize the network.
| Name | Type | Description |
|---|---|---|
| $layer |
| Type | Description |
|---|---|
| float | A random weight |

getTestDataIDs() : arrayReturns the identifiers of the data used to train the network (if available)
| Type | Description |
|---|---|
| array | An array of identifiers |

getTrainingSuccessful() : booleanDetermines if the training was successful.
| Type | Description |
|---|---|
| boolean | 'true' if the training was successful, 'false' otherwise |

import(array $nn_array) : voidImport a neural network
| Name | Type | Description |
|---|---|---|
| $nn_array | array | An array of the neural network parameters |

isVerbose() : booleanReturns whether or not the network displays status and error messages.
| Type | Description |
|---|---|
| boolean | 'true' if status and error messages are displayed, 'false' otherwise |

load(string $filename) : booleanLoads a neural network from a file saved by the 'save()' function. Clears the training and control data added so far.
| Name | Type | Description |
|---|---|---|
| $filename | string | The filename to load the network from |
| Type | Description |
|---|---|
| boolean | 'true' on success, 'false' otherwise |

save(string $filename) : booleanSaves a neural network to a file
| Name | Type | Description |
|---|---|---|
| $filename | string | The filename to save the neural network to |
| Type | Description |
|---|---|
| boolean | 'true' on success, 'false' otherwise |

setEpoch(integer $epoch) : voidAfter 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.
| Name | Type | Description |
|---|---|---|
| $epoch | integer |

setErrorControlSet(float $error) : voidAfter training, this function is used to store the squared error between the desired output and the obtained output of the control data.
| Name | Type | Description |
|---|---|---|
| $error | float | The squared error of the control data |

setErrorTrainingSet(float $error) : voidAfter training, this function is used to store the squared error between the desired output and the obtained output of the training data.
| Name | Type | Description |
|---|---|---|
| $error | float | The squared error of the training data |

setLearningRate(array $learningRate) : voidSets the learning rate between the different layers.
| Name | Type | Description |
|---|---|---|
| $learningRate | array | 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. |

setMomentum(float $momentum) : voidSets the 'momentum' for the learning algorithm. The momentum should accelerate the learning process and help avoid local minima.
| Name | Type | Description |
|---|---|---|
| $momentum | float | The momentum. Must be between 0.0 and 1.0; Usually between 0.5 and 0.9 |

setTrainingSuccessful(boolean $success) : voidAfter training, this function is used to store whether or not the training was successful.
| Name | Type | Description |
|---|---|---|
| $success | boolean | 'true' if the training was successful, 'false' otherwise |

setVerbose(boolean $isVerbose) : voidDetermines if the neural network displays status and error messages. By default, it does.
| Name | Type | Description |
|---|---|---|
| $isVerbose | boolean | 'true' if you want to display status and error messages, 'false' if you don't |

showWeights(boolean $force = false) : voidShows the current weights and thresholds
| Name | Type | Description |
|---|---|---|
| $force | boolean | Force the output, even if the network is {@link setVerbose() not verbose}. |

squaredError(array $input, array $desired_output) : floatCalculate the root-mean-squared error of the output, given the desired output.
| Name | Type | Description |
|---|---|---|
| $input | array | The input to test |
| $desired_output | array | The desired output |
| Type | Description |
|---|---|
| float | The root-mean-squared error of the output compared to the desired output |

squaredErrorControlSet() : floatCalculate the root-mean-squared error of the output, given the controldata.
| Type | Description |
|---|---|
| float | The root-mean-squared error of the output |

squaredErrorEpoch() : floatCalculate the root-mean-squared error of the output, given the trainingdata.
| Type | Description |
|---|---|
| float | The root-mean-squared error of the output |

train(integer $maxEpochs = 500, float $maxError = 0.01) : booleanStart the training process
| Name | Type | Description |
|---|---|---|
| $maxEpochs | integer | The maximum number of epochs |
| $maxError | float | The maximum squared error in the training data |
| Type | Description |
|---|---|
| boolean | 'true' if the training was successful, 'false' otherwise |