Skip to content

Latest commit

 

History

History
48 lines (40 loc) · 1.7 KB

README.md

File metadata and controls

48 lines (40 loc) · 1.7 KB

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.