Passed
Pull Request — master (#399)
by
unknown
05:27
created

Layer   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 24
dl 0
loc 71
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addNode() 0 3 1
A getNodes() 0 3 1
A __construct() 0 8 3
A getTrainedCharacteristics() 0 10 3
A createNode() 0 7 2
A setTrainedCharacteristics() 0 14 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Phpml\NeuralNetwork;
6
7
use Phpml\Exception\InvalidArgumentException;
8
use Phpml\NeuralNetwork\Node\Neuron;
9
10
class Layer
11
{
12
    /**
13
     * @var Node[]
14
     */
15
    private $nodes = [];
16
17
    /**
18
     * @throws InvalidArgumentException
19
     */
20
    public function __construct(int $nodesNumber = 0, string $nodeClass = Neuron::class, ?ActivationFunction $activationFunction = null)
21
    {
22
        if (!in_array(Node::class, class_implements($nodeClass), true)) {
23
            throw new InvalidArgumentException('Layer node class must implement Node interface');
24
        }
25
26
        for ($i = 0; $i < $nodesNumber; ++$i) {
27
            $this->nodes[] = $this->createNode($nodeClass, $activationFunction);
28
        }
29
    }
30
31
    public function addNode(Node $node): void
32
    {
33
        $this->nodes[] = $node;
34
    }
35
36
    /**
37
     * @return Node[]
38
     */
39
    public function getNodes(): array
40
    {
41
        return $this->nodes;
42
    }
43
44
    public function getTrainedCharacteristics(): array
45
    {
46
        $result = [];
47
        foreach ($this->nodes as $node) {
48
            if ($node instanceof Neuron) {
49
                $result[] = $node->getTrainedCharacteristics();
50
            }
51
        }
52
53
        return $result;
54
    }
55
56
    public function setTrainedCharacteristics(array $characteristics): void
57
    {
58
        // iterate over the node instances
59
        $iNode = -1;
60
        for ($i = 0; $i < count($this->nodes); $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...
61
            $node = $this->nodes[$i];
62
            if ($node instanceof Neuron) {
63
                $iNode ++;
64
65
                if (count($characteristics) < $iNode + 1) {
66
                    throw new InvalidArgumentException('Loaded weights should match the layer structure ');
67
                }
68
69
                $node->setTrainedCharacteristics($characteristics[$iNode]);
70
            }
71
        }
72
    }
73
74
    private function createNode(string $nodeClass, ?ActivationFunction $activationFunction = null): Node
75
    {
76
        if ($nodeClass === Neuron::class) {
77
            return new Neuron($activationFunction);
78
        }
79
80
        return new $nodeClass();
81
    }
82
}
83