-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrn_layer.class.php
257 lines (212 loc) · 6.43 KB
/
rn_layer.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
<?php
/** layer class include
*
*
*
* @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
*/
require_once( 'rn_node.class.php' );
/* layer class
*
* @param int $num_nodes num of nodes that will have the layer
* @param float $default_weight default weight for each node
* @param float $default_bias default weight for bias
*/
class layer
{
public $num_nodes;
public $nodes;
public $previous_layer;
public $next_layer;
public $imFirst;
public $imLast;
public $activation_function;
public function __construct( $num_nodes = 2, $default_weight = [.5], $default_bias = .5) {
$this->nodes = [];
// Init nodes
for($i=0;$i<$num_nodes;$i++){
$this->nodes[] = new node( $default_weight, $default_bias);
}
$this->default_bias = $default_bias;
$this->num_nodes = $num_nodes;
$this->imFirst = false; // Input Data
$this->imLast = false; // Output Data
$this->activation_function = 'sigm'; // sigm | tanh | relu | bin
} // /__construct()
/**
* Get Delta for Last Layer
*/
public function deltaLastLayer($id_output_node, $arrInputValues, $arrOutputValues){
$Y = $this->Y( $id_output_node, $arrInputValues );
$Derivate = $this->DerivateFunctionActivation( $id_output_node, $arrInputValues, $Y );
$RealError = $this->RealError( $id_output_node, $arrOutputValues, $Y );
return $Derivate*$RealError;
} // /deltaLastLayer()
/**
* Get Delta for Layer
*/
public function deltaLayer($id_previous_node, $id_output_node, $arrInputValues, $arrOutputValues){
$next_layer = $this->next_layer;
$next_layer_num_nodes = $next_layer->num_nodes;
$previous_layer = $this->previous_layer;
$previous_layer_num_nodes = $previous_layer->num_nodes;
$num_nodes = $this->num_nodes;
$outputNode = $this->nodes[$id_output_node];
$Y = $this->Y( $id_output_node, $arrInputValues );
$sum=0;
for($i=0; $i<$next_layer_num_nodes; $i++){
$NextWeight = $this->nodes[$id_output_node]->arr_weights_to_next_layer[$i];
$DeltaLayer = (($next_layer->imLast)?$next_layer->nodes[$i]->delta:$next_layer->deltaLayer($id_output_node, $i, $arrInputValues, $arrOutputValues));
$sum += ($NextWeight*$DeltaLayer);
}
$Derivate = $this->DerivateFunctionActivation( $id_output_node, $arrInputValues, $Y );
return $Derivate * $sum;
} // /deltaLastLayer()
/**
* Get Real Error for Last Layer
*/
public function RealError($id_output_node, $arrOutputValues, $Y){
return ($Y-$arrOutputValues[$id_output_node]);
} // /deltaLastLayer()
/**
* Set the activation function for this layer
* sigm = sigmoidal
* tanh = hyperbolic tangent
*
* We can set different activation funcion for each layer
*
* @param string $activation_function ['sigm'|'tanh']
*/
public function fSet_activation_function( $activation_function ){
$this->activation_function = $activation_function;
} // /fSet_activation_function()
/**
* Set this layer as first layer in neural network
*/
public function fSetImFirst( ){
$this->imFirst = true;
} // /fSetImFirst()
/**
* Set this layer as last layer in neural network
*/
public function fSetImLast( ){
$this->imLast = true;
} // /fSetImLast()
/**
* Set the weights to next layer
*
* @param array array of weights
*/
public function fInitNodeWeightsToNextLayer( $arrNewWeights, $bias_weight = .5 ){
// Init nodes
for($i=0;$i<$this->num_nodes;$i++){
$this->nodes[$i]->setNodeWeightsToNextLayer ( $arrNewWeights );
$this->nodes[$i]->bias = $bias_weight;
}
} // /fInitNodeWeightsToNextLayer()
/**
* Get the derivate function of activacion function
*
* @param int $id_output_node
* @param array $arrInputValues
* @return float
*/
public function DerivateFunctionActivation( $id_output_node, $arrInputValues, $Y=null ){
if(! $Y ){
$f = $this->Y( $id_output_node, $arrInputValues );
} else {
$f = $Y;
}
switch($this->activation_function){
case 'relu': return $f > 0;
break;
case 'tanh': $tanh = tanh($f);
return (1 - $tanh) * (1 + $tanh);
break;
case 'sigm':
default: return $f*(1-$f);
break;
}
}// /DerivateFunctionActivation()
/** Activation function
*
* @param float
*/
function f($x){
switch( $this->activation_function ){
case 'relu': return $x * ($x > 0); // or max(0, $x);
break;
case 'tanh': return tanh($x);
break;
case 'sigm':
default: return 1 / (1 + exp(-$x));
break;
}
} // /f()
/** Output value
*
* @param int $id_output_node
* @param array $arrInputValues
* @return float
*/
public function Y( $id_output_node, $arrInputValues ){
if( $this->imFirst ){
return $arrInputValues[$id_output_node];
} else {
$Y = 0;
$PreviousLayer = $this->previous_layer;
$NumNodesPrevLayer = $PreviousLayer->num_nodes;
for($i=0;$i<$NumNodesPrevLayer;$i++){
$sumPreviousLayer = $PreviousLayer->Y( $i, $arrInputValues );
$NextWeight = $PreviousLayer->nodes[$i]->arr_weights_to_next_layer[$id_output_node];
$Y += ( $sumPreviousLayer*$NextWeight );
}
$Y += $this->nodes[$id_output_node]->bias;
return $this->f($Y);
} // if !imFirst
} // /Y()
/**
* Export the data of layer into jSON object
*
* @return json $arrJSON
*/
public function exportData2Json(){
$arrJSONNodes = [];
// Create an array of jSON'S data layers
foreach($this->nodes as $node){
$arrJSONNodes[] = json_decode( $node->exportData2Json() );
}
$arrJSON = [ 'Layer' =>
[ 'num_nodes' => $this->num_nodes,
'imFirst' => $this->imFirst,
'imLast' => $this->imLast,
'activation_function' => $this->activation_function,
'Nodes' => $arrJSONNodes
]
];
return json_encode($arrJSON);
} // /exportData2Json()
/**
* Import the data of jSON object into layer
*
* @param json $JsonData
*/
public function importJson2Data($JsonData){
$JsonDataLayer = $JsonData->Layer;
$this->num_nodes = $JsonDataLayer->num_nodes;
$this->imFirst = $JsonDataLayer->imFirst;
$this->imLast = $JsonDataLayer->imLast;
$this->activation_function = $JsonDataLayer->activation_function;
$this->nodes = [];
$i = 0;
foreach($JsonDataLayer->Nodes as $Node){
$this->nodes[] = new node( );
$this->nodes[$i++]->importJson2Data($Node);
}
} // /importJson2Data()
} // /class
?>