MultilayerPerceptronTest.php 4.23 KB
Newer Older
1
2
3
4
<?php

declare(strict_types=1);

5
namespace Phpml\Tests\NeuralNetwork\Network;
6

7
use Phpml\Exception\InvalidArgumentException;
8
9
use Phpml\NeuralNetwork\ActivationFunction;
use Phpml\NeuralNetwork\Layer;
10
use Phpml\NeuralNetwork\Network\MultilayerPerceptron;
11
use Phpml\NeuralNetwork\Node\Neuron;
12
use PHPUnit\Framework\TestCase;
13
use PHPUnit_Framework_MockObject_MockObject;
14
15
16

class MultilayerPerceptronTest extends TestCase
{
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
    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]
        );
    }

50
51
    public function testLearningRateSetter(): void
    {
52
        /** @var MultilayerPerceptron $mlp */
53
54
55
56
57
58
59
60
61
62
63
64
65
66
        $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'));
    }
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

    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);
    }
115
}