Fast Artificial Neural Network Library - PHP Extension

Steffen Nissen

Evan Nemerson


Table of Contents
1. PHP Extension
1.1. Installation
1.1.1. Using PEAR
1.1.2. Compiling into PHP
1.2. API Reference
fann_create -- Creates an artificial neural network.
fann_train -- Train an artificial neural network.
fann_save -- Save an artificial neural network to a file.
fann_run -- Run an artificial neural network.
fann_randomize_weights -- Randomize the weights of the neurons in the network.
fann_init_weights -- Initialize the weight of each connection.
fann_get_MSE -- Get the mean squared error.
fann_get_num_input -- Get the number of input neurons.
fann_get_num_output -- Get the number of output neurons.
fann_get_total_neurons -- Get the total number of neurons.
fann_get_total_connections -- Get the total number of connections.
fann_get_learning_rate -- Get the learning rate.
fann_get_activation_function_hidden -- Get the activation function of the hidden neurons.
fann_get_activation_function_output -- Get the activation function of the output neurons.
fann_get_activation_steepness_hidden -- Get the steepness of the activation function for the hidden neurons.
fann_get_activation_steepness_output -- Get the steepness of the activation function for the output neurons.
fann_set_learning_rate -- Set the learning rate.
fann_set_activation_function_hidden -- Set the activation function for the hidden neurons.
fann_set_activation_function_output -- Set the activation function for the output neurons.
fann_set_activation_steepness_hidden -- Set the steepness of the activation function for the hidden neurons.
fann_set_activation_steepness_output -- Set the steepness of the activation function for the output neurons.
Bibliography
List of Examples
1-1. fann_create from scratch
1-2. fann_create loading from a file
1-1. fann_create from training data
1-1. fann_runExample

Chapter 1. PHP Extension

These functions allow you to interact with the FANN library from PHP.

This extension requires the FANN library, version 1.1.0 or later.

This extension supports the same activation functions as the library, a list of which can be found in the reference manual for the C library.


1.1. Installation

1.1.1. Using PEAR

The easiest way to install FANN-PHP is to use PEAR- if you have a fairly recent version of PHP installed, simply run pear install fann. Note that if there are no stable releases of FANN-PHP, you may have to specify the URI for the package, which can be obtained from http://pecl.php.net/fann.

If you cannot install FANN-PHP using PEAR, you can try following the (obsolete) instructions at http://www.cs.utexas.edu/users/UTCS/online-docs/php/pear/faq.install-pecl.html.

If you use one of these methods, you'll need to either dl('fann.so') or add it to your php.ini

If you use either of the above methods, you will probably need to be root.


1.1.2. Compiling into PHP

Please only use this method if using the methods outlined in Using PEAR have failed.

If you wish to compile FANN-PHP into PHP itself, you can. First, uncompress the package into the ext subdirectory of your copy of the PHP source code, and rename the directory to ext/fann (from fann-x.x.x).

Next, you must rebuild the configure script- to do so, run ./buildconf from the PHP source directory.

From here on, the procedure is similar to when you built PHP originally- run ./configure with your desired options, plus --with-fann.

Finally, run make and make install. Note that you will probably need to be root for make install to work.

This method may require flex and bison to work- more information can be obtained at http://www.php.net/anoncvs.php


1.2. API Reference

Table of Contents
fann_create -- Creates an artificial neural network.
fann_train -- Train an artificial neural network.
fann_save -- Save an artificial neural network to a file.
fann_run -- Run an artificial neural network.
fann_randomize_weights -- Randomize the weights of the neurons in the network.
fann_init_weights -- Initialize the weight of each connection.
fann_get_MSE -- Get the mean squared error.
fann_get_num_input -- Get the number of input neurons.
fann_get_num_output -- Get the number of output neurons.
fann_get_total_neurons -- Get the total number of neurons.
fann_get_total_connections -- Get the total number of connections.
fann_get_learning_rate -- Get the learning rate.
fann_get_activation_function_hidden -- Get the activation function of the hidden neurons.
fann_get_activation_function_output -- Get the activation function of the output neurons.
fann_get_activation_steepness_hidden -- Get the steepness of the activation function for the hidden neurons.
fann_get_activation_steepness_output -- Get the steepness of the activation function for the output neurons.
fann_set_learning_rate -- Set the learning rate.
fann_set_activation_function_hidden -- Set the activation function for the hidden neurons.
fann_set_activation_function_output -- Set the activation function for the output neurons.
fann_set_activation_steepness_hidden -- Set the steepness of the activation function for the hidden neurons.
fann_set_activation_steepness_output -- Set the steepness of the activation function for the output neurons.

fann_create

Name

fann_create -- Creates an artificial neural network.

Description

