Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
A neural network (Simple Perceptron) implemented in PHP.
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
README.md
cmatrix.php
cperceptron.php

README.md

cPerceptron

A neural network (Simple Perceptron) implemented in PHP.

Usage

You need to create a training set of input and output values to train the network, like these:

$trainIn = array(
    array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1),
    array(1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1),
    array(-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1),
    array(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1));

$trainOut = array(
    array(1,1,1),
    array(1,-1,1),
    array(-1,1,-1),
    array(-1,-1,-1));

After this, you can call the function trainPS with the following parameters:

$cp = new cperceptron();
$w = $cp->trainPS($trainIn, $trainOut, $error, $iter, $vel);
  • $trainIn: Training set, input values.
  • $trainOut: Training set, output values.
  • $error: Maximum level of error.
  • $iter: Maximum number of iterations.
  • $vel: Learning speed.

The result of this call is a weight matrix corresponding to the neural network trained with the training set introduced.

Now, everything is ready to test the network. You just have to make the following function call:

$test = $cp->matrix2Vector($trainIn, 1);
$res = $cp->prodMatrix($test, $w);
$out = $cp->signMatrix($res);

Where 1 represents the input value you want to test. The result should be the value assigned in the Training Set (output value).

You can also test the network with altered values and let this approximates the results. For example, with this input value:

array(1,1,1,-1,1,1,1,1,1,1,1,1,-1,1,1,1,1,1,1,1,1,-1,1,1,1)

License

cPerceptron is released under the MIT License.

Something went wrong with that request. Please try again.