Skip to content

Commit

Permalink
add Layer, Input and Bias for neutal network
Browse files Browse the repository at this point in the history
  • Loading branch information
akondas committed Aug 5, 2016
1 parent 7062ee2 commit 95b29d4
Show file tree
Hide file tree
Showing 11 changed files with 221 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/Phpml/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,12 @@ public static function invalidStopWordsLanguage(string $language)
{
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');
}
}
49 changes: 49 additions & 0 deletions src/Phpml/NeuralNetwork/Layer.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
18 changes: 18 additions & 0 deletions src/Phpml/NeuralNetwork/Node/Bias.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
39 changes: 39 additions & 0 deletions src/Phpml/NeuralNetwork/Node/Input.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
1 change: 1 addition & 0 deletions src/Phpml/NeuralNetwork/Node/Neuron.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Phpml\NeuralNetwork\Node;

use Phpml\NeuralNetwork\ActivationFunction;
use Phpml\NeuralNetwork\Node\Neuron\Synapse;
use Phpml\NeuralNetwork\Node;

class Neuron implements Node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 56 additions & 0 deletions tests/Phpml/NeuralNetwork/LayerTest.php
Original file line number Diff line number Diff line change
@@ -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());
}
}
17 changes: 17 additions & 0 deletions tests/Phpml/NeuralNetwork/Node/BiasTest.php
Original file line number Diff line number Diff line change
@@ -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());
}
}
27 changes: 27 additions & 0 deletions tests/Phpml/NeuralNetwork/Node/InputTest.php
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Phpml/NeuralNetwork/Node/NeuronTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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
{
Expand Down Expand Up @@ -53,7 +53,7 @@ public function testNeuronRefresh()
/**
* @param int $output
*
* @return \PHPUnit_Framework_MockObject_MockObject
* @return Synapse|\PHPUnit_Framework_MockObject_MockObject
*/
private function getSynapseMock($output = 2)
{
Expand Down

0 comments on commit 95b29d4

Please sign in to comment.