82 lines
1.8 KiB
PHP
82 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare (strict_types = 1);
|
|
|
|
namespace Phpml\Regression;
|
|
|
|
|
|
use Phpml\Helper\Predictable;
|
|
use Phpml\NeuralNetwork\ActivationFunction;
|
|
use Phpml\NeuralNetwork\Network\MultilayerPerceptron;
|
|
use Phpml\NeuralNetwork\Training\Backpropagation;
|
|
|
|
class MLPRegressor implements Regression
|
|
{
|
|
use Predictable;
|
|
|
|
/**
|
|
* @var MultilayerPerceptron
|
|
*/
|
|
private $perceptron;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
private $hiddenLayers;
|
|
|
|
/**
|
|
* @var float
|
|
*/
|
|
private $desiredError;
|
|
|
|
/**
|
|
* @var int
|
|
*/
|
|
private $maxIterations;
|
|
|
|
/**
|
|
* @var ActivationFunction
|
|
*/
|
|
private $activationFunction;
|
|
|
|
/**
|
|
* @param array $hiddenLayers
|
|
* @param float $desiredError
|
|
* @param int $maxIterations
|
|
* @param ActivationFunction $activationFunction
|
|
*/
|
|
public function __construct(array $hiddenLayers = [100], float $desiredError, int $maxIterations, ActivationFunction $activationFunction = null)
|
|
{
|
|
$this->hiddenLayers = $hiddenLayers;
|
|
$this->desiredError = $desiredError;
|
|
$this->maxIterations = $maxIterations;
|
|
$this->activationFunction = $activationFunction;
|
|
}
|
|
|
|
|
|
/**
|
|
* @param array $samples
|
|
* @param array $targets
|
|
*/
|
|
public function train(array $samples, array $targets)
|
|
{
|
|
$layers = [count($samples[0])] + $this->hiddenLayers + [count($targets[0])];
|
|
|
|
$this->perceptron = new MultilayerPerceptron($layers, $this->activationFunction);
|
|
|
|
$trainer = new Backpropagation($this->perceptron);
|
|
$trainer->train($samples, $targets, $this->desiredError, $this->maxIterations);
|
|
}
|
|
|
|
/**
|
|
* @param array $sample
|
|
*
|
|
* @return array
|
|
*/
|
|
protected function predictSample(array $sample)
|
|
{
|
|
return $this->perceptron->setInput($sample)->getOutput();
|
|
}
|
|
|
|
}
|