-
Notifications
You must be signed in to change notification settings - Fork 1
/
rn.class.php
369 lines (302 loc) · 11.7 KB
/
rn.class.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<?php
/** rn class. red neuronal (neural network)
*
*
*
* @author Rafael Martin Soto
* @author {@link http://www.inatica.com/ Inatica}
* @since July 2021
* @version 1.0.1
* @license GNU General Public License v3.0
*
* Thanks to:
* - https://github.com/infostreams/neural-network/blob/master/class_neuralnetwork.php
* - https://gist.github.com/ikarius6/26851fb7220837e8016fe0c425d34dd6
* - https://mattmazur.com/2015/03/17/a-step-by-step-backpropagation-example/
* - https://www.youtube.com/channel/UCy5znSnfMsDwaLlROnZ7Qbg
*/
require_once( 'rn_layer.class.php' );
/** rn class. red neuronal (neural network)
*
* Create a neural network class with one array with number of nodes each layer.
* Example: If we want a neural network with 4 layers (2 inputs neurons, 12 hidden neurons, 8 hidden neurons and 1 output neuron) as 2x12x8x1
* Only need to send as paramater an array as: [2, 12, 8, 1]
*
* $rn = new rn( [2, 12, 8, 1] ); // 2x12x8x1
*
* @param array $arrLayersNodes
*/
class rn
{
public $layers = [];
public $num_layers = 0;
public $alpha = 1;
public $InformEachXBlock = 100; // echoes actual error each 100 blocks of datasets
public $InformEachXEpoch = 100; // echoes actual error each 100 epochs
public $MaxItemsMeanSquareError = 1000000; // Max number of Train items to get the Mean Square Error
public $num_epochs = 1000;
public $MeanSquareError = 0;
public $system_resources = null;
public function __construct( $arrLayersNodes = [2, 12, 8, 1] ) {
$this->layers = [];
$this->num_layers = count( $arrLayersNodes );
$previous_layer = null;
$system_resources = new system_resources(); // Create obj class system_resources, needed for control CPU Temperature in process of BackPropagation of the Deep Learning
// Init layers
for($i=0;$i<$this->num_layers;$i++){
$NumOfNewNodes = $arrLayersNodes[$i];
$this->layers[] = new layer( $NumOfNewNodes );
if( $previous_layer != null ){
$previous_layer->next_layer = $this->layers[$i];
$arrNodeWeightsToNextLayer = array_fill(0, $NumOfNewNodes, .5);
$previous_layer->fInitNodeWeightsToNextLayer( $arrNodeWeightsToNextLayer, .5 );
}
$this->layers[$i]->previous_layer = $previous_layer;
$previous_layer = $this->layers[$i];
} // /for i
$this->layers[0]->fSetImFirst();
$this->layers[$this->num_layers-1]->fSetImLast();
} // / __construct
/**
* Set the activation function for all the layers
* sigm = sigmoidal
* tanh = hyperbolic tangent
*
* Optionaly, we can set different activation funcion for each layer
*
* @param string $activation_function ['sigm'|'tanh']
*/
public function fSet_activation_function( $activation_function ){
for($i=0;$i<$this->num_layers;$i++){
$this->layers[$i]->fSet_activation_function($activation_function);
}
} // /fSet_activation_function()
/**
* Set Num Epochs to model
*
* @param array $arrTrainInputItems
* @param array $arrTrainOutputItems
* @param int $Epochs (Optional. Default 1000 Epochs)
*/
public function fSet_num_epochs( $Epochs ){
$this->num_epochs = $Epochs;
} // /fSet_num_epochs
/**
* Set Num of max items to be calculated at Mean Square Error
*
* @param array $MaxItemsMeanSquareError
*/
public function fSet_MaxItemsMeanSquareError( $MaxItemsMeanSquareError ){
$this->MaxItemsMeanSquareError = $MaxItemsMeanSquareError;
} // /fSet_MaxItemsMeanSquareError
/**
* Run our model
*
* @param int $id_output_node
* @param array $arrInputValues
*/
public function run( $id_output_node, $arrInputValues ){
$idLastLayer = $this->num_layers-1;
return $this->layers[$idLastLayer]->Y( $id_output_node, $arrInputValues );
} // /run()
/**
* BackPropagation function
*
* @param array $arrInputValues
* @param array $arrOutputValues
*/
public function BackPropagation( $arrInputValues, $arrOutputValues ){
$idLastLayer = $this->num_layers-1;
$OutputLayer = $this->layers[$idLastLayer];
$previous_layer = $OutputLayer->previous_layer;
$previous_layer_num_nodes = $previous_layer->num_nodes;
$num_output_nodes = $OutputLayer->num_nodes;
$inputLayer = $this->layers[0];
$input_layer_num_nodes = $inputLayer->num_nodes;
for($i=0; $i<$num_output_nodes; $i++){
$OutputLayer->nodes[$i]->delta = $OutputLayer->deltaLastLayer( $i, $arrInputValues, $arrOutputValues );
}
// Set Error to the other layers
for($id_layer=0;$id_layer<($this->num_layers-1);$id_layer++){
$layer = $this->layers[$id_layer];
$next_layer = $layer->next_layer;
$next_layer_num_nodes = $next_layer->num_nodes;
$num_nodes = $layer->num_nodes;
for($i=0; $i<$num_nodes; $i++){
$layer_node = $layer->nodes[$i];
$Y = $layer->Y( $i, $arrInputValues );
for($j=0;$j<$next_layer_num_nodes;$j++){
$this->layers[$id_layer]->nodes[$i]->arr_weights_to_next_layer[$j] -= $this->alpha * $this->error($Y, $i, $j, $arrInputValues, $arrOutputValues, $layer->next_layer);
$next_layer->nodes[$j]->bias -= $this->alpha * (($next_layer->imLast)?$next_layer->nodes[$j]->delta:$next_layer->deltaLayer($i, $j, $arrInputValues, $arrOutputValues));
//exit(1);
}
}
}
} // /BackPropagation()
/**
* Set Alpha (Learning rate)
*
* @param float $alpha
*/
public function set_alpha($alpha){
$this->alpha = $alpha;
}
/**
* Get the error
*
* @param float $Y
* @param int $i
* @param int $j
* @param array $arrTrainInputItems
* @param array $arrTrainOutputItems
* @param object $next_layer
* @return float error
*/
public function error($Y, $i, $j, $arrInputValues, $arrOutputValues, $next_layer){
$Delta = (($next_layer->imLast)?$next_layer->nodes[$j]->delta:$next_layer->deltaLayer($i , $j, $arrInputValues, $arrOutputValues));
return $Y * $Delta;
}
/**
* Master function who calls BackPropagation process
* in the future it will be multithread
*
* @param array $arrTrainInputItems
* @param array $arrTrainOutputItems
* @param int $Epochs (Optional. Default 1000 Epochs)
*/
public function Learn($arrTrainInputItems, $arrTrainOutputItems, $arrValidationInputItems = NULL, $arrValidationOutputItems = NULL, $arrTestInputItems = NULL, $arrTestOutputItems = NULL, $Epochs = NULL){
if( !isset($arrValidationInputItems) || is_null($arrValidationInputItems) ){
$arrValidationInputItems = $arrTrainInputItems;
$arrValidationOutputItems = $arrTrainOutputItems;
}
if( !isset($arrTestInputItems) || is_null($arrTestInputItems) ){
$arrTestInputItems = $arrValidationInputItems;
$arrTestOutputItems = $arrValidationOutputItems;
}
if( isset($Epochs) && !is_null($Epochs) ){
$this->num_epochs = $Epochs;
}
$num_sample_data = count($arrTrainInputItems);
for($i = 0;$i<$this->num_epochs;$i++){
for($j=0;$j<$num_sample_data;$j++){
$this->BackPropagation($arrTrainInputItems[$j],$arrTrainOutputItems[$j]);
if( $j%$this->InformEachXBlock == 0 ){
$MeanSquareErrorValidationData = $this->MeanSquareError( $arrValidationInputItems, $arrValidationOutputItems );
$MeanSquareErrorTestData = $this->MeanSquareError( $arrTestInputItems, $arrTestOutputItems );
$StrEcho = 'Item '.$j.'/'.$num_sample_data.' . Epoch '.$i.'/'.$this->num_epochs;
$StrEcho .= '. Actual error: (Validaton Data: '.number_format($MeanSquareErrorValidationData, 4, '.', ','). ')';
$StrEcho .= '/ (Test Data: '.number_format($MeanSquareErrorTestData, 4, '.', ','). ')';
echo $StrEcho.PHP_EOL;
$this->MeanSquareError = $MeanSquareErrorValidationData;
}
}
if( $i%$this->InformEachXEpoch == 0 && $this->InformEachXEpoch > $this->InformEachXBlock ){
$MeanSquareErrorValidationData = $this->MeanSquareError( $arrValidationInputItems, $arrValidationOutputItems );
$MeanSquareErrorTestData = $this->MeanSquareError( $arrTestInputItems, $arrTestOutputItems );
$StrEcho = 'Epoch '.$i.'/'.$this->num_epochs;
$StrEcho .= '. Actual error: (Validaton Data: '.number_format($MeanSquareErrorValidationData, 4, '.', ','). ')';
$StrEcho .= '/ (Test Data: '.number_format($MeanSquareErrorTestData, 4, '.', ','). ')';
echo $StrEcho.PHP_EOL;
$this->MeanSquareError = $MeanSquareErrorValidationData;
}
}
} // /Learn()
/**
* Mean Square Error
*
* @param array $arrValidationInputItems
* @param array $arrValidationOutputItems
* @return float $MeanSquareError
*/
public function MeanSquareError($arrValidationInputItems, $arrValidationOutputItems){
$last_id_layer = $this->num_layers - 1;
$num_output_nodes = $this->layers[$last_id_layer]->num_nodes;
$ErrorSum = 0;
$num_sample_data = count($arrValidationInputItems);
if($num_sample_data > $this->MaxItemsMeanSquareError){
$num_sample_data = $this->MaxItemsMeanSquareError;
}
$NumItemsAdded = 0;
for($i=0;$i<$num_sample_data ;$i++){
for($j=0;$j<$num_output_nodes;$j++){
$ValueEstimated = $this->run($j, $arrValidationInputItems[$i]);
$Diff = ( $arrValidationOutputItems[$i][$j]-$ValueEstimated );
$ErrorSum += pow($Diff, 2);
++$NumItemsAdded;
}
}
$MeanSquareError = ( $ErrorSum / ($NumItemsAdded) );
return $MeanSquareError;
} // /MeanSquareError()
/**
* Echo output values of neural network with given values. If DesiredValues given, it will echo it too
*
* @param array $arrTrainInputItems
* @param array $arrTrainOutputItems
* @param int $Epochs (Optional. Default 1000 Epochs)
*/
public function EchoOutputValues($arrInputItems, $arrDesiredOutputItems = null){
$last_id_layer = $this->num_layers - 1;
$num_output_nodes = $this->layers[$last_id_layer]->num_nodes;
echo 'Input Values: '.implode(',', $arrInputItems).PHP_EOL;
for($j=0;$j<$num_output_nodes;$j++){
echo 'Output neuron ['.$j.']: '. $this->run($j, $arrInputItems);
if( isset($arrDesiredOutputItems) ){
echo '. Expect: '.$arrDesiredOutputItems[$j];
}
echo PHP_EOL;
}
echo PHP_EOL;
} // /EchoOutputValues()
/**
* Export the data of neural network into jSON object
*
* @return json $arrJSON
*/
public function exportData2Json(){
$FirstLayer = $this->layers[0];
$NumInputNodes = $FirstLayer->num_nodes;
$arrJSONLayers = [];
// Create an array of jSON'S data layers
foreach($this->layers as $layer){
$arrJSONLayers[] = json_decode( $layer->exportData2Json() );
}
$arrJSON = [ 'InaticaNeuralNetwork' =>
[ 'NumInputNeurons' => $this->layers[0]->num_nodes,
'NumOutputNeurons' => $this->layers[$this->num_layers-1]->num_nodes,
'CreationDate' => date("Y-m-d H:i:s"),
'NumTotalLayers' => $this->num_layers,
'NumHiddenLayers' => $this->num_layers -2, // All layers without input layer and without output layer
'NumEpochs' => $this->num_epochs, // number of epochs that the neural network has done to learn
'MeanSquareError' => $this->MeanSquareError,
'Layers' => $arrJSONLayers
]
];
return json_encode($arrJSON);
} // /exportData2Json()
/**
* Import the data of jSON object into rn
*
* @param json $JsonData
*/
public function importJson2Data($JsonData){
$this->num_layers = $JsonData->NumTotalLayers;
$this->NumEpochs = $JsonData->NumEpochs;
$this->MeanSquareError = $JsonData->MeanSquareError;
$previous_layer = null;
$this->layers = [];
$JsonLayers = $JsonData->Layers;
$i = 0;
foreach($JsonLayers as $Layer){
$this->layers[] = new layer( $Layer->Layer->num_nodes );
$this->layers[$i]->importJson2Data($Layer);
if( $previous_layer != null ){
$previous_layer->next_layer = $this->layers[$i];
}
$this->layers[$i]->previous_layer = $previous_layer;
$previous_layer = $this->layers[$i];
++$i;
} // /foreach
} // /importJson2Data()
} // /rn class
?>