diff --git a/src/Phpml/NeuralNetwork/Network.php b/src/Phpml/NeuralNetwork/Network.php index be59b34..269351f 100644 --- a/src/Phpml/NeuralNetwork/Network.php +++ b/src/Phpml/NeuralNetwork/Network.php @@ -4,9 +4,8 @@ declare (strict_types = 1); namespace Phpml\NeuralNetwork; -interface Network extends Node +interface Network { - /** * @param mixed $input */ @@ -15,6 +14,15 @@ interface Network extends Node /** * @return array */ - public function getLayers(): array; + public function getOutput(): array; + /** + * @param Layer $layer + */ + public function addLayer(Layer $layer); + + /** + * @return Layer[] + */ + public function getLayers(): array; } diff --git a/src/Phpml/NeuralNetwork/Network/LayeredNetwork.php b/src/Phpml/NeuralNetwork/Network/LayeredNetwork.php index a46b267..699c4d4 100644 --- a/src/Phpml/NeuralNetwork/Network/LayeredNetwork.php +++ b/src/Phpml/NeuralNetwork/Network/LayeredNetwork.php @@ -4,25 +4,51 @@ declare (strict_types = 1); namespace Phpml\NeuralNetwork\Network; +use Phpml\NeuralNetwork\Layer; use Phpml\NeuralNetwork\Network; abstract class LayeredNetwork implements Network { + /** + * @var Layer[] + */ + protected $layers; + + /** + * @param Layer $layer + */ + public function addLayer(Layer $layer) + { + $this->layers[] = $layer; + } + + /** + * @return Layer[] + */ + public function getLayers(): array + { + return $this->layers; + } + + /** + * @return Layer + */ + public function getOutputLayer(): Layer + { + return $this->layers[count($this->layers) - 1]; + } /** * @return array */ - public function getLayers(): array - { - - } - - /** - * @return float - */ - public function getOutput(): float + public function getOutput(): array { + $result = []; + foreach ($this->getOutputLayer()->getNodes() as $neuron) { + $result[] = $neuron->getOutput(); + } + return $result; } /** @@ -30,7 +56,10 @@ abstract class LayeredNetwork implements Network */ public function setInput($input) { + $firstLayer = $this->layers[0]; + foreach ($firstLayer->getNodes() as $key => $neuron) { + $neuron->setInput($input[$key]); + } } - } diff --git a/src/Phpml/NeuralNetwork/Network/MultilayerPerceptron.php b/src/Phpml/NeuralNetwork/Network/MultilayerPerceptron.php index ce5a615..c0f7df3 100644 --- a/src/Phpml/NeuralNetwork/Network/MultilayerPerceptron.php +++ b/src/Phpml/NeuralNetwork/Network/MultilayerPerceptron.php @@ -6,5 +6,4 @@ namespace Phpml\NeuralNetwork\Network; class MultilayerPerceptron extends LayeredNetwork { - } diff --git a/src/Phpml/NeuralNetwork/Training.php b/src/Phpml/NeuralNetwork/Training.php index 9870c11..932f189 100644 --- a/src/Phpml/NeuralNetwork/Training.php +++ b/src/Phpml/NeuralNetwork/Training.php @@ -10,7 +10,7 @@ interface Training * @param array $samples * @param array $targets * @param float $desiredError - * @param int $maxIterations + * @param int $maxIterations */ public function train(array $samples, array $targets, float $desiredError = 0.001, int $maxIterations = 10000); } diff --git a/src/Phpml/NeuralNetwork/Training/Backpropagation.php b/src/Phpml/NeuralNetwork/Training/Backpropagation.php index 17ca44c..ce9f5e6 100644 --- a/src/Phpml/NeuralNetwork/Training/Backpropagation.php +++ b/src/Phpml/NeuralNetwork/Training/Backpropagation.php @@ -8,16 +8,13 @@ use Phpml\NeuralNetwork\Training; class Backpropagation implements Training { - /** * @param array $samples * @param array $targets * @param float $desiredError - * @param int $maxIterations + * @param int $maxIterations */ public function train(array $samples, array $targets, float $desiredError = 0.001, int $maxIterations = 10000) { - } - }