mixed fann_create(mixed data, float connection_rate, float learning_rate);

fann_create will create an artificial neural network using the data given.

If the first parameter is an array, fann_create will use the data and structure of the array, as well as connection_rate and learning_rate.

If fann_create is called with a sole string argument, it will attempt to load an ANN created with fann_save from the file at filename.

fann_create will return the artificial neural network on success, or FALSE if it fails.

Example 1-1. fann_create from scratch


<?php
$ann = fann_create(
  /* Layers. In this case, three layers-
   * two input neurons, 4 neurons on a
   * hidden layer, and one output neuron. */
  array(2, 4, 1),
  1.0,
  0.7);
?>

            

Example 1-2. fann_create loading from a file


<?php
$ann = fann_create("http://www.example.com/ann.net");
?>

            

See also fann_save.

This function appears in FANN-PHP >= 0.1.0.

fann_train

Name

fann_train -- Train an artificial neural network.

Description

bool fann_train(resource ann, mixed data, int max_iterations, double desired_error, int iterations_between_reports);

fann_train will train ann on the data supplied, returning TRUE on success or FALSE on failure.

Resources is an artificial neural network returned by fann_create.

data must be either an array of training data, or the URI of a properly formatted training file.

fann_train will continue training until desired_error is reached, or max_iterations is exceeded.

If iterations_between_reports is set, fann_create will output a short progress report every iterations_between_reports. Default is 0 (meaning no reports).

Example 1-1. fann_create from training data


<?php
$ann = fann_create(array(2, 4, 1), 1.0, 0.7);
if ( fann_train($ann,
           array(
                 array(
                       array(0,0), /* Input(s) */
                       array(0) /* Output(s) */
                       ),
                 array(
                       array(0,1), /* Input(s) */
                       array(1) /* Output(s) */
                       ),
                 array(
                       array(1,0), /* Input(s) */
                       array(1) /* Output(s) */
                       ),
                 array(array(1,1), /* Input(s) */
                       array(0) /* Output(s) */
                       )
                 ),
           100000,
           0.00001,
           1000) == FALSE) {
  exit('Could not train $ann.');
}
?>

            

This function appears in FANN-PHP >= 0.1.0.

fann_save

Name

fann_save -- Save an artificial neural network to a file.

Description

bool fann_save(resource ann, string filename);

fann_save will save ann to filename, returning TRUE on success or FALSE on failure.

See also fann_create.

This function appears in FANN-PHP >= 0.1.0.

fann_run

Name

fann_run -- Run an artificial neural network.

Description

mixed fann_run(resource ann, array input);

fann_run will run input through ann, returning an an output array on success or FALSE on failure.

Example 1-1. fann_runExample


<?php
if ( ($ann = fann_create("http://www.example.com/ann.net")) == FALSE )
  exit("Could not create ANN.");
if ( fann_train($ann, "http://www.example.com/train.data", 100000, 0.00001) == FALSE )
  exit("Could not train ANN.");

if ( ($output = fann_run($ann, array(0, 1))) == FALSE )
  exit("Could not run ANN.");
else
  print_r($output);
?>

            

This function appears in FANN-PHP >= 0.1.0.

fann_randomize_weights

Name

fann_randomize_weights -- Randomize the weights of the neurons in the network.

Description

void fann_randomize_weights(resource ann, float minimum, float maximum);

fann_randomize_weights will randomize the weights of all neurons in ann, effectively resetting the network.

fann_init_weights

This function appears in FANN-PHP >= 0.1.0.

fann_init_weights

Name

fann_init_weights -- Initialize the weight of each connection.

Description

void fann_init_weights(resource ann, mixed training_data);

This function behaves similarly to fann_randomize_weights. It will use the algorithm developed by Derrick Nguyen and Bernard Widrow [Nguyen and Widrow, 1990] to set the weights in such a way as to speed up training.

The algorithm requires access to the range of the input data (ie, largest and smallest input), and therefore accepts a second argument, data, which is the training data that will be used to train the network.

fann_randomize_weights

This function appears in FANN-PHP >= 0.1.0.

fann_get_MSE

Name

fann_get_MSE -- Get the mean squared error.

Description

float fann_get_MSE(resource ann);

fann_get_MSE will return the mean squared error (MSE) of ann, or 0 if it is unavailable.

This function appears in FANN-PHP >= 0.1.0.

fann_get_num_input

Name

fann_get_num_input -- Get the number of input neurons.

Description

int fann_get_num_input(resource ann);

fann_get_num_inputwill return the number of input neurons in ann.

See also fann_get_num_output, fann_get_total_neurons.

This function appears in FANN-PHP >= 0.1.0.

fann_get_num_output

Name

