Passed
Push — master ( 7ab80b...4af844 )
by Arkadiusz
03:30
created

Neuron   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 65
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A addSynapse() 0 4 1
A getSynapses() 0 4 1
A getOutput() 0 13 3
A reset() 0 4 1
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\Neuron\Synapse;
9
use Phpml\NeuralNetwork\Node;
10
11
class Neuron implements Node
12
{
13
    /**
14
     * @var Synapse[]
15
     */
16
    protected $synapses;
17
18
    /**
19
     * @var ActivationFunction
20
     */
21
    protected $activationFunction;
22
23
    /**
24
     * @var float
25
     */
26
    protected $output;
27
28
    /**
29
     * @param ActivationFunction|null $activationFunction
30
     */
31
    public function __construct(ActivationFunction $activationFunction = null)
32
    {
33
        $this->activationFunction = $activationFunction ?: new ActivationFunction\Sigmoid();
34
        $this->synapses = [];
35
        $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...
36
    }
37
38
    /**
39
     * @param Synapse $synapse
40
     */
41
    public function addSynapse(Synapse $synapse)
42
    {
43
        $this->synapses[] = $synapse;
44
    }
45
46
    /**
47
     * @return Synapse[]
48
     */
49
    public function getSynapses()
50
    {
51
        return $this->synapses;
52
    }
53
54
    /**
55
     * @return float
56
     */
57
    public function getOutput(): float
58
    {
59
        if (0 === $this->output) {
60
            $sum = 0;
61
            foreach ($this->synapses as $synapse) {
62
                $sum += $synapse->getOutput();
63
            }
64
65
            $this->output = $this->activationFunction->compute($sum);
66
        }
67
68
        return $this->output;
69
    }
70
71
    public function reset()
72
    {
73
        $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...
74
    }
75
}
76