add Layer, Input and Bias for neutal network

This commit is contained in:
Arkadiusz Kondas 2016-08-05 10:20:31 +02:00
parent 7062ee29e1
commit 95b29d40b1
11 changed files with 221 additions and 6 deletions

View File

@ -73,4 +73,12 @@ class InvalidArgumentException extends \Exception
{
return new self(sprintf('Can\'t find %s language for StopWords', $language));
}
/**
* @return InvalidArgumentException
*/
public static function invalidLayerNodeClass()
{
return new self('Layer node class must implement Node interface');
}
}

View File

@ -0,0 +1,49 @@
<?php
declare (strict_types = 1);
namespace Phpml\NeuralNetwork;
use Phpml\Exception\InvalidArgumentException;
use Phpml\NeuralNetwork\Node\Neuron;
class Layer
{
/**
* @var Node[]
*/
private $nodes = [];
/**
* @param int $nodesNumber
* @param string $nodeClass
*
* @throws InvalidArgumentException
*/
public function __construct(int $nodesNumber = 0, string $nodeClass = Neuron::class)
{
if (!in_array(Node::class, class_implements($nodeClass))) {
throw InvalidArgumentException::invalidLayerNodeClass();
}
for ($i = 0; $i < $nodesNumber; ++$i) {
$this->nodes[] = new $nodeClass();
}
}
/**
* @param Node $node
*/
public function addNode(Node $node)
{
$this->nodes[] = $node;
}
/**
* @return Node[]
*/
public function getNodes()
{
return $this->nodes;
}
}

View File

@ -0,0 +1,18 @@
<?php
declare (strict_types = 1);
namespace Phpml\NeuralNetwork\Node;
use Phpml\NeuralNetwork\Node;
class Bias implements Node
{
/**
* @return float
*/
public function getOutput(): float
{
return 1.0;
}
}

View File

@ -0,0 +1,39 @@
<?php
declare (strict_types = 1);
namespace Phpml\NeuralNetwork\Node;
use Phpml\NeuralNetwork\Node;
class Input implements Node
{
/**
* @var float
*/
private $input;
/**
* @param float $input
*/
public function __construct(float $input = 0.0)
{
$this->input = $input;
}
/**
* @return float
*/
public function getOutput(): float
{
return $this->input;
}
/**
* @param float $input
*/
public function setInput(float $input)
{
$this->input = $input;
}
}

View File

@ -5,6 +5,7 @@ declare (strict_types = 1);
namespace Phpml\NeuralNetwork\Node;
use Phpml\NeuralNetwork\ActivationFunction;
use Phpml\NeuralNetwork\Node\Neuron\Synapse;
use Phpml\NeuralNetwork\Node;
class Neuron implements Node

View File

@ -2,11 +2,11 @@
declare (strict_types = 1);
namespace Phpml\NeuralNetwork\Node;
namespace Phpml\NeuralNetwork\Node\Neuron;
use Phpml\NeuralNetwork\Node;
class Synapse implements Node
class Synapse
{
/**
* @var float

View File

@ -0,0 +1,56 @@
<?php
declare (strict_types = 1);
namespace tests\Phpml\NeuralNetwork;
use Phpml\NeuralNetwork\Node\Bias;
use Phpml\NeuralNetwork\Layer;
use Phpml\NeuralNetwork\Node\Neuron;
class LayerTest extends \PHPUnit_Framework_TestCase
{
public function testLayerInitialization()
{
$layer = new Layer();
$this->assertEquals([], $layer->getNodes());
}
public function testLayerInitializationWithDefaultNodesType()
{
$layer = new Layer($number = 5);
$this->assertCount($number, $layer->getNodes());
foreach ($layer->getNodes() as $node) {
$this->assertInstanceOf(Neuron::class, $node);
}
}
public function testLayerInitializationWithExplicitNodesType()
{
$layer = new Layer($number = 5, $class = Bias::class);
$this->assertCount($number, $layer->getNodes());
foreach ($layer->getNodes() as $node) {
$this->assertInstanceOf($class, $node);
}
}
/**
* @expectedException \Phpml\Exception\InvalidArgumentException
*/
public function testThrowExceptionOnInvalidNodeClass()
{
new Layer(1, \stdClass::class);
}
public function testAddNodesToLayer()
{
$layer = new Layer();
$layer->addNode($node1 = new Neuron());
$layer->addNode($node2 = new Neuron());
$this->assertEquals([$node1, $node2], $layer->getNodes());
}
}

View File

@ -0,0 +1,17 @@
<?php
declare (strict_types = 1);
namespace tests\Phpml\NeuralNetwork\Node;
use Phpml\NeuralNetwork\Node\Bias;
class BiasTest extends \PHPUnit_Framework_TestCase
{
public function testBiasOutput()
{
$bias = new Bias();
$this->assertEquals(1.0, $bias->getOutput());
}
}

View File

@ -0,0 +1,27 @@
<?php
declare (strict_types = 1);
namespace tests\Phpml\NeuralNetwork\Node;
use Phpml\NeuralNetwork\Node\Input;
class InputTest extends \PHPUnit_Framework_TestCase
{
public function testInputInitialization()
{
$input = new Input();
$this->assertEquals(0.0, $input->getOutput());
$input = new Input($value = 9.6);
$this->assertEquals($value, $input->getOutput());
}
public function testSetInput()
{
$input = new Input();
$input->setInput($value = 6.9);
$this->assertEquals($value, $input->getOutput());
}
}

View File

@ -2,10 +2,10 @@
declare (strict_types = 1);
namespace tests\Phpml\NeuralNetwork\Node;
namespace tests\Phpml\NeuralNetwork\Node\Neuron;
use Phpml\NeuralNetwork\Node\Neuron\Synapse;
use Phpml\NeuralNetwork\Node\Neuron;
use Phpml\NeuralNetwork\Node\Synapse;
class SynapseTest extends \PHPUnit_Framework_TestCase
{

View File

@ -6,7 +6,7 @@ namespace tests\Phpml\NeuralNetwork\Node;
use Phpml\NeuralNetwork\ActivationFunction\BinaryStep;
use Phpml\NeuralNetwork\Node\Neuron;
use Phpml\NeuralNetwork\Node\Synapse;
use Phpml\NeuralNetwork\Node\Neuron\Synapse;
class NeuronTest extends \PHPUnit_Framework_TestCase
{
@ -53,7 +53,7 @@ class NeuronTest extends \PHPUnit_Framework_TestCase
/**
* @param int $output
*
* @return \PHPUnit_Framework_MockObject_MockObject
* @return Synapse|\PHPUnit_Framework_MockObject_MockObject
*/
private function getSynapseMock($output = 2)
{