2016-08-02 18:30:20 +00:00
|
|
|
<?php
|
|
|
|
|
2016-11-20 21:53:17 +00:00
|
|
|
declare(strict_types=1);
|
2016-08-02 18:30:20 +00:00
|
|
|
|
2018-01-06 12:09:33 +00:00
|
|
|
namespace Phpml\Tests\NeuralNetwork\Node\Neuron;
|
2016-08-02 18:30:20 +00:00
|
|
|
|
|
|
|
use Phpml\NeuralNetwork\Node\Neuron;
|
2017-11-06 07:56:37 +00:00
|
|
|
use Phpml\NeuralNetwork\Node\Neuron\Synapse;
|
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-02 18:30:20 +00:00
|
|
|
|
2017-02-03 11:58:25 +00:00
|
|
|
class SynapseTest extends TestCase
|
2016-08-02 18:30:20 +00:00
|
|
|
{
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testSynapseInitialization(): void
|
2016-08-02 18:30:20 +00:00
|
|
|
{
|
|
|
|
$node = $this->getNodeMock($nodeOutput = 0.5);
|
|
|
|
|
|
|
|
$synapse = new Synapse($node, $weight = 0.75);
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertEquals($node, $synapse->getNode());
|
|
|
|
self::assertEquals($weight, $synapse->getWeight());
|
|
|
|
self::assertEquals($weight * $nodeOutput, $synapse->getOutput());
|
2016-08-02 18:30:20 +00:00
|
|
|
|
|
|
|
$synapse = new Synapse($node);
|
2018-10-28 06:44:52 +00:00
|
|
|
$weight = $synapse->getWeight();
|
2016-08-02 18:30:20 +00:00
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertTrue($weight === -1. || $weight === 1.);
|
2016-08-02 18:30:20 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testSynapseWeightChange(): void
|
2016-08-02 18:30:20 +00:00
|
|
|
{
|
|
|
|
$node = $this->getNodeMock();
|
|
|
|
$synapse = new Synapse($node, $weight = 0.75);
|
|
|
|
$synapse->changeWeight(1.0);
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertEquals(1.75, $synapse->getWeight());
|
2016-08-02 18:30:20 +00:00
|
|
|
|
|
|
|
$synapse->changeWeight(-2.0);
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertEquals(-0.25, $synapse->getWeight());
|
2016-08-02 18:30:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-10 18:42:59 +00:00
|
|
|
* @return Neuron|MockObject
|
2016-08-02 18:30:20 +00:00
|
|
|
*/
|
2019-11-08 14:28:42 +00:00
|
|
|
private function getNodeMock(float $output = 1.)
|
2016-08-02 18:30:20 +00:00
|
|
|
{
|
2016-09-21 19:46:16 +00:00
|
|
|
$node = $this->getMockBuilder(Neuron::class)->getMock();
|
|
|
|
$node->method('getOutput')->willReturn($output);
|
2016-08-02 18:30:20 +00:00
|
|
|
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
}
|