-
Notifications
You must be signed in to change notification settings - Fork 0
/
ann.php
231 lines (179 loc) · 5.28 KB
/
ann.php
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
define("_RAND_MAX",32767);
class Ann{
public $num_of_layers=0;
public $layer_size=0;
public $learning_rate=0;
public $new_weights=null;
public $weights = array();
public $delta=array();
public $output=array();
public $data=array();
public $testData = array();
public function __construct($num_of_layers, $layer_size, $learning_rate) {
$this->num_of_layers = $num_of_layers;
$this->layer_size = $layer_size;
$this->learning_rate = $learning_rate;
//seed the weights of the layers
for($i=1;$i<$this->num_of_layers;$i++){
for($j=0;$j<$this->layer_size[$i];$j++){
for($k=0;$k<$this->layer_size[$i-1]+1;$k++)
{
$this->weights[$i][$j][$k]=$this->random();
}
// bias in the last neuron
$this->weights[$i][$j][$this->layer_size[$i-1]]=-1;
}
}
}
public function forward($inputSource){
$sum = 0.0;
//assign content to input layer
for($i=0;$i<$this->layer_size[0];$i++){
$this->output[0][$i] = $inputSource[$i];
}
//assign sum and activation function to each neuron
for($i=1;$i<$this->num_of_layers;$i++)
{
for($j=0;$j<$this->layer_size[$i];$j++)
{
$sum=0.0;
for($k=0;$k<$this->layer_size[$i-1];$k++)
{
$sum+=$this->output[$i-1][$k]*$this->weights[$i][$j][$k]; // Apply weight to inputs and add to sum
}
// Apply bias
$sum+=$this->weights[$i][$j][$this->layer_size[$i-1]];
// Apply sigmoid function
$this->output[$i][$j]=$this->sigmoid($sum);
}
}
}
public function backward($inputSource,$target){
$this->forward($inputSource);
// FIND DELTA FOR OUPUT LAYER (Last Layer)
for($i=0;$i<$this->layer_size[$this->num_of_layers-1];$i++)
{ //\delta_{o1} = out_{o1}(1 - out_{o1}) * -(target_{o1} - out_{o1})
$this->delta[$this->num_of_layers-1][$i]=$this->output[$this->num_of_layers-1][$i]*(1-$this->output[$this->num_of_layers-1][$i])*($target-$this->output[$this->num_of_layers-1][$i]);
}
//FIND DELTA FOR HIDDEN LAYERS (From Last Hidden Layer BACKWARDS To First Hidden Layer)
for($i=$this->num_of_layers-2;$i>0;$i--)
{
for($j=0;$j<$this->layer_size[$i];$j++)
{
$sum=0.0;
for($k=0;$k<$this->layer_size[$i+1];$k++)
{
$sum+=$this->delta[$i+1][$k]*$this->weights[$i+1][$k][$j];
}
$this->delta[$i][$j]=$this->output[$i][$j]*(1-$this->output[$i][$j])*$sum;
}
}
// $this->debug();
// ADJUST WEIGHT
for($i=1;$i<$this->num_of_layers;$i++)
{
for($j=0;$j<$this->layer_size[$i];$j++)
{
for($k=0;$k<$this->layer_size[$i-1];$k++)
{
$this->new_weights[$i][$j][$k]=$this->learning_rate*$this->delta[$i][$j]*$this->output[$i-1][$k];
$this->weights[$i][$j][$k]+=$this->new_weights[$i][$j][$k];
}
/* --- Apply the corrections */
$this->new_weights[$i][$j][$this->layer_size[$i-1]]=$this->learning_rate*$this->delta[$i][$j];
$this->weights[$i][$j][$this->layer_size[$i-1]]+=$this->new_weights[$i][$j][$this->layer_size[$i-1]];
}
}
}
public function debug(){
echo "<br/>";
echo " weights :";
echo "<br/>";
print_r($this->weights);
echo "<br/>";
echo "<br/>";
echo " outputs :";
echo "<br/>";
print_r($this->output);
echo "<br/>";
echo "<br/>";
echo " delta :";
echo "<br/>";
print_r($this->delta);
echo "<br/>";
echo "<br/>";
echo " new_weights :";
echo "<br/>";
print_r($this->new_weights);
echo "<br/>";
die();
}
protected function sigmoid($inputSource){
return abs((double)(1.0 / (1.0 + exp(-$inputSource))));
}
protected function random(){
return (double)(rand())/(_RAND_MAX/2) - 1;//32767
}
protected function sigmoidPrime($value){
return exp($value)/(pow((1+exp($value)),2));
}
protected function mse($target){
$mse=0;
for($i=0;$i<$this->layer_size[$this->num_of_layers-1];$i++)
{
$mse+=($target-$this->output[$this->num_of_layers-1][$i])*($target-$this->output[$this->num_of_layers-1][$i]);
}
return $mse/2;
}
// returns i'th outputput of the net
public function Out($i){
return $this->output[$this->num_of_layers-1][$i];
}
public function run($data,$testData){
/* --- Threshhold - thresh (value of target mse, training stops once it is achieved) */
$Thresh = 0.0001;
$numEpoch = 200000;
$MSE=0.0;
$NumPattern=count($data); // Lines
$NumInput=count($data[0]); // Columns
echo "\nNow training the network.... <br/>";
for($e=0;$e<$numEpoch;$e++)
{
/* -- Backpropagate */
$this->backward($data[$e%$NumPattern],$data[$e%$NumPattern][$NumInput-1]);
$MSE=$this->mse($data[$e%$NumPattern][$NumInput-1]);
if($e==0)
{
echo "\nFirst epoch Mean Square Error: $MSE <br/>";
}
if( $MSE < $Thresh)
{
echo "\nNetwork Trained. Threshold value achieved in ".$e." iterations. <br/>";
echo "\nMSE: ".$MSE. "<br/>";
break;
}
}
echo "\nLast epoch Mean Square Error: $MSE <br/>";
echo "<br/>";
echo "\nNow using the trained network to make estimations on test data.... <br/>";
echo "<table>";
echo "<th> X1 </th>";
echo "<th> X2 </th>";
echo "<th> Estimation </th>";
for ($i = 0 ; $i < count($testData); $i++ )
{
$this->forward($testData[$i]);
echo "\n";
echo "<tr>";
for($j=0;$j<$NumInput-1;$j++)
{
echo "<td>".$testData[$i][$j]."</td>";
}
echo "<td>".(double)$this->Out(0)."</td>";
echo "</tr>";
}
echo "</table>";
}
}
?>