2016-08-05 08:20:31 +00:00
|
|
|
<?php
|
|
|
|
|
2016-11-20 21:53:17 +00:00
|
|
|
declare(strict_types=1);
|
2016-08-05 08:20:31 +00:00
|
|
|
|
|
|
|
namespace Phpml\NeuralNetwork;
|
|
|
|
|
|
|
|
use Phpml\Exception\InvalidArgumentException;
|
|
|
|
use Phpml\NeuralNetwork\Node\Neuron;
|
|
|
|
|
|
|
|
class Layer
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var Node[]
|
|
|
|
*/
|
|
|
|
private $nodes = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
2016-08-11 11:21:22 +00:00
|
|
|
public function __construct(int $nodesNumber = 0, string $nodeClass = Neuron::class, ActivationFunction $activationFunction = null)
|
2016-08-05 08:20:31 +00:00
|
|
|
{
|
|
|
|
if (!in_array(Node::class, class_implements($nodeClass))) {
|
|
|
|
throw InvalidArgumentException::invalidLayerNodeClass();
|
|
|
|
}
|
|
|
|
|
|
|
|
for ($i = 0; $i < $nodesNumber; ++$i) {
|
2016-08-11 11:21:22 +00:00
|
|
|
$this->nodes[] = $this->createNode($nodeClass, $activationFunction);
|
2016-08-05 08:20:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-11 11:21:22 +00:00
|
|
|
/**
|
|
|
|
* @param ActivationFunction|null $activationFunction
|
|
|
|
*
|
|
|
|
* @return Neuron
|
|
|
|
*/
|
|
|
|
private function createNode(string $nodeClass, ActivationFunction $activationFunction = null)
|
|
|
|
{
|
|
|
|
if (Neuron::class == $nodeClass) {
|
|
|
|
return new Neuron($activationFunction);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new $nodeClass();
|
|
|
|
}
|
|
|
|
|
2016-08-05 08:20:31 +00:00
|
|
|
public function addNode(Node $node)
|
|
|
|
{
|
|
|
|
$this->nodes[] = $node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Node[]
|
|
|
|
*/
|
2017-11-06 07:56:37 +00:00
|
|
|
public function getNodes() : array
|
2016-08-05 08:20:31 +00:00
|
|
|
{
|
|
|
|
return $this->nodes;
|
|
|
|
}
|
|
|
|
}
|