Completed
Push — develop ( f186aa...7062ee )
by Arkadiusz
03:20
created

Neuron::getOutput()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 7
nc 2
nop 0
1
<?php
2
3
declare (strict_types = 1);
4
5
namespace Phpml\NeuralNetwork\Node;
6
7
use Phpml\NeuralNetwork\ActivationFunction;
8
use Phpml\NeuralNetwork\Node;
9
10
class Neuron implements Node
11
{
12
    /**
13
     * @var Synapse[]
14
     */
15
    protected $synapses;
16
17
    /**
18
     * @var ActivationFunction
19
     */
20
    protected $activationFunction;
21
22
    /**
23
     * @var float
24
     */
25
    protected $output;
26
27
    /**
28
     * @param ActivationFunction|null $activationFunction
29
     */
30
    public function __construct(ActivationFunction $activationFunction = null)
31
    {
32
        $this->activationFunction = $activationFunction ?: new ActivationFunction\Sigmoid();
33
        $this->synapses = [];
34
        $this->output = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $output was declared of type double, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
35
    }
36
37
    /**
38
     * @param Synapse $synapse
39
     */
40
    public function addSynapse(Synapse $synapse)
41
    {
42
        $this->synapses[] = $synapse;
43
    }
44
45
    /**
46
     * @return Synapse[]
47
     */
48
    public function getSynapses()
49
    {
50
        return $this->synapses;
51
    }
52
53
    /**
54
     * @return float
55
     */
56
    public function getOutput(): float
57
    {
58
        if (0 === $this->output) {
59
            $sum = 0;
60
            foreach ($this->synapses as $synapse) {
61
                $sum += $synapse->getOutput();
62
            }
63
64
            $this->output = $this->activationFunction->compute($sum);
65
        }
66
67
        return $this->output;
68
    }
69
70
    public function refresh()
71
    {
72
        $this->output = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $output was declared of type double, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
73
    }
74
}
75