mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2024-11-17 18:55:12 +00:00
4af8449b1c
* MultilayerPerceptron interface changes - Signature closer to other algorithms - New predict method - Remove desired error - Move maxIterations to constructor * MLP tests for multiple hidden layers and multi-class * Update all MLP-related tests * coding style fixes * Backpropagation included in multilayer-perceptron
67 lines
1.8 KiB
PHP
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()
|
|
{
|
|
$neuron = new Neuron();
|
|
|
|
$this->assertEquals([], $neuron->getSynapses());
|
|
$this->assertEquals(0.5, $neuron->getOutput());
|
|
}
|
|
|
|
public function testNeuronActivationFunction()
|
|
{
|
|
$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()
|
|
{
|
|
$neuron = new Neuron();
|
|
$neuron->addSynapse($synapse = $this->getSynapseMock());
|
|
|
|
$this->assertEquals([$synapse], $neuron->getSynapses());
|
|
$this->assertEquals(0.88, $neuron->getOutput(), '', 0.01);
|
|
}
|
|
|
|
public function testNeuronRefresh()
|
|
{
|
|
$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;
|
|
}
|
|
}
|