forked from jorgecasas/php-ml
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* MultilayerPerceptron interface changes - Signature closer to other algorithms - New predict method - Remove desired error - Move maxIterations to constructor * MLP tests for multiple hidden layers and multi-class * Update all MLP-related tests * coding style fixes * Backpropagation included in multilayer-perceptron
- Loading branch information
Showing
18 changed files
with
369 additions
and
343 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
This file was deleted.
Oops, something went wrong.
50 changes: 50 additions & 0 deletions
50
docs/machine-learning/neural-network/multilayer-perceptron-classifier.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,50 @@ | ||
# MLPClassifier | ||
|
||
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 | ||
|
||
* $inputLayerFeatures (int) - the number of input layer features | ||
* $hiddenLayers (array) - array with the hidden layers configuration, each value represent number of neurons in each layers | ||
* $classes (array) - array with the different training set classes (array keys are ignored) | ||
* $iterations (int) - number of training iterations | ||
* $theta (int) - network theta parameter | ||
* $activationFunction (ActivationFunction) - neuron activation function | ||
|
||
``` | ||
use Phpml\Classification\MLPClassifier; | ||
$mlp = new MLPClassifier(4, [2], ['a', 'b', 'c']); | ||
// 4 nodes in input layer, 2 nodes in first hidden layer and 3 possible labels. | ||
``` | ||
|
||
## Train | ||
|
||
To train a MLP simply provide train samples and labels (as array). Example: | ||
|
||
|
||
``` | ||
$mlp->train( | ||
$samples = [[1, 0, 0, 0], [0, 1, 1, 0], [1, 1, 1, 1], [0, 0, 0, 0]], | ||
$targets = ['a', 'a', 'b', 'c'] | ||
); | ||
``` | ||
|
||
## Predict | ||
|
||
To predict sample label use predict method. You can provide one sample or array of samples: | ||
|
||
``` | ||
$mlp->predict([[1, 1, 1, 1], [0, 0, 0, 0]]); | ||
// return ['b', 'c']; | ||
``` | ||
|
||
## Activation Functions | ||
|
||
* BinaryStep | ||
* Gaussian | ||
* HyperbolicTangent | ||
* Sigmoid (default) |
29 changes: 0 additions & 29 deletions
29
docs/machine-learning/neural-network/multilayer-perceptron.md
This file was deleted.
Oops, something went wrong.
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,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Phpml\Classification; | ||
|
||
use Phpml\Classification\Classifier; | ||
use Phpml\Exception\InvalidArgumentException; | ||
use Phpml\NeuralNetwork\Network\MultilayerPerceptron; | ||
use Phpml\NeuralNetwork\Training\Backpropagation; | ||
use Phpml\NeuralNetwork\ActivationFunction; | ||
use Phpml\NeuralNetwork\Layer; | ||
use Phpml\NeuralNetwork\Node\Bias; | ||
use Phpml\NeuralNetwork\Node\Input; | ||
use Phpml\NeuralNetwork\Node\Neuron; | ||
use Phpml\NeuralNetwork\Node\Neuron\Synapse; | ||
use Phpml\Helper\Predictable; | ||
|
||
class MLPClassifier extends MultilayerPerceptron implements Classifier | ||
{ | ||
|
||
/** | ||
* @param mixed $target | ||
* @return int | ||
*/ | ||
public function getTargetClass($target): int | ||
{ | ||
if (!in_array($target, $this->classes)) { | ||
throw InvalidArgumentException::invalidTarget($target); | ||
} | ||
return array_search($target, $this->classes); | ||
} | ||
|
||
/** | ||
* @param array $sample | ||
* | ||
* @return mixed | ||
*/ | ||
protected function predictSample(array $sample) | ||
{ | ||
$output = $this->setInput($sample)->getOutput(); | ||
|
||
$predictedClass = null; | ||
$max = 0; | ||
foreach ($output as $class => $value) { | ||
if ($value > $max) { | ||
$predictedClass = $class; | ||
$max = $value; | ||
} | ||
} | ||
return $this->classes[$predictedClass]; | ||
} | ||
|
||
/** | ||
* @param array $sample | ||
* @param mixed $target | ||
*/ | ||
protected function trainSample(array $sample, $target) | ||
{ | ||
|
||
// Feed-forward. | ||
$this->setInput($sample)->getOutput(); | ||
|
||
// Back-propagate. | ||
$this->backpropagation->backpropagate($this->getLayers(), $this->getTargetClass($target)); | ||
} | ||
} |
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
Oops, something went wrong.