Class NeuralNetwork
Multilayer 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 0201711591)
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 testdata 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...<hr />"; } // print a message if the network was succesfully trained if ($success) { $epochs = $n>getEpoch(); echo "Success in $epochs training rounds!<hr />"; } // in any case, we print the output of the neural network echo "<h2>End result</h2>"; for ($i = 0; $i < count($n>trainInputs); $i ++) { $output = $n>calculate($n>trainInputs[$i]); echo "<br />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: Tremani, Delft, The Netherlands
Version: 1.1
Since: feb 2007
Located at class_neuralnetwork.php
Methods summary
public


public


public


public


public
float


public

#
setMomentum( float $momentum )
Sets the 'momentum' for the learning algorithm. The momentum should accelerate the learning process and help avoid local minima. 
public
float


public
mixed


protected
float

#
activation( float $value )
Implements the standard (default) activation function for backpropagation networks, the 'tanh' activation function. 
protected

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


public


public
array


public

#
addControlData( array $input, array $output, integer $id = null )
Add a set of control data to the network. 
public
array

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


public

#
setVerbose( boolean $isVerbose )
Determines if the neural network displays status and error messages. By default, it does. 
public
boolean


public
boolean


public
boolean


public


public
boolean


public
integer


public
float

#
getErrorTrainingSet( )
Gets the squared error between the desired output and the obtained output of the training data. 
public
float

#
getErrorControlSet( )
Gets the squared error between the desired output and the obtained output of the control data. 
public
boolean


public

Properties summary
protected
array

$nodeCount

#
array ()

protected
array

$nodeValue

#
array ()

protected
array

$nodeThreshold

#
array ()

protected
array

$edgeWeight

#
array ()

protected
array

$learningRate

#
array (0.1)

protected
integer

$layerCount

#
0

protected
array

$previousWeightCorrection

#
array ()

protected
float

$momentum

#
0.8

protected
boolean

$isVerbose

#
true

protected
boolean

$weightsInitialized

#
false

public
array

$trainInputs

#
array ()

public
array

$trainOutput

#
array ()

public
array

$trainDataID

#
array ()

public
array

$controlInputs

#
array ()

public
array

$controlOutput

#
array ()

public
array

$controlDataID

#
array ()

protected

$epoch


protected

$errorTrainingset


protected

$errorControlset


protected

$success
