LayeredNetwork::addLayer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Phpml\NeuralNetwork\Network;
6
7
use Phpml\NeuralNetwork\Layer;
8
use Phpml\NeuralNetwork\Network;
9
use Phpml\NeuralNetwork\Node\Input;
10
use Phpml\NeuralNetwork\Node\Neuron;
11
12
abstract class LayeredNetwork implements Network
13
{
14
    /**
15
     * @var Layer[]
16
     */
17
    protected $layers = [];
18
19
    public function addLayer(Layer $layer): void
20
    {
21
        $this->layers[] = $layer;
22
    }
23
24
    /**
25
     * @return Layer[]
26
     */
27
    public function getLayers(): array
28
    {
29
        return $this->layers;
30
    }
31
32
    public function removeLayers(): void
33
    {
34
        unset($this->layers);
35
    }
36
37
    public function getOutputLayer(): Layer
38
    {
39
        return $this->layers[count($this->layers) - 1];
40
    }
41
42
    public function getOutput(): array
43
    {
44
        $result = [];
45
        foreach ($this->getOutputLayer()->getNodes() as $neuron) {
46
            $result[] = $neuron->getOutput();
47
        }
48
49
        return $result;
50
    }
51
52
    /**
53
     * @param mixed $input
54
     */
55
    public function setInput($input): Network
56
    {
57
        $firstLayer = $this->layers[0];
58
59
        foreach ($firstLayer->getNodes() as $key => $neuron) {
60
            if ($neuron instanceof Input) {
61
                $neuron->setInput($input[$key]);
62
            }
63
        }
64
65
        foreach ($this->getLayers() as $layer) {
66
            foreach ($layer->getNodes() as $node) {
67
                if ($node instanceof Neuron) {
68
                    $node->reset();
69
                }
70
            }
71
        }
72
73
        return $this;
74
    }
75
}
76