$trainInputs
$trainInputs : array
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 "
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.
__construct(array $nodeCount)
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));
| array | $nodeCount | The number of nodes in the consecutive layers. |
setLearningRate(array $learningRate)
Sets the learning rate between the different layers.
| array | $learningRate | 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. |
addControlData(array $input, array $output, integer $id = null)
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.
| array | $input | An input vector |
| array | $output | The corresponding output |
| integer | $id | (optional) An identifier for this piece of data |
fitLine(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.
| array | $data | 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. |
An array containing, respectively, the slope and the offset of the fitted line.
squaredError(array $input, array $desired_output) : float
Calculate the root-mean-squared error of the output, given the desired output.
| array | $input | The input to test |
| array | $desired_output | The desired output |
The root-mean-squared error of the output compared to the desired output