forked from jorgecasas/php-ml
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Layer, Input and Bias for neutal network
- Loading branch information
Showing
11 changed files
with
221 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters