mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2025-01-24 23:58:24 +00:00
implements and test multilayer perceptron methods
This commit is contained in:
parent
72afeb7040
commit
e5d39ee18a
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace tests\Phpml\NeuralNetwork\Network;
|
||||
|
||||
use Phpml\NeuralNetwork\Network\MultilayerPerceptron;
|
||||
use Phpml\NeuralNetwork\Node\Neuron;
|
||||
|
||||
class MultilayerPerceptronTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testMultilayerPerceptronLayersInitialization()
|
||||
{
|
||||
$mlp = new MultilayerPerceptron([2, 2, 1]);
|
||||
|
||||
$this->assertCount(3, $mlp->getLayers());
|
||||
|
||||
$layers = $mlp->getLayers();
|
||||
|
||||
// input layer
|
||||
$this->assertCount(3, $layers[0]->getNodes());
|
||||
$this->assertNotContainsOnly(Neuron::class, $layers[0]->getNodes());
|
||||
|
||||
// hidden layer
|
||||
$this->assertCount(3, $layers[1]->getNodes());
|
||||
$this->assertNotContainsOnly(Neuron::class, $layers[0]->getNodes());
|
||||
|
||||
// output layer
|
||||
$this->assertCount(1, $layers[2]->getNodes());
|
||||
$this->assertContainsOnly(Neuron::class, $layers[2]->getNodes());
|
||||
}
|
||||
|
||||
public function testSynapsesGeneration()
|
||||
{
|
||||
$mlp = new MultilayerPerceptron([2, 2, 1]);
|
||||
$layers = $mlp->getLayers();
|
||||
|
||||
foreach ($layers[1]->getNodes() as $node) {
|
||||
if ($node instanceof Neuron) {
|
||||
$synapses = $node->getSynapses();
|
||||
$this->assertCount(3, $synapses);
|
||||
|
||||
$synapsesNodes = $this->getSynapsesNodes($synapses);
|
||||
foreach ($layers[0]->getNodes() as $prevNode) {
|
||||
$this->assertContains($prevNode, $synapsesNodes);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $synapses
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getSynapsesNodes(array $synapses): array
|
||||
{
|
||||
$nodes = [];
|
||||
foreach ($synapses as $synapse) {
|
||||
$nodes[] = $synapse->getNode();
|
||||
}
|
||||
|
||||
return $nodes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Phpml\Exception\InvalidArgumentException
|
||||
*/
|
||||
public function testThrowExceptionOnInvalidLayersNumber()
|
||||
{
|
||||
new MultilayerPerceptron([2]);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user