-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweight.js
223 lines (191 loc) · 5.7 KB
/
weight.js
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
/**
* Weight
* Deals with the generation and saving of several levels of weights for a neural network.
*/
var Weight = function()
{
/** Initializer **/
/**
* Create weights for then neural networks matching the data.
* @param layerDataArray And array containing the number of neuron for each level (0=input level)
*/
this.init_random = function(layerDataArray)
{
weights = new Array(layerDataArray.length-1);
//Go trhoug the COUPLE of layer
for(var i=0; i<layerDataArray.length-1; i++ )
{
weights[i] = new Array(layerDataArray[i+1]);
//We fill the previous weight for a layer (layer-1)
for(var j=0; j<layerDataArray[i+1]; j++ )
{
weights[i][j] = new Array(layerDataArray[i]);
//Adding as link as there is previous neuron in the previous layer
for(var k=0; k<layerDataArray[i]; k++ )
{
weights[i][j][k] = Math.random()*2 - 1; //Between -1 and 1
}
}
}
}
/**
* Unserialize data from a string
* @param string String with the following structure: w111 w112; w121 w122 \n w211 w212; w221 w222 \n...
*/
this.init_string = function(string)
{
layersStr = string.split("\n");
weights = new Array(layersStr.length -1); // not -1, last \n ignored
//Go trhoug the COUPLE of layer
for(var i=0; i<layersStr.length -1; i++ )
{
neuronStr = layersStr[i].split(";");
weights[i] = new Array(neuronStr.length -1); //-1 to ignore the last ;
//We fill the previous weight for a layer (layer-1)
for(var j=0; j<neuronStr.length -1; j++ )
{
linkStr = neuronStr[j].split(" ");
weights[i][j] = new Array(linkStr.length -1); //-1 to ignore the last space
//Adding as link as there is previous neuron in the previous layer
for(var k=0; k<linkStr.length -1; k++ )
{
weights[i][j][k] = (linkStr[k]);
}
}
}
}
/**
* Unserialize data from a string, but may change the value to evolve !
* @param string String with the following structure: w111 w112; w121 w122 \n w211 w212; w221 w222 \n...
* @param threshold Probability to get a new value
*/
this.init_evolve = function(string, threshold)
{
layersStr = string.split("\n");
weights = new Array(layersStr.length -1); // not -1, last \n ignored
//Go trhoug the COUPLE of layer
for(var i=0; i<layersStr.length -1; i++ )
{
neuronStr = layersStr[i].split(";");
weights[i] = new Array(neuronStr.length -1); //-1 to ignore the last ;
//We fill the previous weight for a layer (layer-1)
for(var j=0; j<neuronStr.length -1; j++ )
{
linkStr = neuronStr[j].split(" ");
weights[i][j] = new Array(linkStr.length -1); //-1 to ignore the last space
//Adding as link as there is previous neuron in the previous layer
for(var k=0; k<linkStr.length -1; k++ )
{
if( Math.random() < threshold )
{ weights[i][j][k] = Math.random()*2 - 1;}
else
{ weights[i][j][k] = (linkStr[k]); }
}
}
}
}
/**
* Unserialize from two string. It merges the two weights with a chance for 0.5 for each !
*/
this.init_merge = function(string1, string2)
{
layersStr1 = string1.split("\n");
layersStr2 = string2.split("\n");
//Check if same size !
if( layersStr1.length != layersStr2.length )
{
console.log("Merge impossible: size of the two NN different: "+layersStr1.length+" and"+ layersStr2.length);
}
weights = new Array(layersStr1.length -1); // not -1, last \n ignored
//Go trhoug the COUPLE of layer
for(var i=0; i<layersStr1.length -1; i++ )
{
neuronStr1 = layersStr1[i].split(";");
neuronStr2 = layersStr2[i].split(";");
weights[i] = new Array(neuronStr1.length -1); //-1 to ignore the last ;
//We fill the previous weight for a layer (layer-1)
for(var j=0; j<neuronStr1.length -1; j++ )
{
linkStr1 = neuronStr1[j].split(" ");
linkStr2 = neuronStr2[j].split(" ");
weights[i][j] = new Array(linkStr1.length -1); //-1 to ignore the last space
//Adding as link as there is previous neuron in the previous layer
for(var k=0; k<linkStr1.length -1; k++ )
{
if( Math.random() < 0.5 )
{ weights[i][j][k] = (linkStr2[k]); }
else
{ weights[i][j][k] = (linkStr1[k]); }
}
}
}
}
/** Attributes **/
/**
* Triple dimension array containing each weight for the neural network (for each neuron: layer->neuron->link to the previous level)
* 0 is weight for link betwenn level 0 and level 1 of the neural networks, for the level 1 layer (the one after the inputs)
* Be aware of the shift !
* It is really created in the initializer
*/
var weights;
/** Methods **/
/**
* Get the array of weight for the wanted neuron at the wanted level (layer-1 to match our array)
*/
this.get_weight = function(layer, neuron)
{
if( layer <= 0 || layer > weights.length )
{ return null; }
else
{
pos = layer - 1;
if( neuron < 0 || neuron > weights[pos].length )
{ return null; }
else
{ return weights[pos][neuron]; }
}
}
/**
* Return the total number of layer in that 'Weight'
*/
this.get_layer_nbr = function()
{
return weights.length + 1;
}
/**
* Return the number of neuron for a given layer
*/
this.get_neuron_nbr = function( layer )
{
if( layer <= 0 || layer > weights.length )
{ return null; }
else
{
pos = layer - 1;
return weights[pos].length;
}
}
/**
* Serialize the array as a string
*/
this.serialize = function()
{
var ser = "";
//Layer
for( var i=0; i<weights.length; i++)
{
//Neuron
for( var j=0; j<weights[i].length; j++)
{
//Link
for( var k=0; k<weights[i][j].length; k++)
{
ser += weights[i][j][k]+" ";
}
ser += ";"; //Separator betwwen neuron
}
ser += "\n"; //separator between layer
}
return ser;
}
}