2017-12-05 20:09:06 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-01-06 20:25:47 +00:00
|
|
|
namespace Phpml\Tests\NeuralNetwork\Network;
|
2017-12-05 20:09:06 +00:00
|
|
|
|
2018-10-15 17:47:42 +00:00
|
|
|
use Phpml\Exception\InvalidArgumentException;
|
2018-02-01 22:15:36 +00:00
|
|
|
use Phpml\NeuralNetwork\ActivationFunction;
|
|
|
|
use Phpml\NeuralNetwork\Layer;
|
2017-12-05 20:09:06 +00:00
|
|
|
use Phpml\NeuralNetwork\Network\MultilayerPerceptron;
|
2018-02-01 22:15:36 +00:00
|
|
|
use Phpml\NeuralNetwork\Node\Neuron;
|
2017-12-05 20:09:06 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2018-02-01 22:15:36 +00:00
|
|
|
use PHPUnit_Framework_MockObject_MockObject;
|
2017-12-05 20:09:06 +00:00
|
|
|
|
|
|
|
class MultilayerPerceptronTest extends TestCase
|
|
|
|
{
|
2018-10-15 17:47:42 +00:00
|
|
|
public function testThrowExceptionWhenHiddenLayersAreEmpty(): void
|
|
|
|
{
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$this->expectExceptionMessage('Provide at least 1 hidden layer');
|
|
|
|
|
|
|
|
$this->getMockForAbstractClass(
|
|
|
|
MultilayerPerceptron::class,
|
|
|
|
[5, [], [0, 1], 1000, null, 0.42]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testThrowExceptionWhenThereIsOnlyOneClass(): void
|
|
|
|
{
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$this->expectExceptionMessage('Provide at least 2 different classes');
|
|
|
|
|
|
|
|
$this->getMockForAbstractClass(
|
|
|
|
MultilayerPerceptron::class,
|
|
|
|
[5, [3], [0], 1000, null, 0.42]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testThrowExceptionWhenClassesAreNotUnique(): void
|
|
|
|
{
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$this->expectExceptionMessage('Classes must be unique');
|
|
|
|
|
|
|
|
$this->getMockForAbstractClass(
|
|
|
|
MultilayerPerceptron::class,
|
|
|
|
[5, [3], [0, 1, 2, 3, 1], 1000, null, 0.42]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-12-05 20:09:06 +00:00
|
|
|
public function testLearningRateSetter(): void
|
|
|
|
{
|
2018-01-06 20:25:47 +00:00
|
|
|
/** @var MultilayerPerceptron $mlp */
|
2017-12-05 20:09:06 +00:00
|
|
|
$mlp = $this->getMockForAbstractClass(
|
|
|
|
MultilayerPerceptron::class,
|
|
|
|
[5, [3], [0, 1], 1000, null, 0.42]
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(0.42, $this->readAttribute($mlp, 'learningRate'));
|
|
|
|
$backprop = $this->readAttribute($mlp, 'backpropagation');
|
|
|
|
$this->assertEquals(0.42, $this->readAttribute($backprop, 'learningRate'));
|
|
|
|
|
|
|
|
$mlp->setLearningRate(0.24);
|
|
|
|
$this->assertEquals(0.24, $this->readAttribute($mlp, 'learningRate'));
|
|
|
|
$backprop = $this->readAttribute($mlp, 'backpropagation');
|
|
|
|
$this->assertEquals(0.24, $this->readAttribute($backprop, 'learningRate'));
|
|
|
|
}
|
2018-02-01 22:15:36 +00:00
|
|
|
|
|
|
|
public function testLearningRateSetterWithCustomActivationFunctions(): void
|
|
|
|
{
|
|
|
|
$activation_function = $this->getActivationFunctionMock();
|
|
|
|
|
|
|
|
/** @var MultilayerPerceptron $mlp */
|
|
|
|
$mlp = $this->getMockForAbstractClass(
|
|
|
|
MultilayerPerceptron::class,
|
|
|
|
[5, [[3, $activation_function], [5, $activation_function]], [0, 1], 1000, null, 0.42]
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(0.42, $this->readAttribute($mlp, 'learningRate'));
|
|
|
|
$backprop = $this->readAttribute($mlp, 'backpropagation');
|
|
|
|
$this->assertEquals(0.42, $this->readAttribute($backprop, 'learningRate'));
|
|
|
|
|
|
|
|
$mlp->setLearningRate(0.24);
|
|
|
|
$this->assertEquals(0.24, $this->readAttribute($mlp, 'learningRate'));
|
|
|
|
$backprop = $this->readAttribute($mlp, 'backpropagation');
|
|
|
|
$this->assertEquals(0.24, $this->readAttribute($backprop, 'learningRate'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLearningRateSetterWithLayerObject(): void
|
|
|
|
{
|
|
|
|
$activation_function = $this->getActivationFunctionMock();
|
|
|
|
|
|
|
|
/** @var MultilayerPerceptron $mlp */
|
|
|
|
$mlp = $this->getMockForAbstractClass(
|
|
|
|
MultilayerPerceptron::class,
|
|
|
|
[5, [new Layer(3, Neuron::class, $activation_function), new Layer(5, Neuron::class, $activation_function)], [0, 1], 1000, null, 0.42]
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(0.42, $this->readAttribute($mlp, 'learningRate'));
|
|
|
|
$backprop = $this->readAttribute($mlp, 'backpropagation');
|
|
|
|
$this->assertEquals(0.42, $this->readAttribute($backprop, 'learningRate'));
|
|
|
|
|
|
|
|
$mlp->setLearningRate(0.24);
|
|
|
|
$this->assertEquals(0.24, $this->readAttribute($mlp, 'learningRate'));
|
|
|
|
$backprop = $this->readAttribute($mlp, 'backpropagation');
|
|
|
|
$this->assertEquals(0.24, $this->readAttribute($backprop, 'learningRate'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ActivationFunction|PHPUnit_Framework_MockObject_MockObject
|
|
|
|
*/
|
|
|
|
private function getActivationFunctionMock()
|
|
|
|
{
|
|
|
|
return $this->getMockForAbstractClass(ActivationFunction::class);
|
|
|
|
}
|
2017-12-05 20:09:06 +00:00
|
|
|
}
|