php-ml/tests/Phpml/NeuralNetwork/Node/NeuronTest.php
Tomáš Votruba 653c7c772d Upgrade to PHP 7.1 (#150)
* upgrade to PHP 7.1

* bump travis and composer to PHP 7.1

* fix tests
2017-11-14 21:21:23 +01:00

67 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace tests\Phpml\NeuralNetwork\Node;
use Phpml\NeuralNetwork\ActivationFunction\BinaryStep;
use Phpml\NeuralNetwork\Node\Neuron;
use Phpml\NeuralNetwork\Node\Neuron\Synapse;
use PHPUnit\Framework\TestCase;
class NeuronTest extends TestCase
{
public function testNeuronInitialization(): void
{
$neuron = new Neuron();
$this->assertEquals([], $neuron->getSynapses());
$this->assertEquals(0.5, $neuron->getOutput());
}
public function testNeuronActivationFunction(): void
{
$activationFunction = $this->getMockBuilder(BinaryStep::class)->getMock();
$activationFunction->method('compute')->with(0)->willReturn($output = 0.69);
$neuron = new Neuron($activationFunction);
$this->assertEquals($output, $neuron->getOutput());
}
public function testNeuronWithSynapse(): void
{
$neuron = new Neuron();
$neuron->addSynapse($synapse = $this->getSynapseMock());
$this->assertEquals([$synapse], $neuron->getSynapses());
$this->assertEquals(0.88, $neuron->getOutput(), '', 0.01);
}
public function testNeuronRefresh(): void
{
$neuron = new Neuron();
$neuron->getOutput();
$neuron->addSynapse($this->getSynapseMock());
$this->assertEquals(0.5, $neuron->getOutput(), '', 0.01);
$neuron->reset();
$this->assertEquals(0.88, $neuron->getOutput(), '', 0.01);
}
/**
* @param int $output
*
* @return Synapse|\PHPUnit_Framework_MockObject_MockObject
*/
private function getSynapseMock($output = 2)
{
$synapse = $this->getMockBuilder(Synapse::class)->disableOriginalConstructor()->getMock();
$synapse->method('getOutput')->willReturn($output);
return $synapse;
}
}