fann_get_num_output -- Get the number of output neurons.

Description

int fann_get_num_output(resource ann);

fann_get_num_output will return the number of output neurons in ann.

See also fann_get_num_input, fann_get_total_neurons.

This function appears in FANN-PHP >= 0.1.0.

fann_get_total_neurons

Name

fann_get_total_neurons -- Get the total number of neurons.

Description

int fann_get_total_neurons(resource ann);

fann_get_total_neuronswill return the total number of neurons in ann.

See also fann_get_num_input, fann_get_num_output.

This function appears in FANN-PHP >= 0.1.0.

fann_get_total_connections

Name

fann_get_total_connections -- Get the total number of connections.

Description

int fann_get_total_connections(resource ann);

fann_get_total_connections will return the total number of connections in ann.

This function appears in FANN-PHP >= 0.1.0.

fann_get_learning_rate

Name

fann_get_learning_rate -- Get the learning rate.

Description

float fann_get_learning_rate(resource ann);

fann_get_learning_rate will return the learning rate of ann.

See also fann_set_learning_rate.

This function appears in FANN-PHP >= 0.1.0.

fann_get_activation_function_hidden

Name

fann_get_activation_function_hidden -- Get the activation function of the hidden neurons.

Description

int fann_get_activation_function_hidden(resource ann);

fann_get_activation_function_hidden will return the activation function for the hidden neurons in ann.

See also fann_set_activation_function_hidden.

This function appears in FANN-PHP >= 0.1.0.

fann_get_activation_function_output

Name

fann_get_activation_function_output -- Get the activation function of the output neurons.

Description

int fann_get_activation_function_output(resource ann);

fann_get_activation_function_output will return the activation function for the output neurons in ann.

See also fann_set_activation_function_output.

This function appears in FANN-PHP >= 0.1.0.

fann_get_activation_steepness_hidden

Name

fann_get_activation_steepness_hidden -- Get the steepness of the activation function for the hidden neurons.

Description

float fann_get_activation_steepness_hidden(resource ann);

fann_get_activation_steepness_hidden will return the steepness of the activation function for the hidden neurons in ann.

See also fann_set_activation_steepness_hidden.

This function appears in FANN-PHP >= 0.1.0.

fann_get_activation_steepness_output

Name

fann_get_activation_steepness_output -- Get the steepness of the activation function for the output neurons.

Description

float fann_get_activation_steepness_output(resource ann);

fann_get_activation_steepness_output will return the steepness of the activation function for the output neurons in ann.

See also fann_set_activation_steepness_output.

This function appears in FANN-PHP >= 0.1.0.

fann_set_learning_rate

Name

fann_set_learning_rate -- Set the learning rate.

Description

float fann_set_learning_rate(resource ann);

fann_set_learning_rate will return the learning rate of ann.

See also fann_set_learning_rate.

This function appears in FANN-PHP >= 0.1.0.

fann_set_activation_function_hidden

Name

fann_set_activation_function_hidden -- Set the activation function for the hidden neurons.

Description

void fann_set_activation_function_hidden(resource ann, int activation_function);

fann_set_activation_function_hidden sets the activation function for the hidden neurons to activation_function, which must be one of the supported activation functions.

See also fann_get_activation_function_hidden.

This function appears in FANN-PHP >= 0.1.0.

fann_set_activation_function_output

Name

fann_set_activation_function_output -- Set the activation function for the output neurons.

Description

void fann_set_activation_function_output(resource ann, int activation_function);

fann_set_activation_function_output sets the activation function for the output neurons to activation_function, which must be one of the supported activation functions.

See also fann_get_activation_function_output.

This function appears in FANN-PHP >= 0.1.0.

fann_set_activation_steepness_hidden

Name

fann_set_activation_steepness_hidden -- Set the steepness of the activation function for the hidden neurons.

Description

void fann_set_activation_steepness_hidden(resource ann, float steepness);

fann_set_activation_steepness_hiddensets the steepness of the activation function hidden neurons to steepness.

See also fann_get_activation_steepness_hidden.

This function appears in FANN-PHP >= 0.1.0.

fann_set_activation_steepness_output

Name

fann_set_activation_steepness_output -- Set the steepness of the activation function for the output neurons.

Description

void fann_set_activation_steepness_output(resource ann, float steepness);

fann_set_activation_steepness_output sets the steepness of the activation function output neurons to steepness.

See also fann_get_activation_steepness_output.

This function appears in FANN-PHP >= 0.1.0.


Bibliography

[Nguyen and Widrow, 1990] Reinforcement Learning, Derrick Nguyen, Bernard Widrow, 1990, Proc. IJCNN, 3, 21-26, http://www.cs.montana.edu/~clemens/nguyen-widrow.pdf .