Class: NeuralNetwork

Class Overview


Multi-layer Neural Network in PHP


Author(s):

Version:

  • 1.0

Methods



Class Details

[line 75]
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

  1.  require_once("class_neuralnetwork.php");
  2.  
  3.  // Create a new neural network with 3 input neurons,
  4.  // 4 hidden neurons, and 1 output neuron
  5.   $n new NeuralNetwork(341);
  6.  $n->setVerbose(false);
  7.  
  8.  // Add test-data to the network. In this case,
  9.  // we want the network to learn the 'XOR'-function.
  10.  // The third input-parameter is the 'bias'.
  11.   $n->addTestDataarray (-1-11)array (-1));
  12.  $n->addTestDataarray (-1,  11)array 1));
  13.  $n->addTestDataarray 1-11)array 1));
  14.  $n->addTestDataarray 1,  11)array (-1));
  15.  
  16.  // we try training the network for at most $max times
  17.   $max 3;
  18.  
  19.  // train the network in max 1000 epochs, with a max squared error of 0.01
  20.   while (!($success=$n->train(10000.01)) && $max-->0{
  21.          // training failed:
  22.          // 1. re-initialize the weights in the network
  23.                   $n->initWeights();
  24.  
  25.          // 2. display message
  26.                   echo "Nothing found...<hr />";
  27.  }
  28.  
  29.  // print a message if the network was succesfully trained
  30.   if ($success{
  31.          $epochs $n->getEpoch();
  32.          echo "Success in $epochs training rounds!<hr />";
  33.  }
  34.  
  35.  // in any case, we print the output of the neural network
  36.   for ($i 0$i count($n->trainInputs)$i ++{
  37.          $output $n->calculate($n->trainInputs[$i]);
  38.          print "<br />Testset $i";
  39.          print "expected output = (".implode(", "$n->trainOutput[$i]).") ";
  40.          print "output from neural network = (".implode(", "$output).")\n";
  41.  }

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

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

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




Tags:

version:  1.0
since:  feb 2007
author:  E. Akerboom
author:  Tremani, Delft
license:  BSD License


[ Top ]


Class Methods


constructor NeuralNetwork [line 124]

NeuralNetwork NeuralNetwork( array $nodecount)

Creates a neural network.

Example:

  1.  // create a network with 4 input nodes, 10 hidden nodes, and 4 output nodes
  2.   $n new NeuralNetwork(4104);
  3.  
  4.  // create a network with 4 input nodes, 1 hidden layer with 10 nodes,
  5.  // another hidden layer with 10 nodes, and 4 output nodes
  6.   $n new NeuralNetwork(410104);
  7.  
  8.  // alternative syntax
  9.   $n new NeuralNetwork(array(410104));




Parameters:

array   $nodecount   The number of nodes in the consecutive layers.

[ Top ]

method activation [line 234]

float activation( float $value)

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



Tags:

return:  The final output of the node


Parameters:

float   $value   The preliminary output to apply this function to

[ Top ]

method addControlData [line 294]

void addControlData( array $input, array $output, [int $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.




Parameters:

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

[ Top ]

method addTestData [line 259]

void addTestData( array $input, array $output, [int $id = null])

Add a test vector and its output



Parameters:

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

[ Top ]

method calculate [line 187]

mixed calculate( array $input)

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



Tags:

return:  The output of the network


Parameters:

array   $input   The vector to calculate

[ Top ]

method derivative_activation [line 246]

$float derivative_activation( 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);



Parameters:

float   $value   'X'

[ Top ]

method getControlDataIDs [line 313]

array getControlDataIDs( )

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



Tags:

return:  An array of identifiers


[ Top ]

method getLearningRate [line 155]

float getLearningRate( int $layer)

Gets the learning rate for a specific layer



Tags:

return:  The learning rate for that layer


Parameters:

int   $layer   The layer to obtain the learning rate for

[ Top ]

method getMomentum [line 177]

float getMomentum( )

Gets the momentum.



Tags:

return:  The momentum


[ Top ]

method getRandomWeight [line 647]

float getRandomWeight( $layer)

Gets a random weight between [-0.25 .. 0.25]. Used to initialize the network.



Tags:

return:  A random weight


Parameters:

   $layer  

[ Top ]

method getTestDataIDs [line 277]

array getTestDataIDs( )

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



Tags:

return:  An array of identifiers


[ Top ]

method isVerbose [line 344]

boolean isVerbose( )

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



Tags:

return:  'true' if status and error messages are displayed, 'false' otherwise


[ Top ]

method load [line 355]

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.



Tags:

return:  'true' on success, 'false' otherwise


Parameters:

string   $filename   The filename to load the network from

[ Top ]

method save [line 399]

boolean save( string $filename)

Saves a neural network to a file



Tags:

return:  'true' on success, 'false' otherwise


Parameters:

string   $filename   The filename to save the neural network to

[ Top ]

method setLearningRate [line 141]

void setLearningRate( array $learningrate)

Sets the learning rate between the different layers.



Parameters:

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.

[ Top ]

method setMomentum [line 168]

void setMomentum( float $momentum)

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



Parameters:

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

[ Top ]

method setVerbose [line 335]

void setVerbose( boolean $is_verbose)

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



Parameters:

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

[ Top ]

method showWeights [line 322]

void showWeights( [boolean $force = false])

Shows the current weights and thresholds



Parameters:

boolean   $force   Force the output, even if the network is not verbose.

[ Top ]

method train [line 424]

bool train( [int $maxEpochs = 500], [float $maxError = 0.01])

Start the training process



Tags:

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


Parameters:

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

[ Top ]


Documentation generated by phpDocumentor 1.3.1