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
|
|
|
|
2018-01-06 12:09:33 +00:00
|
|
|
namespace Phpml\Tests\NeuralNetwork;
|
2016-08-05 08:20:31 +00:00
|
|
|
|
2017-11-28 07:00:13 +00:00
|
|
|
use Phpml\Exception\InvalidArgumentException;
|
2016-08-05 08:20:31 +00:00
|
|
|
use Phpml\NeuralNetwork\Layer;
|
2017-11-06 07:56:37 +00:00
|
|
|
use Phpml\NeuralNetwork\Node\Bias;
|
2016-08-05 08:20:31 +00:00
|
|
|
use Phpml\NeuralNetwork\Node\Neuron;
|
2017-02-03 11:58:25 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2017-11-22 21:16:10 +00:00
|
|
|
use stdClass;
|
2016-08-05 08:20:31 +00:00
|
|
|
|
2017-02-03 11:58:25 +00:00
|
|
|
class LayerTest extends TestCase
|
2016-08-05 08:20:31 +00:00
|
|
|
{
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testLayerInitialization(): void
|
2016-08-05 08:20:31 +00:00
|
|
|
{
|
|
|
|
$layer = new Layer();
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertEquals([], $layer->getNodes());
|
2016-08-05 08:20:31 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testLayerInitializationWithDefaultNodesType(): void
|
2016-08-05 08:20:31 +00:00
|
|
|
{
|
|
|
|
$layer = new Layer($number = 5);
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertCount($number, $layer->getNodes());
|
2016-08-05 08:20:31 +00:00
|
|
|
foreach ($layer->getNodes() as $node) {
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertInstanceOf(Neuron::class, $node);
|
2016-08-05 08:20:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testLayerInitializationWithExplicitNodesType(): void
|
2016-08-05 08:20:31 +00:00
|
|
|
{
|
|
|
|
$layer = new Layer($number = 5, $class = Bias::class);
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertCount($number, $layer->getNodes());
|
2016-08-05 08:20:31 +00:00
|
|
|
foreach ($layer->getNodes() as $node) {
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertInstanceOf($class, $node);
|
2016-08-05 08:20:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testThrowExceptionOnInvalidNodeClass(): void
|
2016-08-05 08:20:31 +00:00
|
|
|
{
|
2017-11-28 07:00:13 +00:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2017-11-22 21:16:10 +00:00
|
|
|
new Layer(1, stdClass::class);
|
2016-08-05 08:20:31 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testAddNodesToLayer(): void
|
2016-08-05 08:20:31 +00:00
|
|
|
{
|
|
|
|
$layer = new Layer();
|
|
|
|
$layer->addNode($node1 = new Neuron());
|
|
|
|
$layer->addNode($node2 = new Neuron());
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertEquals([$node1, $node2], $layer->getNodes());
|
2016-08-05 08:20:31 +00:00
|
|
|
}
|
|
|
|
}
|