Passed
Push — master ( 8daed2...c32bf3 )
by Arkadiusz
02:53
created

MultilayerPerceptron::addNeuronLayers()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 5
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Phpml\NeuralNetwork\Network;
6
7
use Phpml\Estimator;
8
use Phpml\Exception\InvalidArgumentException;
9
use Phpml\Helper\Predictable;
10
use Phpml\IncrementalEstimator;
11
use Phpml\NeuralNetwork\ActivationFunction;
12
use Phpml\NeuralNetwork\ActivationFunction\Sigmoid;
13
use Phpml\NeuralNetwork\Layer;
14
use Phpml\NeuralNetwork\Node\Bias;
15
use Phpml\NeuralNetwork\Node\Input;
16
use Phpml\NeuralNetwork\Node\Neuron;
17
use Phpml\NeuralNetwork\Node\Neuron\Synapse;
18
use Phpml\NeuralNetwork\Training\Backpropagation;
19
20
abstract class MultilayerPerceptron extends LayeredNetwork implements Estimator, IncrementalEstimator
21
{
22
    use Predictable;
23
24
    /**
25
     * @var array
26
     */
27
    protected $classes = [];
28
29
    /**
30
     * @var ActivationFunction|null
31
     */
32
    protected $activationFunction;
33
34
    /**
35
     * @var Backpropagation
36
     */
37
    protected $backpropagation;
38
39
    /**
40
     * @var int
41
     */
42
    private $inputLayerFeatures;
43
44
    /**
45
     * @var array
46
     */
47
    private $hiddenLayers = [];
48
49
    /**
50
     * @var float
51
     */
52
    private $learningRate;
53
54
    /**
55
     * @var int
56
     */
57
    private $iterations;
58
59
    /**
60
     * @throws InvalidArgumentException
61
     */
62
    public function __construct(int $inputLayerFeatures, array $hiddenLayers, array $classes, int $iterations = 10000, ?ActivationFunction $activationFunction = null, float $learningRate = 1)
63
    {
64
        if (empty($hiddenLayers)) {
65
            throw InvalidArgumentException::invalidLayersNumber();
66
        }
67
68
        if (count($classes) < 2) {
69
            throw InvalidArgumentException::invalidClassesNumber();
70
        }
71
72
        $this->classes = array_values($classes);
73
        $this->iterations = $iterations;
74
        $this->inputLayerFeatures = $inputLayerFeatures;
75
        $this->hiddenLayers = $hiddenLayers;
76
        $this->activationFunction = $activationFunction;
77
        $this->learningRate = $learningRate;
0 ignored issues
show
Documentation Bug introduced by
It seems like $learningRate can also be of type integer. However, the property $learningRate is declared as type double. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
78
79
        $this->initNetwork();
80
    }
81
82
    public function train(array $samples, array $targets): void
83
    {
84
        $this->reset();
85
        $this->initNetwork();
86
        $this->partialTrain($samples, $targets, $this->classes);
87
    }
88
89
    /**
90
     * @throws InvalidArgumentException
91
     */
92
    public function partialTrain(array $samples, array $targets, array $classes = []): void
93
    {
94
        if (!empty($classes) && array_values($classes) !== $this->classes) {
95
            // We require the list of classes in the constructor.
96
            throw InvalidArgumentException::inconsistentClasses();
97
        }
98
99
        for ($i = 0; $i < $this->iterations; ++$i) {
100
            $this->trainSamples($samples, $targets);
101
        }
102
    }
103
104
    public function setLearningRate(float $learningRate): void
105
    {
106
        $this->learningRate = $learningRate;
107
        $this->backpropagation->setLearningRate($this->learningRate);
108
    }
109
110
    /**
111
     * @param mixed $target
112
     */
113
    abstract protected function trainSample(array $sample, $target);
114
115
    /**
116
     * @return mixed
117
     */
118
    abstract protected function predictSample(array $sample);
119
120
    protected function reset(): void
121
    {
122
        $this->removeLayers();
123
    }
124
125
    private function initNetwork(): void
126
    {
127
        $this->addInputLayer($this->inputLayerFeatures);
128
        $this->addNeuronLayers($this->hiddenLayers, $this->activationFunction);
129
130
        // Sigmoid function for the output layer as we want a value from 0 to 1.
131
        $sigmoid = new Sigmoid();
132
        $this->addNeuronLayers([count($this->classes)], $sigmoid);
133
134
        $this->addBiasNodes();
135
        $this->generateSynapses();
136
137
        $this->backpropagation = new Backpropagation($this->learningRate);
138
    }
139
140
    private function addInputLayer(int $nodes): void
141
    {
142
        $this->addLayer(new Layer($nodes, Input::class));
143
    }
144
145
    private function addNeuronLayers(array $layers, ?ActivationFunction $defaultActivationFunction = null): void
146
    {
147
        foreach ($layers as $layer) {
148
            if (is_array($layer)) {
149
                $function = $layer[1] instanceof ActivationFunction ? $layer[1] : $defaultActivationFunction;
150
                $this->addLayer(new Layer($layer[0], Neuron::class, $function));
151
            } elseif ($layer instanceof Layer) {
152
                $this->addLayer($layer);
153
            } else {
154
                $this->addLayer(new Layer($layer, Neuron::class, $defaultActivationFunction));
155
            }
156
        }
157
    }
158
159
    private function generateSynapses(): void
160
    {
161
        $layersNumber = count($this->layers) - 1;
162
        for ($i = 0; $i < $layersNumber; ++$i) {
163
            $currentLayer = $this->layers[$i];
164
            $nextLayer = $this->layers[$i + 1];
165
            $this->generateLayerSynapses($nextLayer, $currentLayer);
166
        }
167
    }
168
169
    private function addBiasNodes(): void
170
    {
171
        $biasLayers = count($this->layers) - 1;
172
        for ($i = 0; $i < $biasLayers; ++$i) {
173
            $this->layers[$i]->addNode(new Bias());
174
        }
175
    }
176
177
    private function generateLayerSynapses(Layer $nextLayer, Layer $currentLayer): void
178
    {
179
        foreach ($nextLayer->getNodes() as $nextNeuron) {
180
            if ($nextNeuron instanceof Neuron) {
181
                $this->generateNeuronSynapses($currentLayer, $nextNeuron);
182
            }
183
        }
184
    }
185
186
    private function generateNeuronSynapses(Layer $currentLayer, Neuron $nextNeuron): void
187
    {
188
        foreach ($currentLayer->getNodes() as $currentNeuron) {
189
            $nextNeuron->addSynapse(new Synapse($currentNeuron));
190
        }
191
    }
192
193
    private function trainSamples(array $samples, array $targets): void
194
    {
195
        foreach ($targets as $key => $target) {
196
            $this->trainSample($samples[$key], $target);
197
        }
198
    }
199
}
200