2016-08-07 21:41:08 +00:00
|
|
|
<?php
|
|
|
|
|
2016-11-20 21:53:17 +00:00
|
|
|
declare(strict_types=1);
|
2016-08-07 21:41:08 +00:00
|
|
|
|
2018-01-06 12:09:33 +00:00
|
|
|
namespace Phpml\Tests\NeuralNetwork\Network;
|
2016-08-07 21:41:08 +00:00
|
|
|
|
2018-02-01 22:15:36 +00:00
|
|
|
use Phpml\NeuralNetwork\ActivationFunction;
|
2016-08-07 21:41:08 +00:00
|
|
|
use Phpml\NeuralNetwork\Layer;
|
|
|
|
use Phpml\NeuralNetwork\Network\LayeredNetwork;
|
|
|
|
use Phpml\NeuralNetwork\Node\Input;
|
2019-04-10 18:42:59 +00:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2017-02-03 11:58:25 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-08-07 21:41:08 +00:00
|
|
|
|
2017-02-03 11:58:25 +00:00
|
|
|
class LayeredNetworkTest extends TestCase
|
2016-08-07 21:41:08 +00:00
|
|
|
{
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testLayersSettersAndGetters(): void
|
2016-08-07 21:41:08 +00:00
|
|
|
{
|
|
|
|
$network = $this->getLayeredNetworkMock();
|
|
|
|
|
|
|
|
$network->addLayer($layer1 = new Layer());
|
|
|
|
$network->addLayer($layer2 = new Layer());
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertEquals([$layer1, $layer2], $network->getLayers());
|
2016-08-07 21:41:08 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testGetLastLayerAsOutputLayer(): void
|
2016-08-07 21:41:08 +00:00
|
|
|
{
|
|
|
|
$network = $this->getLayeredNetworkMock();
|
|
|
|
$network->addLayer($layer1 = new Layer());
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertEquals($layer1, $network->getOutputLayer());
|
2016-08-07 21:41:08 +00:00
|
|
|
|
|
|
|
$network->addLayer($layer2 = new Layer());
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertEquals($layer2, $network->getOutputLayer());
|
2016-08-07 21:41:08 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testSetInputAndGetOutput(): void
|
2016-08-07 21:41:08 +00:00
|
|
|
{
|
|
|
|
$network = $this->getLayeredNetworkMock();
|
|
|
|
$network->addLayer(new Layer(2, Input::class));
|
|
|
|
|
|
|
|
$network->setInput($input = [34, 43]);
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertEquals($input, $network->getOutput());
|
2016-08-07 21:41:08 +00:00
|
|
|
|
|
|
|
$network->addLayer(new Layer(1));
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertEquals([0.5], $network->getOutput());
|
2016-08-07 21:41:08 +00:00
|
|
|
}
|
|
|
|
|
2018-02-01 22:15:36 +00:00
|
|
|
public function testSetInputAndGetOutputWithCustomActivationFunctions(): void
|
|
|
|
{
|
|
|
|
$network = $this->getLayeredNetworkMock();
|
|
|
|
$network->addLayer(new Layer(2, Input::class, $this->getActivationFunctionMock()));
|
|
|
|
|
|
|
|
$network->setInput($input = [34, 43]);
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertEquals($input, $network->getOutput());
|
2018-02-01 22:15:36 +00:00
|
|
|
}
|
|
|
|
|
2018-01-06 12:09:33 +00:00
|
|
|
/**
|
2019-04-10 18:42:59 +00:00
|
|
|
* @return LayeredNetwork|MockObject
|
2018-01-06 12:09:33 +00:00
|
|
|
*/
|
|
|
|
private function getLayeredNetworkMock()
|
2016-08-07 21:41:08 +00:00
|
|
|
{
|
|
|
|
return $this->getMockForAbstractClass(LayeredNetwork::class);
|
|
|
|
}
|
2018-02-01 22:15:36 +00:00
|
|
|
|
|
|
|
/**
|
2019-04-10 18:42:59 +00:00
|
|
|
* @return ActivationFunction|MockObject
|
2018-02-01 22:15:36 +00:00
|
|
|
*/
|
|
|
|
private function getActivationFunctionMock()
|
|
|
|
{
|
|
|
|
return $this->getMockForAbstractClass(ActivationFunction::class);
|
|
|
|
}
|
2016-08-07 21:41:08 +00:00
|
|
|
}
|