forked from jorgecasas/php-ml
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from php-ai/develop
Simple Neural Network with MultilayerPerceptron and Backpropagation
- Loading branch information
Showing
44 changed files
with
1,599 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Contributing to PHP-ML | ||
|
||
PHP-ML is an open source project. If you'd like to contribute, please read the following text. Before I can merge your | ||
Pull-Request here are some guidelines that you need to follow. These guidelines exist not to annoy you, but to keep the | ||
code base clean, unified and future proof. | ||
|
||
## Branch | ||
|
||
You should only open pull requests against the develop branch. | ||
|
||
## Unit-Tests | ||
|
||
Please try to add a test for your pull-request. You can run the unit-tests by calling: | ||
|
||
``` | ||
bin/phpunit | ||
``` | ||
|
||
## Travis | ||
|
||
GitHub automatically run your pull request through Travis CI against PHP 7. | ||
If you break the tests, I cannot merge your code, so please make sure that your code is working | ||
before opening up a Pull-Request. | ||
|
||
## Merge | ||
|
||
Please allow me time to review your pull requests. I will give my best to review everything as fast as possible, but cannot always live up to my own expectations. | ||
|
||
## Coding Standards | ||
|
||
When contributing code to PHP-ML, you must follow its coding standards. To make a long story short, here is the golden tool: | ||
|
||
``` | ||
tools/php-cs-fixer.sh | ||
``` | ||
|
||
This script run PHP Coding Standards Fixer with `--level=symfony` param. | ||
|
||
More about PHP-CS-Fixer: [http://cs.sensiolabs.org/](http://cs.sensiolabs.org/) | ||
|
||
--- | ||
|
||
Thank you very much again for your contribution! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Backpropagation | ||
|
||
Backpropagation, an abbreviation for "backward propagation of errors", is a common method of training artificial neural networks used in conjunction with an optimization method such as gradient descent. | ||
|
||
## Constructor Parameters | ||
|
||
* $network (Network) - network to train (for example MultilayerPerceptron instance) | ||
* $theta (int) - network theta parameter | ||
|
||
``` | ||
use Phpml\NeuralNetwork\Network\MultilayerPerceptron; | ||
use Phpml\NeuralNetwork\Training\Backpropagation; | ||
$network = new MultilayerPerceptron([2, 2, 1]); | ||
$training = new Backpropagation($network); | ||
``` | ||
|
||
## Training | ||
|
||
Example of XOR training: | ||
|
||
``` | ||
$training->train( | ||
$samples = [[1, 0], [0, 1], [1, 1], [0, 0]], | ||
$targets = [[1], [1], [0], [0]], | ||
$desiredError = 0.2, | ||
$maxIteraions = 30000 | ||
); | ||
``` |
29 changes: 29 additions & 0 deletions
29
docs/machine-learning/neural-network/multilayer-perceptron.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# MultilayerPerceptron | ||
|
||
A multilayer perceptron (MLP) is a feedforward artificial neural network model that maps sets of input data onto a set of appropriate outputs. | ||
|
||
## Constructor Parameters | ||
|
||
* $layers (array) - array with layers configuration, each value represent number of neurons in each layers | ||
* $activationFunction (ActivationFunction) - neuron activation function | ||
|
||
``` | ||
use Phpml\NeuralNetwork\Network\MultilayerPerceptron; | ||
$mlp = new MultilayerPerceptron([2, 2, 1]); | ||
// 2 nodes in input layer, 2 nodes in first hidden layer and 1 node in output layer | ||
``` | ||
|
||
## Methods | ||
|
||
* setInput(array $input) | ||
* getOutput() | ||
* getLayers() | ||
* addLayer(Layer $layer) | ||
|
||
## Activation Functions | ||
|
||
* BinaryStep | ||
* Gaussian | ||
* HyperbolicTangent | ||
* Sigmoid (default) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare (strict_types = 1); | ||
|
||
namespace Phpml\NeuralNetwork; | ||
|
||
interface ActivationFunction | ||
{ | ||
/** | ||
* @param float|int $value | ||
* | ||
* @return float | ||
*/ | ||
public function compute($value): float; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare (strict_types = 1); | ||
|
||
namespace Phpml\NeuralNetwork\ActivationFunction; | ||
|
||
use Phpml\NeuralNetwork\ActivationFunction; | ||
|
||
class BinaryStep implements ActivationFunction | ||
{ | ||
/** | ||
* @param float|int $value | ||
* | ||
* @return float | ||
*/ | ||
public function compute($value): float | ||
{ | ||
return $value >= 0 ? 1.0 : 0.0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare (strict_types = 1); | ||
|
||
namespace Phpml\NeuralNetwork\ActivationFunction; | ||
|
||
use Phpml\NeuralNetwork\ActivationFunction; | ||
|
||
class Gaussian implements ActivationFunction | ||
{ | ||
/** | ||
* @param float|int $value | ||
* | ||
* @return float | ||
*/ | ||
public function compute($value): float | ||
{ | ||
return exp(-pow($value, 2)); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Phpml/NeuralNetwork/ActivationFunction/HyperbolicTangent.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare (strict_types = 1); | ||
|
||
namespace Phpml\NeuralNetwork\ActivationFunction; | ||
|
||
use Phpml\NeuralNetwork\ActivationFunction; | ||
|
||
class HyperbolicTangent implements ActivationFunction | ||
{ | ||
/** | ||
* @var float | ||
*/ | ||
private $beta; | ||
|
||
/** | ||
* @param float $beta | ||
*/ | ||
public function __construct($beta = 1.0) | ||
{ | ||
$this->beta = $beta; | ||
} | ||
|
||
/** | ||
* @param float|int $value | ||
* | ||
* @return float | ||
*/ | ||
public function compute($value): float | ||
{ | ||
return tanh($this->beta * $value); | ||
} | ||
} |
Oops, something went wrong.