Passed
Push — master ( fbbe5c...a34811 )
by Arkadiusz
07:00
created

Phpml/NeuralNetwork/Training/Backpropagation.php (2 issues)

assigning incompatible types to properties.

Bug Documentation Major

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\Training;
6
7
use Phpml\NeuralNetwork\Node\Neuron;
8
use Phpml\NeuralNetwork\Training\Backpropagation\Sigma;
9
10
class Backpropagation
11
{
12
    /**
13
     * @var float
14
     */
15
    private $learningRate;
16
17
    /**
18
     * @var array
19
     */
20
    private $sigmas = null;
21
22
    /**
23
     * @var array
24
     */
25
    private $prevSigmas = null;
26
27
    public function __construct(float $learningRate)
28
    {
29
        $this->setLearningRate($learningRate);
30
    }
31
32
    public function setLearningRate(float $learningRate): void
33
    {
34
        $this->learningRate = $learningRate;
35
    }
36
37
    /**
38
     * @param mixed $targetClass
39
     */
40
    public function backpropagate(array $layers, $targetClass): void
41
    {
42
        $layersNumber = count($layers);
43
44
        // Backpropagation.
45
        for ($i = $layersNumber; $i > 1; --$i) {
46
            $this->sigmas = [];
47
            foreach ($layers[$i - 1]->getNodes() as $key => $neuron) {
48
                if ($neuron instanceof Neuron) {
49
                    $sigma = $this->getSigma($neuron, $targetClass, $key, $i == $layersNumber);
50
                    foreach ($neuron->getSynapses() as $synapse) {
51
                        $synapse->changeWeight($this->learningRate * $sigma * $synapse->getNode()->getOutput());
52
                    }
53
                }
54
            }
55
56
            $this->prevSigmas = $this->sigmas;
57
        }
58
59
        // Clean some memory (also it helps make MLP persistency & children more maintainable).
60
        $this->sigmas = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $sigmas.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
61
        $this->prevSigmas = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $prevSigmas.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
62
    }
63
64
    private function getSigma(Neuron $neuron, int $targetClass, int $key, bool $lastLayer): float
65
    {
66
        $neuronOutput = $neuron->getOutput();
67
        $sigma = $neuronOutput * (1 - $neuronOutput);
68
69
        if ($lastLayer) {
70
            $value = 0;
71
            if ($targetClass === $key) {
72
                $value = 1;
73
            }
74
75
            $sigma *= ($value - $neuronOutput);
76
        } else {
77
            $sigma *= $this->getPrevSigma($neuron);
78
        }
79
80
        $this->sigmas[] = new Sigma($neuron, $sigma);
81
82
        return $sigma;
83
    }
84
85
    private function getPrevSigma(Neuron $neuron): float
86
    {
87
        $sigma = 0.0;
88
89
        foreach ($this->prevSigmas as $neuronSigma) {
90
            $sigma += $neuronSigma->getSigmaForNeuron($neuron);
91
        }
92
93
        return $sigma;
94
    }
95
}
96