-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNeuron.php
More file actions
146 lines (114 loc) · 3.59 KB
/
Neuron.php
File metadata and controls
146 lines (114 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
use Traits\IdTrait;
use Layers\Layer;
use Logging\Logger;
class Neuron
{
use IdTrait;
protected $value;
protected $outputVal;
protected $preValue;
protected $outputWeights;
protected $gradient;
protected $myIndex;
/** @var Layer */
protected $layer;
protected $eta = 0.15;
protected $alpha = 0.5;
public function __construct(int $numOutputs, int $myIndex)
{
foreach (range(0, $numOutputs) as $i) {
$this->outputWeights[] = [
'weight' => randFloat(),
'deltaWeight' => 0
];
}
$this->myIndex = $myIndex;
$this->setId();
}
public function setLayer(Layer $layer)
{
$this->layer = $layer;
}
public function setOutputVal(float $value)
{
$this->outputVal = $value;
}
public function getOutputVal()
{
return $this->outputVal;
}
public function feedForward(Layer $prevLayer)
{
$sum = 0.0;
$prevNodes = $prevLayer->getNodes();
for ($n = 0; $n < count($prevNodes); ++$n) {
/** @var Neuron $prevNode */
$prevNode = $prevNodes[$n];
$sum += $prevNode->getOutputVal() * $prevNode->getOutputWeightFor($this->myIndex)['weight'];
}
if (!is_numeric($sum)) {
throw new Exception(ErrorsEnum::NODE_OUTPUT_NOT_NUMERIC);
}
$this->setOutputVal(transferFnc($sum));
}
public function calcOutputGradients(float $targetVal)
{
Logger::debug("Calculating output gradients");
$delta = $targetVal - $this->getOutputVal();
if (!is_numeric($this->getOutputVal())) {
throw new Exception(ErrorsEnum::NODE_OUTPUT_NOT_NUMERIC);
}
$this->gradient = $delta * transferDerivativeFnc($this->getOutputVal());
}
public function calcHiddenGradients(Layer $nextLayer)
{
$dow = $this->sumDOW($nextLayer);
$this->gradient = $dow * transferDerivativeFnc($this->getOutputVal());
}
function sumDOW(Layer $nextLayer): float
{
$sum = 0.0;
$nodes = $nextLayer->getNodes();
for ($n = 0; $n < count($nodes) - 1; ++$n) {
/** @var Neuron $nextNode */
$nextNode = $nodes[$n];
$sum += $this->outputWeights[$n]['weight'] * $nextNode->getGradient();
}
return $sum;
}
public function getGradient()
{
return $this->gradient;
}
public function updateInputWeights(Layer $prevLayer)
{
$nodes = $prevLayer->getNodes();
for ($n = 0; $n < count($nodes); ++$n) {
/** @var Neuron $node */
$node = $nodes[$n];
$oldDeltaWeight = $node->getOutputWeightFor($this->myIndex)['deltaWeight'];
$newDeltaWeight =
$this->eta
* $node->getOutputVal()
* $this->gradient
+ $this->alpha
* $oldDeltaWeight;
$node->updateOutputWeightsFor($this->myIndex, $newDeltaWeight);
}
}
public function getOutputWeightFor($index)
{
return array_get($this->outputWeights, $index, ['weight' => 0, 'deltaWeight' => 0.0]);
}
public function getOutputWeights()
{
return $this->outputWeights;
}
public function updateOutputWeightsFor(int $nodeIndex, float $newDeltaWeight)
{
$weight = $this->outputWeights[$nodeIndex]['weight'] ?? 0.0;
$this->outputWeights[$nodeIndex]['deltaWeight'] = $newDeltaWeight;
$this->outputWeights[$nodeIndex]['weight'] = $weight + $newDeltaWeight;
}
}