-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNet.php
More file actions
198 lines (147 loc) · 5.04 KB
/
Net.php
File metadata and controls
198 lines (147 loc) · 5.04 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
use Logging\Logger;
use Layers\Layer;
use Layers\HiddenLayer;
use Layers\InputLayer;
use Layers\OutputLayer;
class Net
{
private $layers;
/** @var InputLayer */
public $inputLayer;
/** @var HiddenLayer */
public $hiddenLayer;
/** @var OutputLayer */
public $outputLayer;
/** @var float */
private $error = 0.0;
/** @var float */
private $recentAvgError = 0.0;
/** @var float */
private static $recentAvgSmoothingFactor = 100.0;
/** @var bool */
private $isSetup = false;
public function setup(array $topology)
{
$lastIndex = count($topology) - 1;
foreach ($topology as $index => $layerTopology) {
if ($index === 0) {
$this->layers[] = $layer = $this->inputLayer = new InputLayer($this, $layerTopology);
} elseif ($index === $lastIndex) {
$this->layers[] = $layer = $this->outputLayer = new OutputLayer($this, $layerTopology);
} else {
$this->layers[] = $layer = $this->hiddenLayer = new HiddenLayer($this, $layerTopology);
}
}
$this->isSetup = true;
Logger::debug("Total number of layers created: " . count($this->layers));
}
public function feedForward(array $inputVals)
{
$this->assertSetup();
Logger::all("Setting input values for feed forward: " . json_encode($inputVals));
assert(
count($inputVals) === count($this->inputLayer->getNodes()) - 1,
"The number of inputs should be the same as number of nodes in input layer"
);
$inputVals[] = 1.0;
$this->inputLayer->setOutputVals($inputVals);
/** @var Layer $currLayer */
foreach ($this->layers as $i => $currLayer) {
if ($i === 0) {
continue;
}
/** @var Layer $prevLayer */
$prevLayer = $this->layers[$i - 1];
$currLayer->feedForward($prevLayer);
}
}
public function backProp(array $targetVals)
{
$this->assertSetup();
//
// error
// Calculate overall net error (RMS of output neuron errors)
$outputNodes = $this->outputLayer->getNodes();
$this->error = 0.0;
for ($n = 0; $n < count($outputNodes) - 1; ++$n) {
/** @var Neuron $outputNode */
$outputNode = $outputNodes[$n];
$delta = $targetVals[$n] - $outputNode->getOutputVal();
$this->error += $delta * $delta;
}
$this->error /= (count($outputNodes) - 1);
$this->error = sqrt($this->error);
// Implement a recent average measurement
$this->recentAvgError =
($this->recentAvgError * self::$recentAvgSmoothingFactor + $this->error)
/ (self::$recentAvgSmoothingFactor + 1.0);
// Calculate output layer gradients
for ($n = 0; $n < count($outputNodes) - 1; ++$n) {
/** @var Neuron $outputNode */
$outputNode = $outputNodes[$n];
$outputNode->calcOutputGradients($targetVals[$n]);
}
// Calculate hidden layer gradients
$this->calcHiddenGradients();
// For all layers from outputs to first hidden layer,
// update connection weights
$this->updateInputWeights();
}
public function updateInputWeights()
{
$this->assertSetup();
for ($i = count($this->layers) - 1; $i > 0; --$i) {
/** @var Layer $currLayer */
$currLayer = $this->layers[$i];
$currNodes = $currLayer->getNodes();
/** @var Layer $prevLayer */
$prevLayer = $this->layers[$i - 1];
for ($n = 0; $n < count($currNodes) - 1; ++$n) {
/** @var Neuron $currNode */
$currNode = $currNodes[$n];
$currNode->updateInputWeights($prevLayer);
}
}
}
/**
* Calculate hidden gradients.
* @todo: In order to be dynamic, this needs to go through ALL hidden
* @todo: layers and not just assume 1 hidden layer.
*/
public function calcHiddenGradients()
{
for ($i = count($this->layers) - 2; $i > 0; --$i) {
$hiddenLayer = $this->layers[$i];
$hiddenNodes = $hiddenLayer->getNodes();
$nextLayer = $this->layers[$i + 1];
for ($n = 0; $n < count($hiddenNodes); ++$n) {
/** @var Neuron $hiddenNode */
$hiddenNode = $hiddenNodes[$n];
$hiddenNode->calcHiddenGradients($nextLayer);
}
}
}
public function getError()
{
return $this->error;
}
public function getRecentAvgError()
{
return $this->recentAvgError;
}
public function getResults()
{
return $this->outputLayer->getOutputVals();
}
public function getLayers()
{
return $this->layers;
}
private function assertSetup()
{
if (!$this->isSetup) {
throw new Exception(ErrorsEnum::NETWORK_NOT_SETUP);
}
}
}