Passed
Pull Request — master (#399)
by
unknown
02:57
created

Neuron   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 25
dl 0
loc 84
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getDerivative() 0 3 1
A getTrainedCharacteristics() 0 8 2
A getSynapses() 0 3 1
A setTrainedCharacteristics() 0 9 3
A reset() 0 4 1
A getOutput() 0 12 3
A __construct() 0 3 2
A addSynapse() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Phpml\NeuralNetwork\Node;
6
7
use Phpml\Exception\BadNeuralNetworkStructureException;
8
use Phpml\NeuralNetwork\ActivationFunction;
9
use Phpml\NeuralNetwork\ActivationFunction\Sigmoid;
10
use Phpml\NeuralNetwork\Node;
11
use Phpml\NeuralNetwork\Node\Neuron\Synapse;
12
13
class Neuron implements Node
14
{
15
    /**
16
     * @var Synapse[]
17
     */
18
    protected $synapses = [];
19
20
    /**
21
     * @var ActivationFunction
22
     */
23
    protected $activationFunction;
24
25
    /**
26
     * @var float
27
     */
28
    protected $output = 0.0;
29
30
    /**
31
     * @var float
32
     */
33
    protected $z = 0.0;
34
35
    public function __construct(?ActivationFunction $activationFunction = null)
36
    {
37
        $this->activationFunction = $activationFunction ?: new Sigmoid();
38
    }
39
40
    public function addSynapse(Synapse $synapse): void
41
    {
42
        $this->synapses[] = $synapse;
43
    }
44
45
    /**
46
     * @return Synapse[]
47
     */
48
    public function getSynapses(): array
49
    {
50
        return $this->synapses;
51
    }
52
53
    public function getOutput(): float
54
    {
55
        if ($this->output === 0.0) {
0 ignored issues
show
introduced by
The condition $this->output === 0.0 is always false.
Loading history...
56
            $this->z = 0;
57
            foreach ($this->synapses as $synapse) {
58
                $this->z += $synapse->getOutput();
59
            }
60
61
            $this->output = $this->activationFunction->compute($this->z);
62
        }
63
64
        return $this->output;
65
    }
66
67
    public function getDerivative(): float
68
    {
69
        return $this->activationFunction->differentiate($this->z, $this->output);
70
    }
71
72
    public function reset(): void
73
    {
74
        $this->output = 0.0;
75
        $this->z = 0.0;
76
    }
77
78
    public function getTrainedCharacteristics(): array
79
    {
80
        $result = [];
81
        foreach ($this->synapses as $synapse) {
82
            $result[] = $synapse->getWeight();
83
        }
84
85
        return $result;
86
    }
87
88
    public function setTrainedCharacteristics(array $characteristics): void
89
    {
90
        // length of weights should equals number of synapses
91
        if (count($characteristics) != count($this->synapses)) {
92
            throw new BadNeuralNetworkStructureException();
93
        }
94
95
        for ($i = 0; $i < count($characteristics); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
96
            $this->synapses[$i]->setWeight($characteristics[$i]);
97
        }
98
    }
99
}
100