2017-05-17 22:07:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-01-06 12:09:33 +00:00
|
|
|
namespace Phpml\Tests\Classification;
|
2017-05-17 22:07:14 +00:00
|
|
|
|
|
|
|
use Phpml\Classification\MLPClassifier;
|
2017-11-28 07:00:13 +00:00
|
|
|
use Phpml\Exception\InvalidArgumentException;
|
2017-05-23 07:03:05 +00:00
|
|
|
use Phpml\ModelManager;
|
2018-01-09 10:09:59 +00:00
|
|
|
use Phpml\NeuralNetwork\ActivationFunction;
|
|
|
|
use Phpml\NeuralNetwork\ActivationFunction\HyperbolicTangent;
|
|
|
|
use Phpml\NeuralNetwork\ActivationFunction\PReLU;
|
|
|
|
use Phpml\NeuralNetwork\ActivationFunction\Sigmoid;
|
|
|
|
use Phpml\NeuralNetwork\ActivationFunction\ThresholdedReLU;
|
2017-11-06 07:56:37 +00:00
|
|
|
use Phpml\NeuralNetwork\Node\Neuron;
|
2017-05-17 22:07:14 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
class MLPClassifierTest extends TestCase
|
|
|
|
{
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testMLPClassifierLayersInitialization(): void
|
2017-05-17 22:07:14 +00:00
|
|
|
{
|
|
|
|
$mlp = new MLPClassifier(2, [2], [0, 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[1]->getNodes());
|
|
|
|
|
|
|
|
// output layer
|
|
|
|
$this->assertCount(2, $layers[2]->getNodes());
|
|
|
|
$this->assertContainsOnly(Neuron::class, $layers[2]->getNodes());
|
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testSynapsesGeneration(): void
|
2017-05-17 22:07:14 +00:00
|
|
|
{
|
|
|
|
$mlp = new MLPClassifier(2, [2], [0, 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testBackpropagationLearning(): void
|
2017-05-17 22:07:14 +00:00
|
|
|
{
|
|
|
|
// Single layer 2 classes.
|
2018-02-11 21:10:12 +00:00
|
|
|
$network = new MLPClassifier(2, [2], ['a', 'b'], 1000);
|
2017-05-17 22:07:14 +00:00
|
|
|
$network->train(
|
|
|
|
[[1, 0], [0, 1], [1, 1], [0, 0]],
|
|
|
|
['a', 'b', 'a', 'b']
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals('a', $network->predict([1, 0]));
|
|
|
|
$this->assertEquals('b', $network->predict([0, 1]));
|
|
|
|
$this->assertEquals('a', $network->predict([1, 1]));
|
|
|
|
$this->assertEquals('b', $network->predict([0, 0]));
|
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testBackpropagationTrainingReset(): void
|
2017-05-23 07:03:05 +00:00
|
|
|
{
|
|
|
|
// Single layer 2 classes.
|
|
|
|
$network = new MLPClassifier(2, [2], ['a', 'b'], 1000);
|
|
|
|
$network->train(
|
|
|
|
[[1, 0], [0, 1]],
|
|
|
|
['a', 'b']
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals('a', $network->predict([1, 0]));
|
|
|
|
$this->assertEquals('b', $network->predict([0, 1]));
|
|
|
|
|
|
|
|
$network->train(
|
|
|
|
[[1, 0], [0, 1]],
|
|
|
|
['b', 'a']
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals('b', $network->predict([1, 0]));
|
|
|
|
$this->assertEquals('a', $network->predict([0, 1]));
|
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testBackpropagationPartialTraining(): void
|
2017-05-23 07:03:05 +00:00
|
|
|
{
|
|
|
|
// Single layer 2 classes.
|
|
|
|
$network = new MLPClassifier(2, [2], ['a', 'b'], 1000);
|
|
|
|
$network->partialTrain(
|
|
|
|
[[1, 0], [0, 1]],
|
|
|
|
['a', 'b']
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals('a', $network->predict([1, 0]));
|
|
|
|
$this->assertEquals('b', $network->predict([0, 1]));
|
|
|
|
|
|
|
|
$network->partialTrain(
|
|
|
|
[[1, 1], [0, 0]],
|
|
|
|
['a', 'b']
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals('a', $network->predict([1, 0]));
|
|
|
|
$this->assertEquals('b', $network->predict([0, 1]));
|
|
|
|
$this->assertEquals('a', $network->predict([1, 1]));
|
|
|
|
$this->assertEquals('b', $network->predict([0, 0]));
|
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testBackpropagationLearningMultilayer(): void
|
2017-05-17 22:07:14 +00:00
|
|
|
{
|
|
|
|
// Multi-layer 2 classes.
|
2018-02-11 21:10:12 +00:00
|
|
|
$network = new MLPClassifier(5, [3, 2], ['a', 'b', 'c'], 2000);
|
2017-05-17 22:07:14 +00:00
|
|
|
$network->train(
|
|
|
|
[[1, 0, 0, 0, 0], [0, 1, 1, 0, 0], [1, 1, 1, 1, 1], [0, 0, 0, 0, 0]],
|
2017-11-20 22:11:21 +00:00
|
|
|
['a', 'b', 'a', 'c']
|
2017-05-17 22:07:14 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals('a', $network->predict([1, 0, 0, 0, 0]));
|
|
|
|
$this->assertEquals('b', $network->predict([0, 1, 1, 0, 0]));
|
|
|
|
$this->assertEquals('a', $network->predict([1, 1, 1, 1, 1]));
|
2017-11-20 22:11:21 +00:00
|
|
|
$this->assertEquals('c', $network->predict([0, 0, 0, 0, 0]));
|
2017-05-17 22:07:14 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testBackpropagationLearningMulticlass(): void
|
2017-05-17 22:07:14 +00:00
|
|
|
{
|
|
|
|
// Multi-layer more than 2 classes.
|
2018-02-11 21:10:12 +00:00
|
|
|
$network = new MLPClassifier(5, [3, 2], ['a', 'b', 4], 1000);
|
2017-05-17 22:07:14 +00:00
|
|
|
$network->train(
|
|
|
|
[[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 1, 0], [1, 1, 1, 1, 1], [0, 0, 0, 0, 0]],
|
|
|
|
['a', 'b', 'a', 'a', 4]
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals('a', $network->predict([1, 0, 0, 0, 0]));
|
|
|
|
$this->assertEquals('b', $network->predict([0, 1, 0, 0, 0]));
|
|
|
|
$this->assertEquals('a', $network->predict([0, 0, 1, 1, 0]));
|
|
|
|
$this->assertEquals('a', $network->predict([1, 1, 1, 1, 1]));
|
|
|
|
$this->assertEquals(4, $network->predict([0, 0, 0, 0, 0]));
|
|
|
|
}
|
|
|
|
|
2018-01-09 10:09:59 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider activationFunctionsProvider
|
|
|
|
*/
|
|
|
|
public function testBackpropagationActivationFunctions(ActivationFunction $activationFunction): void
|
|
|
|
{
|
2018-02-11 21:10:12 +00:00
|
|
|
$network = new MLPClassifier(5, [3], ['a', 'b'], 1000, $activationFunction);
|
2018-01-09 10:09:59 +00:00
|
|
|
$network->train(
|
|
|
|
[[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 1, 0], [1, 1, 1, 1, 1]],
|
|
|
|
['a', 'b', 'a', 'a']
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals('a', $network->predict([1, 0, 0, 0, 0]));
|
|
|
|
$this->assertEquals('b', $network->predict([0, 1, 0, 0, 0]));
|
|
|
|
$this->assertEquals('a', $network->predict([0, 0, 1, 1, 0]));
|
|
|
|
$this->assertEquals('a', $network->predict([1, 1, 1, 1, 1]));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function activationFunctionsProvider(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[new Sigmoid()],
|
|
|
|
[new HyperbolicTangent()],
|
|
|
|
[new PReLU()],
|
|
|
|
[new ThresholdedReLU()],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testSaveAndRestore(): void
|
2017-05-23 07:03:05 +00:00
|
|
|
{
|
|
|
|
// Instantinate new Percetron trained for OR problem
|
|
|
|
$samples = [[0, 0], [1, 0], [0, 1], [1, 1]];
|
|
|
|
$targets = [0, 1, 1, 1];
|
2018-02-11 21:10:12 +00:00
|
|
|
$classifier = new MLPClassifier(2, [2], [0, 1], 1000);
|
2017-05-23 07:03:05 +00:00
|
|
|
$classifier->train($samples, $targets);
|
|
|
|
$testSamples = [[0, 0], [1, 0], [0, 1], [1, 1]];
|
|
|
|
$predicted = $classifier->predict($testSamples);
|
|
|
|
|
2018-10-15 17:47:42 +00:00
|
|
|
$filename = 'perceptron-test-'.random_int(100, 999).'-'.uniqid('', false);
|
2017-05-23 07:03:05 +00:00
|
|
|
$filepath = tempnam(sys_get_temp_dir(), $filename);
|
|
|
|
$modelManager = new ModelManager();
|
|
|
|
$modelManager->saveToFile($classifier, $filepath);
|
|
|
|
|
|
|
|
$restoredClassifier = $modelManager->restoreFromFile($filepath);
|
|
|
|
$this->assertEquals($classifier, $restoredClassifier);
|
|
|
|
$this->assertEquals($predicted, $restoredClassifier->predict($testSamples));
|
|
|
|
}
|
|
|
|
|
2018-02-25 23:02:04 +00:00
|
|
|
public function testSaveAndRestoreWithPartialTraining(): void
|
|
|
|
{
|
|
|
|
$network = new MLPClassifier(2, [2], ['a', 'b'], 1000);
|
|
|
|
$network->partialTrain(
|
|
|
|
[[1, 0], [0, 1]],
|
|
|
|
['a', 'b']
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals('a', $network->predict([1, 0]));
|
|
|
|
$this->assertEquals('b', $network->predict([0, 1]));
|
|
|
|
|
2018-10-15 17:47:42 +00:00
|
|
|
$filename = 'perceptron-test-'.random_int(100, 999).'-'.uniqid('', false);
|
2018-02-25 23:02:04 +00:00
|
|
|
$filepath = tempnam(sys_get_temp_dir(), $filename);
|
|
|
|
$modelManager = new ModelManager();
|
|
|
|
$modelManager->saveToFile($network, $filepath);
|
|
|
|
|
|
|
|
/** @var MLPClassifier $restoredNetwork */
|
|
|
|
$restoredNetwork = $modelManager->restoreFromFile($filepath);
|
|
|
|
$restoredNetwork->partialTrain(
|
|
|
|
[[1, 1], [0, 0]],
|
|
|
|
['a', 'b']
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals('a', $restoredNetwork->predict([1, 0]));
|
|
|
|
$this->assertEquals('b', $restoredNetwork->predict([0, 1]));
|
|
|
|
$this->assertEquals('a', $restoredNetwork->predict([1, 1]));
|
|
|
|
$this->assertEquals('b', $restoredNetwork->predict([0, 0]));
|
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testThrowExceptionOnInvalidLayersNumber(): void
|
2017-05-17 22:07:14 +00:00
|
|
|
{
|
2017-11-28 07:00:13 +00:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2017-05-17 22:07:14 +00:00
|
|
|
new MLPClassifier(2, [], [0, 1]);
|
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testThrowExceptionOnInvalidPartialTrainingClasses(): void
|
2017-05-23 07:03:05 +00:00
|
|
|
{
|
2017-11-28 07:00:13 +00:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2017-05-23 07:03:05 +00:00
|
|
|
$classifier = new MLPClassifier(2, [2], [0, 1]);
|
|
|
|
$classifier->partialTrain(
|
|
|
|
[[0, 1], [1, 0]],
|
|
|
|
[0, 2],
|
|
|
|
[0, 1, 2]
|
|
|
|
);
|
|
|
|
}
|
2017-08-17 06:50:37 +00:00
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testThrowExceptionOnInvalidClassesNumber(): void
|
2017-05-17 22:07:14 +00:00
|
|
|
{
|
2017-11-28 07:00:13 +00:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2017-05-17 22:07:14 +00:00
|
|
|
new MLPClassifier(2, [2], [0]);
|
|
|
|
}
|
|
|
|
|
2018-10-15 17:47:42 +00:00
|
|
|
public function testOutputWithLabels(): void
|
|
|
|
{
|
|
|
|
$output = (new MLPClassifier(2, [2, 2], ['T', 'F']))->getOutput();
|
|
|
|
|
|
|
|
$this->assertEquals(['T', 'F'], array_keys($output));
|
|
|
|
}
|
|
|
|
|
2017-11-22 21:16:10 +00:00
|
|
|
private function getSynapsesNodes(array $synapses): array
|
2017-05-17 22:07:14 +00:00
|
|
|
{
|
|
|
|
$nodes = [];
|
|
|
|
foreach ($synapses as $synapse) {
|
|
|
|
$nodes[] = $synapse->getNode();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $nodes;
|
|
|
|
}
|
|
|
|
}
|