test abstraction from LayeredNetwork

This commit is contained in:
Arkadiusz Kondas 2016-08-07 23:41:02 +02:00
parent 12ee62bbca
commit ddb3cc367b
5 changed files with 52 additions and 19 deletions

View File

@ -4,9 +4,8 @@ declare (strict_types = 1);
namespace Phpml\NeuralNetwork; namespace Phpml\NeuralNetwork;
interface Network extends Node interface Network
{ {
/** /**
* @param mixed $input * @param mixed $input
*/ */
@ -15,6 +14,15 @@ interface Network extends Node
/** /**
* @return array * @return array
*/ */
public function getLayers(): array; public function getOutput(): array;
/**
* @param Layer $layer
*/
public function addLayer(Layer $layer);
/**
* @return Layer[]
*/
public function getLayers(): array;
} }

View File

@ -4,25 +4,51 @@ declare (strict_types = 1);
namespace Phpml\NeuralNetwork\Network; namespace Phpml\NeuralNetwork\Network;
use Phpml\NeuralNetwork\Layer;
use Phpml\NeuralNetwork\Network; use Phpml\NeuralNetwork\Network;
abstract class LayeredNetwork implements 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 * @return array
*/ */
public function getLayers(): array public function getOutput(): array
{ {
$result = [];
foreach ($this->getOutputLayer()->getNodes() as $neuron) {
$result[] = $neuron->getOutput();
} }
/** return $result;
* @return float
*/
public function getOutput(): float
{
} }
/** /**
@ -30,7 +56,10 @@ abstract class LayeredNetwork implements Network
*/ */
public function setInput($input) public function setInput($input)
{ {
$firstLayer = $this->layers[0];
foreach ($firstLayer->getNodes() as $key => $neuron) {
$neuron->setInput($input[$key]);
}
} }
} }

View File

@ -6,5 +6,4 @@ namespace Phpml\NeuralNetwork\Network;
class MultilayerPerceptron extends LayeredNetwork class MultilayerPerceptron extends LayeredNetwork
{ {
} }

View File

@ -8,7 +8,6 @@ use Phpml\NeuralNetwork\Training;
class Backpropagation implements Training class Backpropagation implements Training
{ {
/** /**
* @param array $samples * @param array $samples
* @param array $targets * @param array $targets
@ -17,7 +16,5 @@ class Backpropagation implements Training
*/ */
public function train(array $samples, array $targets, float $desiredError = 0.001, int $maxIterations = 10000) public function train(array $samples, array $targets, float $desiredError = 0.001, int $maxIterations = 10000)
{ {
} }
} }