Passed
Push — master ( e83f7b...d953ef )
by Arkadiusz
03:28
created

NeuralNetwork/Network/MultilayerPerceptron.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 $activationFunction = null): void
146
    {
147
        foreach ($layers as $neurons) {
148
            $this->addLayer(new Layer($neurons, Neuron::class, $activationFunction));
149
        }
150
    }
151
152
    private function generateSynapses(): void
153
    {
154
        $layersNumber = count($this->layers) - 1;
155
        for ($i = 0; $i < $layersNumber; ++$i) {
156
            $currentLayer = $this->layers[$i];
157
            $nextLayer = $this->layers[$i + 1];
158
            $this->generateLayerSynapses($nextLayer, $currentLayer);
159
        }
160
    }
161
162
    private function addBiasNodes(): void
163
    {
164
        $biasLayers = count($this->layers) - 1;
165
        for ($i = 0; $i < $biasLayers; ++$i) {
166
            $this->layers[$i]->addNode(new Bias());
167
        }
168
    }
169
170
    private function generateLayerSynapses(Layer $nextLayer, Layer $currentLayer): void
171
    {
172
        foreach ($nextLayer->getNodes() as $nextNeuron) {
173
            if ($nextNeuron instanceof Neuron) {
174
                $this->generateNeuronSynapses($currentLayer, $nextNeuron);
175
            }
176
        }
177
    }
178
179
    private function generateNeuronSynapses(Layer $currentLayer, Neuron $nextNeuron): void
180
    {
181
        foreach ($currentLayer->getNodes() as $currentNeuron) {
182
            $nextNeuron->addSynapse(new Synapse($currentNeuron));
183
        }
184
    }
185
186
    private function trainSamples(array $samples, array $targets): void
187
    {
188
        foreach ($targets as $key => $target) {
189
            $this->trainSample($samples[$key], $target);
190
        }
191
    }
192
}
193