mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2024-11-22 04:55:10 +00:00
add Layer, Input and Bias for neutal network
This commit is contained in:
parent
7062ee29e1
commit
95b29d40b1
@ -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');
|
||||
}
|
||||
}
|
||||
|
49
src/Phpml/NeuralNetwork/Layer.php
Normal file
49
src/Phpml/NeuralNetwork/Layer.php
Normal 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;
|
||||
}
|
||||
}
|
18
src/Phpml/NeuralNetwork/Node/Bias.php
Normal file
18
src/Phpml/NeuralNetwork/Node/Bias.php
Normal 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;
|
||||
}
|
||||
}
|
39
src/Phpml/NeuralNetwork/Node/Input.php
Normal file
39
src/Phpml/NeuralNetwork/Node/Input.php
Normal 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;
|
||||
}
|
||||
}
|
@ -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
|
||||
|
@ -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
|
56
tests/Phpml/NeuralNetwork/LayerTest.php
Normal file
56
tests/Phpml/NeuralNetwork/LayerTest.php
Normal 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());
|
||||
}
|
||||
}
|
17
tests/Phpml/NeuralNetwork/Node/BiasTest.php
Normal file
17
tests/Phpml/NeuralNetwork/Node/BiasTest.php
Normal 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());
|
||||
}
|
||||
}
|
27
tests/Phpml/NeuralNetwork/Node/InputTest.php
Normal file
27
tests/Phpml/NeuralNetwork/Node/InputTest.php
Normal 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());
|
||||
}
|
||||
}
|
@ -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
|
||||
{
|
@ -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)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user