NeuralNetwork
  • Class
  • Tree

Classes

  • NetworkGraph
  • NeuralNetwork

Class NeuralNetwork

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...<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.

License: BSD License
Author: E. Akerboom
Author: Tremani, Delft, The Netherlands
Version: 1.1
Since: feb 2007
Located at class_neuralnetwork.php

Methods summary

public
# __construct( array $nodeCount )

Creates a neural network.

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

$nodeCount
The number of nodes in the consecutive layers.
public
# export( )

Exports the neural network

Exports the neural network

Returns

array
public
# import( array $nn_array )

Import a neural network

Import a neural network

Parameters

$nn_array
An array of the neural network parameters
public
# setLearningRate( array $learningRate )

Sets the learning rate between the different layers.

Sets the learning rate between the different layers.

Parameters

$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.

public float
# getLearningRate( integer $layer )

Gets the learning rate for a specific layer

Gets the learning rate for a specific layer

Parameters

$layer
The layer to obtain the learning rate for

Returns

float
The learning rate for that layer
public
# setMomentum( float $momentum )

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

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

Parameters

$momentum
The momentum. Must be between 0.0 and 1.0; Usually between 0.5 and 0.9
public float
# getMomentum( )

Gets the momentum.

Gets the momentum.

Returns

float
The momentum
public mixed
# calculate( array $input )

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

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

Parameters

$input
The vector to calculate

Returns

mixed
The output of the network
protected float
# activation( float $value )

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

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

Parameters

$value
The preliminary output to apply this function to

Returns

float
The final output of the node
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);

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

$value
'X'

Returns


$float
public NetworkGraph
# networkgraph( )

Calculates the parameters for the NetworkGraph and returns it

Calculates the parameters for the NetworkGraph and returns it

Returns

NetworkGraph
NetworkGraph class
public
# addTestData( array $input, array $output, integer $id = null )

Add a test vector and its output

Add a test vector and its output

Parameters

$input
An input vector
$output
The corresponding output
$id
(optional) An identifier for this piece of data
public array
# getTestDataIDs( )

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

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

Returns

array
An array of identifiers
public
# addControlData( array $input, array $output, integer $id = null )

Add a set of control data to the network.

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

$input
An input vector
$output
The corresponding output
$id
(optional) An identifier for this piece of data
public array
# getControlDataIDs( )

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

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

Returns

array
An array of identifiers
public
# showWeights( boolean $force = false )

Shows the current weights and thresholds

Shows the current weights and thresholds

Parameters

$force
Force the output, even if the network is setVerbose() not verbose.
public
# setVerbose( boolean $isVerbose )

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

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

Parameters

$isVerbose
'true' if you want to display status and error messages, 'false' if you don't
public boolean
# isVerbose( )

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

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

Returns

boolean
'true' if status and error messages are displayed, 'false' otherwise
public boolean
# load( string $filename )

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

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

Parameters

$filename
The filename to load the network from

Returns

boolean
'true' on success, 'false' otherwise
public boolean
# save( string $filename )

Saves a neural network to a file

Saves a neural network to a file

Parameters

$filename
The filename to save the neural network to

Returns

boolean
'true' on success, 'false' otherwise
public
# clear( )

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

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

public boolean
# train( integer $maxEpochs = 500, float $maxError = 0.01 )

Start the training process

Start the training process

Parameters

$maxEpochs
The maximum number of epochs
$maxError
The maximum squared error in the training data

Returns

boolean
'true' if the training was successful, 'false' otherwise
public integer
# getEpoch( )

Gets the number of epochs the network needed for training.

Gets the number of epochs the network needed for training.

Returns

integer
The number of epochs.
public float
# getErrorTrainingSet( )

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

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

Returns

float
The squared error of the training data
public float
# getErrorControlSet( )

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

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

Returns

float
The squared error of the control data
public boolean
# getTrainingSuccessful( )

Determines if the training was successful.

Determines if the training was successful.

Returns

boolean
'true' if the training was successful, 'false' otherwise
public
# initWeights( )

Randomise the weights in the neural network

Randomise the weights in the neural network

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
#
NeuralNetwork API documentation generated by ApiGen