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

src/Phpml/NeuralNetwork/Node/Neuron.php (2 issues)

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